CompSci 4 Chap 9 Sec 1 October 28, 2010 Prof. Susan Rodger.
-
Upload
crystal-chapman -
Category
Documents
-
view
218 -
download
0
description
Transcript of CompSci 4 Chap 9 Sec 1 October 28, 2010 Prof. Susan Rodger.
![Page 1: CompSci 4 Chap 9 Sec 1 October 28, 2010 Prof. Susan Rodger.](https://reader034.fdocuments.in/reader034/viewer/2022051320/5a4d1b707f8b9ab0599b520a/html5/thumbnails/1.jpg)
CompSci 4Chap 9 Sec 1
October 28, 2010
Prof. Susan Rodger
![Page 2: CompSci 4 Chap 9 Sec 1 October 28, 2010 Prof. Susan Rodger.](https://reader034.fdocuments.in/reader034/viewer/2022051320/5a4d1b707f8b9ab0599b520a/html5/thumbnails/2.jpg)
Announcements• Read Chapter 9, Sec 2 for next time• Assignment 6 due Thursday, Nov 4• Today
– Chapter 9, Section 1 – Lists
![Page 3: CompSci 4 Chap 9 Sec 1 October 28, 2010 Prof. Susan Rodger.](https://reader034.fdocuments.in/reader034/viewer/2022051320/5a4d1b707f8b9ab0599b520a/html5/thumbnails/3.jpg)
Collections
• In some animations, several objects must perform the same actions– Example: marching band marching
• It is convenient to collect all objects into a group (collection)– Major benefit – write code for all the objects in
the group (rather than separate code for each object)
![Page 4: CompSci 4 Chap 9 Sec 1 October 28, 2010 Prof. Susan Rodger.](https://reader034.fdocuments.in/reader034/viewer/2022051320/5a4d1b707f8b9ab0599b520a/html5/thumbnails/4.jpg)
List
• A list - one way to organize objects into a collection– You may use lists to organize
• Shopping list• Todo list
• In programming, a list is a collection of objects or information. We call an organizing structure a data structure.
![Page 5: CompSci 4 Chap 9 Sec 1 October 28, 2010 Prof. Susan Rodger.](https://reader034.fdocuments.in/reader034/viewer/2022051320/5a4d1b707f8b9ab0599b520a/html5/thumbnails/5.jpg)
Creating Lists• In Alice, a list can be a list
of numbers, or a list of objects, or a list of colors, etc.
• Let’s create a list of skeletons
![Page 6: CompSci 4 Chap 9 Sec 1 October 28, 2010 Prof. Susan Rodger.](https://reader034.fdocuments.in/reader034/viewer/2022051320/5a4d1b707f8b9ab0599b520a/html5/thumbnails/6.jpg)
Create List (cont)• Type in name• Select type• Select “make a list”• Add skeletons to list
(click “new item” 4 times)
• Result is:
![Page 7: CompSci 4 Chap 9 Sec 1 October 28, 2010 Prof. Susan Rodger.](https://reader034.fdocuments.in/reader034/viewer/2022051320/5a4d1b707f8b9ab0599b520a/html5/thumbnails/7.jpg)
Programming with a List
• Can “iterate through a list” – Do something to each item in the list
• In order (use “For all in order”)• All together (use “For all together”)
![Page 8: CompSci 4 Chap 9 Sec 1 October 28, 2010 Prof. Susan Rodger.](https://reader034.fdocuments.in/reader034/viewer/2022051320/5a4d1b707f8b9ab0599b520a/html5/thumbnails/8.jpg)
Example/Demo: Iteration in OrderFor each skeleton in order
skeleton says “Boo”
For each skeleton in orderskeleton turns its head around
![Page 9: CompSci 4 Chap 9 Sec 1 October 28, 2010 Prof. Susan Rodger.](https://reader034.fdocuments.in/reader034/viewer/2022051320/5a4d1b707f8b9ab0599b520a/html5/thumbnails/9.jpg)
Applying a Part of an object
• Drag in skeleton turn• Select part• Drag over part• Drag in item• Type in part (must spell correctly!)
![Page 10: CompSci 4 Chap 9 Sec 1 October 28, 2010 Prof. Susan Rodger.](https://reader034.fdocuments.in/reader034/viewer/2022051320/5a4d1b707f8b9ab0599b520a/html5/thumbnails/10.jpg)
Example/Demo: Iteration TogetherFor all skeleton together
skeleton says “Boo”
For all skeleton in togetherskeleton turns its head and neck
around
![Page 11: CompSci 4 Chap 9 Sec 1 October 28, 2010 Prof. Susan Rodger.](https://reader034.fdocuments.in/reader034/viewer/2022051320/5a4d1b707f8b9ab0599b520a/html5/thumbnails/11.jpg)
Add in a girl
• Her parts are almost the same to the head.
• Rename “body” part to “upperBody”• Add her to the list and click “Play”
![Page 12: CompSci 4 Chap 9 Sec 1 October 28, 2010 Prof. Susan Rodger.](https://reader034.fdocuments.in/reader034/viewer/2022051320/5a4d1b707f8b9ab0599b520a/html5/thumbnails/12.jpg)
Add in MadScientist• His parts are not the same structure as the
skeleton, cannot be renamed to match.
• What happens if we put him in the list and Play?
• Note: Alice bug – cannot delete from a list
![Page 13: CompSci 4 Chap 9 Sec 1 October 28, 2010 Prof. Susan Rodger.](https://reader034.fdocuments.in/reader034/viewer/2022051320/5a4d1b707f8b9ab0599b520a/html5/thumbnails/13.jpg)
Selecting an item from a list• You can select a random item or a specific
item from a list. • This code makes a particular skeleton jump
• Suppose we want to have one item from our list (chosen randomly) to jump up and down. How would we do that?
![Page 14: CompSci 4 Chap 9 Sec 1 October 28, 2010 Prof. Susan Rodger.](https://reader034.fdocuments.in/reader034/viewer/2022051320/5a4d1b707f8b9ab0599b520a/html5/thumbnails/14.jpg)
List Questions• What are differences between For all in
order and For all together?• Why is the list a world variable?• When would you want to use each of them?• What can you put in a list?• When can you refer to a part of an object in
a list?• What type of method can you not put in a
For all in order or For all together?
![Page 15: CompSci 4 Chap 9 Sec 1 October 28, 2010 Prof. Susan Rodger.](https://reader034.fdocuments.in/reader034/viewer/2022051320/5a4d1b707f8b9ab0599b520a/html5/thumbnails/15.jpg)
![Page 16: CompSci 4 Chap 9 Sec 1 October 28, 2010 Prof. Susan Rodger.](https://reader034.fdocuments.in/reader034/viewer/2022051320/5a4d1b707f8b9ab0599b520a/html5/thumbnails/16.jpg)
Classwork today
• Create a list of players • Make them do several things (See handout).
![Page 17: CompSci 4 Chap 9 Sec 1 October 28, 2010 Prof. Susan Rodger.](https://reader034.fdocuments.in/reader034/viewer/2022051320/5a4d1b707f8b9ab0599b520a/html5/thumbnails/17.jpg)
Solution to having a random object from a list jump
• Add a variable called jumper
• Drag over and set the variable “jumper” to skeleton temporarily
![Page 18: CompSci 4 Chap 9 Sec 1 October 28, 2010 Prof. Susan Rodger.](https://reader034.fdocuments.in/reader034/viewer/2022051320/5a4d1b707f8b9ab0599b520a/html5/thumbnails/18.jpg)
Solution (cont)
• Drag over the list scaryThings and drop it on skeleton. You can choose which item in the list. Select “random item from list”
![Page 19: CompSci 4 Chap 9 Sec 1 October 28, 2010 Prof. Susan Rodger.](https://reader034.fdocuments.in/reader034/viewer/2022051320/5a4d1b707f8b9ab0599b520a/html5/thumbnails/19.jpg)
Solution (cont)
• Then change “skeleton” in the move commands to “world.jumper”
• Play a random object from your list will jump each time this code executes.