Page 5: Choosing a Costume

Unit 2, Lab 1, Page 5

In Click Alonzo, the game switched back and forth between two different versions of the Alonzo costume in order to tell the player whether they successfully clicked Alonzo. On this page, you will allow the user of your Click Alonzo program to choose the sprite’s costume.

You learned how to add costumes on Unit 1 Lab 2 Page 3: Customizing and Debugging.

  1. If it isn’t open already, open your U2L1-ClickAlonzo project.

  2. Set up what you need for this new version:

    1. Add several costumes.
    2. Delete the backwards-Alonzo costume.
    3. Remove the code in the program that changes the costume when you click the sprite.
  3. Experiment with the switch to costume [] block using different inputs to switch the sprite’s costume manually a few times.

If you’re playing your game and you want to change the costume, you can just choose the costume you want by name, in the switch to costume block’s pulldown menu. But imagine you’re giving this game to a non-programmer friend to play. You want to program the selection of a new costume inside the game, so your friend doesn’t have to know anything about blocks.

When you used item of before (for example, inside who and does what) , you were accessing items of the list randomly. But you can also select a specific item by specifying its position.

: Index

The position number is called the index of the item in the list.
item (2) of (list (apple) (cantaloupe) (banana)) reporting 'cantaloupe'
In this list, 1 is the index of the item “apple,” 2 is the index of the item “cantaloupe,” and so on.

In Snap! and on the AP exam, the index is always a whole number (1, 2, 3, 4, etc.). It is an error to use an index less than 1 or greater than the length of the list.

Lists can contain anything: letters or words, costumes, other lists, or even blocks. You can use my to report a list of the costumes for your sprite.

The my block looks like my (neighbors) until you select “costumes” from its drop-down menu.

my (costumes) reporting a list of three costumes: a girl with an afro, a penguin, and a unicorn

And you can use item of together with my to select a costume in a specific position in the list.

Some blocks (such as item of) have input slots that expect a list. You can tell because the input slot looks kind of like a list:
item (1) of 'list input slot' picture of 2-item list

  1. Practice using the index of the costumes in your list to switch the sprite’s costume a few times.

    You can drag blocks into the input slot of switch to costume even though it’s a drop-down menu.
    switch to costume [] switch to costume (item (1) of 'list input slot')

  1. Modify your code so the player can select the sprite costume:
    1. Use a costumes script variable to store the list of sprite costumes.
    2. Use show variable [] to show the contents of that variable.<
    3. Ask the player “Which costume number do you want?”
    4. Change to the costume with the index number in the player’s answer.
    5. Hide the costume list.

After completing this step, your program should do behave exactly like this:

The list expression item (2) of (costumes) would be written as
costumes[2]

or costumes[2]. All three versions of this code would report/return the penguin costume (if only the AP language had costumes as a data type):
item (2) of (costumes) reporting a picture of the penguin costume

  1. Talk with Your Partner Without using Snap!, predict what each of the following expressions will report:

    1. length of ( list (pineapple) (strawberry) (kiwi) (mango))
    2. length of ( join (pineapple) (strawberry) (kiwi) (mango))
    3. length of ( list (list (pineapple) (strawberry) (kiwi)) (mango))
    4. length of (item (1) of (list (list (pineapple) (strawberry) (kiwi)) (mango)))
  2. Experiment in Snap! to verify your predictions.

The expression length of (words list) would be written as
LENGTH(wordsList)

or LENGTH(wordsList).