Keeping Score with Global Variables

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.

  1. Open your U1L1-ClickAlonzo project, play the game, and review the code so you remember how it works.
  2. "U2L1-ClickAlonzo"Save your work as U2L1-ClickAlonzo Be sure to rename your project with "U2" in the filename.
  3. 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.
  4. Create a global variable named score. Here's how.
  5. Making a Global Variable

    1. Click make a variable button in the Variables palette. (It's not a block; you can't drag it into the scripting area.)
    2. Type the title for your variable. In this case, it's score.
    3. Click "OK."
  6. Use the score variable to keep track of the player's score:
    1. Initialize score to 0 at the beginning of the game.
    2. Setting the starting value of a variable is known as initializing the variable.

      AAP-1.B.1
    3. Make the program change the score by 1 whenever the sprite is clicked.
    4. change (score) by (1) (which means set(score) to (score+1)) would be written as
      score ← score + 1
      or score ← score + 1.
  7. Pair Programming Swap
  8. Test and debug. Play the game enough to make sure that the score variable works. Save your work
  9. Talk with Your Partner 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.
  10. Use a conditional to determine when the player reaches that score. When they do:
    1. Make Alonzo stop jumping around.
    2. Make Alonzo appear again. (You'll have to reset the ghost effect.)
    3. Congratulate the player for a few seconds.
  11. Test and debug. Take turns playing, and fix any bugs. Save your work

    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).
      Checking the global variables in the palette.Displaying the global variable on the stage
    • 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 use hide variable to hide it again).

  1. If the sprite moves without being clicked, make the score go down by 1.
  2. 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.