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.
If it isn’t open already, open your U2L1-ClickAlonzo project.
Set up what you need for this new version:
- Add several costumes.
- Delete the backwards-Alonzo costume.
- Remove the code in the program that changes the costume when you click the sprite.
Experiment with the 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.
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 until you select “costumes” from its drop-down menu.
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:
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.
You used ask
and answer
on Unit 2 Lab 1 Page 2: Checking the Player’s Guess.
- Modify your code so the player can select the sprite costume:
- Use a costumes script variable to store the list of sprite costumes.
- Use to show the contents of that variable.<
Ask
the player “Which costume number do you want?”- Change to the costume with the index number in the player’s
answer
. - Hide the costume list.
After completing this step, your program should do behave exactly like this:
costumes[2]
or . All three versions of this code would report/return the penguin costume (if only the AP language had costumes as a data type):
Without using Snap!, predict what each of the following expressions will report:
Experiment in Snap! to verify your predictions.
LENGTH(wordsList)
or .