Page 4: Keeping Score with Global Variables
Unit 2, Lab 1, Page 4
On this page, you will use a global variable to keep score in your Click Alonzo game.
In your Number Guessing Game, you used script variables to store information (the value of secret number) that was needed by only one script. To keep score in Click Alonzo, more than one script will need access to that information, so you’ll use a global variable.
: Global Variable
A global variable is a variable that is usable by all scripts in the program.
When are global variables needed?
Most of the time, script variables are a better choice because they can’t be changed by some other part of the program, and so they reduce the chance of bugs. But if multiple scripts need access to a variable or if the information in that variable needs to be saved with your project, then use a global variable.
Open your U1L1-ClickAlonzo project, play the game, and review the code so you remember how it works.
- If you never completed Click Alonzo or don’t find your own version, please use this version
- “U2L1-ClickAlonzo”
Be sure to rename your project with “U2” in the filename.
If you save new work under an old filename, Snap! will overwrite your original project. Use “Save as…” from the Snap! File menu to save with the new name. When you load an older project and are about to make a big change, get in the habit of saving first, with the new name, before you change anything.
- Create a global variable named score. Here’s how.
Making a Global Variable
- Click in the Variables palette. (It’s not a block; you can’t drag it into the scripting area.)
- Type the title for your variable. In this case, it’s score.
- Click “OK.”
- Use the score variable to keep track of the player’s score:
- Initialize score to 0 at the beginning of the game.
-
Make the program
change
the score by 1 whenever the sprite is clicked.
Setting the starting value of a variable is known as initializing the variable.
(which means ) would be written as
score ← score + 1
or .
Test and debug. Play the game enough to make sure that the score variable works.
Right now, the game doesn’t have a way to win, but you’ll change that. Decide on the score that you will use to test whether the player has won.
Use a conditional to determine when the player reaches that score. When they do:
- Make Alonzo stop jumping around.
- Make Alonzo appear again. (You’ll have to reset the ghost effect.)
- Congratulate the player for a few seconds.
- Test and debug. Take turns playing, and fix any bugs.
Debugging Tip: Display of Variables
When you are debugging your code, it can help to see the value of a variable at different points in your program. This is done differently for local and global variables:
Global variables have check-boxes that control the display of the variable watcher on the stage (shown below).
If it’s a local (script) variable, you can use the
show variable
block inside the script to display a watcher on the stage (and usehide variable
to hide it again).
If the sprite moves without being clicked, make the score go down by 1.
Game too hard? Increase the score by 2 for a hit. Too big a change? Increase by 3 for a hit, decrease by 2 for a miss. You can make arbitrarily fine adjustments. This process is called playtuning a game.