View
215
Download
0
Tags:
Embed Size (px)
Citation preview
Fall 2007 ACS-1805 Ron McFadyen 1
Ch 9 Lists
• In some animations, several objects must perform the same actions
• Example:
• A marching band, where the band members are performing the same marching steps.
• In such situations, it is convenient to collect all the objects into a list – a group or collection of objects
Fall 2007 ACS-1805 Ron McFadyen 2
Lists
• With Alice, we can organize objects into a list.
• In our daily lives, we use lists to help us organize things. For example,
• to-do list
• assignments
• shopping
• In programming, a list is a special kind of data structure.
Fall 2007 ACS-1805 Ron McFadyen 3
Types of Lists• In Alice, a list can be a list of numbers or a list of objects, colors, or other
type ….
Fall 2007 ACS-1805 Ron McFadyen 4
Example
• The famous Rockettes® are preparing for their winter holiday show. You have been hired to create an animation for the holiday show that will be used on a web site where people can purchase tickets for the show.
Fall 2007 ACS-1805 Ron McFadyen 5
Creating the list
Create the initial world and add five rockette objects to the world.
Then, create a new world-level variable in the properties panel.
(Why should it be world-level?)
Fall 2007 ACS-1805 Ron McFadyen 6
Creating the listIn the popup dialog box,
type in a name
select Object type
check “make a List” box
click new item button 5 times to select each of the 5 rockettes, one at a time
Fall 2007 ACS-1805 Ron McFadyen 7
Programming with lists
• One of the most useful operations with a list is to repeatedly perform some action with each item in the list.
This is called iteration or "iterating through a list."
Fall 2007 ACS-1805 Ron McFadyen 8
Iterating through a list
• Two control structures for iterating through a list:
• in order (one at a time)• all together (simultaneously)
Fall 2007 ACS-1805 Ron McFadyen 9
Example: Iteration in Order
• A typical chorus line dance involves each dancer (one after the other) kicking up a leg.
• Possible storyboard:
For all dancers in order item_from_dancers kickUpRightLeg
kickUpRightLeg
Parameter: whichRockette
Do in order Do together whichRockette right thigh turn back whichRockette right calf turn forward whichRockette right calf turn back
• Implementing in Alice ….
Fall 2007 ACS-1805 Ron McFadyen 10
Example: Iteration all together
• A typical chorus line dance involves each dancer (one after the other) kicking up a leg.
• Possible storyboard:
For all dancers simultaneously item_from_dancers kickUpRightLeg
kickUpRightLeg
Parameter: whichRockette
Do in order Do together whichRockette right thigh turn back whichRockette right calf turn forward whichRockette right calf turn back
• Implementing in Alice ….
Fall 2007 ACS-1805 Ron McFadyen 11
Common Uses of Lists
• Iterating through a list of several like items to accomplish the same task with each item.
• As in the previous Rockettes example
• Iterating through a list of several like items to search for an item with a given property...
Fall 2007 ACS-1805 Ron McFadyen 12
Example
• A WacAMole arcade game.
• Little moles pop up from holes in the top of the booth. The user tries to whack the mole before it drops out of sight.
• The game is over when 10 moles are whacked.
Fall 2007 ACS-1805 Ron McFadyen 13
Designing the game
• To design the game animation, we need to answer several questions:
• How will the game work, overall?
• How do we keep score?
• How do we know when the user whacks a
mole?
• How will a list help us?
Fall 2007 ACS-1805 Ron McFadyen 14
How will the game work?
• A mole pops up and then goes back down. Each time the mole pops up, the user attempts to use the mouse to click the mole.
• When a click occurs, we check to see if a mole was clicked. If so, a sound plays and the score increases.
• The above actions repeat until the game is over.
Fall 2007 ACS-1805 Ron McFadyen 15
Is the game over?
Overall design
pop random mole
no
yescongratulate
Mouse click event
See next slide
Fall 2007 ACS-1805 Ron McFadyen 16
Overall design
Did mouse click on a mole?
yes Play sound
Change score display
Mouse click event
no
do nothing
Fall 2007 ACS-1805 Ron McFadyen 17
Storyboard for overall game
• This is the main driver for the game.
• The code will be written in World.my first method
While the game isn't over randomly select one mole and call popMole
Congratulate the user
Fall 2007 ACS-1805 Ron McFadyen 18
Storyboard: popMole
popMole
Do in order Move the mole up Wait some time Move the mole back down
Fall 2007 ACS-1805 Ron McFadyen 19
Keeping Score
• We will use a visual scorekeeper.
• The scorekeeper is made up of two cylinders• A gray cylinder above the
ground • A yellow cylinder below the
ground.• Each cylinder is 1 meter in height.
Fall 2007 ACS-1805 Ron McFadyen 20
Keeping score
• Each time the user successfully clicks a mole, the yellow cylinder will move up 1/10 meter.
• When the yellow cylinder is above ground (has moved up 10 times), the game is over.
Fall 2007 ACS-1805 Ron McFadyen 21
When a mole is clicked
• This is where a list comes in handy:
• create a list of the moles (one mole is below each hole)
• create a mouse click event
• each time the mouse is clicked, call a score method to iterate through the list of moles to see whether one of the moles has been clicked!