Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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 2008 ACS 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 2008 ACS 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 2008 ACS 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 2008 ACS Ron McFadyen

5 Creating the list Create the initial world and add five rockette objects to the world. Create a new world-level variable in the properties panel. Fall 2008 ACS Ron McFadyen

6 Creating the list In 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 2008 ACS 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 2008 ACS 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 2008 ACS 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: kickUpRightLeg Parameter: whichRockette Do in order Do together whichRockette right thigh turn back whichRockette right calf turn forward whichRockette right calf turn back For all dancers in order item_from_dancers kickUpRightLeg Implementing in Alice …. Fall 2008 ACS 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: kickUpRightLeg Parameter: whichRockette Do in order Do together whichRockette right thigh turn back whichRockette right calf turn forward whichRockette right calf turn back For all dancers simultaneously item_from_dancers kickUpRightLeg Implementing in Alice …. Fall 2008 ACS 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 2008 ACS 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 2008 ACS 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 2008 ACS 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 2008 ACS Ron McFadyen

15 Overall design See next slide Is the game over? yes congratulate no
pop random mole Mouse click event See next slide Fall 2008 ACS Ron McFadyen

16 Overall design Mouse click event yes Did mouse click on a mole?
Play sound Change score display no do nothing Fall 2008 ACS 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 2008 ACS Ron McFadyen

18 Storyboard: popMole popMole Do in order Move the mole up
Wait some time Move the mole back down Fall 2008 ACS 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 2008 ACS 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 2008 ACS 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! Fall 2008 ACS Ron McFadyen

22 Storyboard: keep score
Event: User clicks mouse Response: score For All in order If any mole in the mole list was the object clicked Make a noise Move the playerscore (yellow cylinder) up 1/10 meter And implementing in Alice … Fall 2008 ACS Ron McFadyen


Download ppt "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."

Similar presentations


Ads by Google