CS320n – Elements of Visual Programming Lists Mike Scott (Slides 9-1) Thanks to Wanda Dann, Steve...

16
CS320n – Elements of Visual Programming Lists Mike Scott (Slides 9-1) hanks to Wanda Dann, Steve Cooper, and Susan Rodger or slide ideas.

Transcript of CS320n – Elements of Visual Programming Lists Mike Scott (Slides 9-1) Thanks to Wanda Dann, Steve...

Page 1: CS320n – Elements of Visual Programming Lists Mike Scott (Slides 9-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.

CS320n – Elements of Visual Programming

Lists

Mike Scott

(Slides 9-1)

Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.

Page 2: CS320n – Elements of Visual Programming Lists Mike Scott (Slides 9-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.

Visual Programming Animation Programs 2

Collections• In some animations, several objects must

perform the same actions– Example:

• Cars in lanes from Frogger

• In such situations, it is convenient to collect all the objects into a group (a collection)

• Major benefit of Lists is we can write code for each object in the list – rather than separate code for each object

Page 3: CS320n – Elements of Visual Programming Lists Mike Scott (Slides 9-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.

Visual Programming Animation Programs 3

List• One way to organize objects into a collection is

to use a list.– We use lists to help us organize things.

For example,• assignments list• shopping list• email inbox

• In programming, a list is known as a data structure.– an object that is used to group other objects together

Page 4: CS320n – Elements of Visual Programming Lists Mike Scott (Slides 9-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.

Visual Programming Animation Programs 4

Types of Lists• In Alice, a list can be a list of numbers or a list of

objects, colors, or other type ….• In this session, we will use an example of a list of

objects.

Page 5: CS320n – Elements of Visual Programming Lists Mike Scott (Slides 9-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.

Visual Programming Animation Programs 5

Example• Create a List of chickens• add 5 chicken objects to the world

Page 6: CS320n – Elements of Visual Programming Lists Mike Scott (Slides 9-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.

Visual Programming Animation Programs 6

Creating a list: Step 1

Create the initial world and add five chicken objects to the world.

Then, create a new world-level variable in the properties panel.

(Why should it be world-level rather than class-level?)

Page 7: CS320n – Elements of Visual Programming Lists Mike Scott (Slides 9-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.

Visual Programming Animation Programs 7

Creating a list: Step 2In 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 chickens, one at a time

Page 8: CS320n – Elements of Visual Programming Lists Mike Scott (Slides 9-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.

Visual Programming Animation Programs 8

Resulting List

• No change in the scene

• We have simply added them to a list variable

Page 9: CS320n – Elements of Visual Programming Lists Mike Scott (Slides 9-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.

Visual Programming Animation Programs 9

Programming with a list

• Now that a list has been created, how do we use the list in a program?

• One of the most powerful 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."

Page 10: CS320n – Elements of Visual Programming Lists Mike Scott (Slides 9-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.

Visual Programming Animation Programs 10

Iterating through a list• Iteration can be done in two ways:

– in order (one at a time)– all together (simultaneously)

Page 11: CS320n – Elements of Visual Programming Lists Mike Scott (Slides 9-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.

Visual Programming Animation Programs 11

Example/Demo: Iteration in Order

• Have each chicken say hello

• Have each chicken kicks its legs

Page 12: CS320n – Elements of Visual Programming Lists Mike Scott (Slides 9-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.

Visual Programming Animation Programs 12

The “Object’s part named X” function

• One problem when passing Objects as parameters is how do I affect a sub part?

• If the parameter type is simply Object subparts are unknown

• Same problem with elements of a list

• Use the Object’s part named X function

Page 13: CS320n – Elements of Visual Programming Lists Mike Scott (Slides 9-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.

Visual Programming Animation Programs 13

Part named function• Get "in order" operation for list

• Drop in a Chicken object

• Pick functions for Chicken Object and drag the part named function to replace the Chicken object

Page 14: CS320n – Elements of Visual Programming Lists Mike Scott (Slides 9-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.

Visual Programming Animation Programs 14

Part Named Function• Select which part

• Pick other and type in name of sub part

Page 15: CS320n – Elements of Visual Programming Lists Mike Scott (Slides 9-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.

Visual Programming Animation Programs 15

Part Named function• Replace Chicken object with

item_from_chickens

• Final version

Page 16: CS320n – Elements of Visual Programming Lists Mike Scott (Slides 9-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.

Visual Programming Animation Programs 16

Example / Demo: Iteration Together

• Change the previous animation so that the chickens move their legs together