Custom stamping game scores with costume numbers

Stamping the Score

Figure 2 shows two scripts to display the number value in a variable, which in this case I'm calling Score. This script could plug in to any existing game you have and add some style to the plain variable display commonly used.

Figure 2: Using the stamp tool and costumes to display a custom-styled number on the screen.

The stack that starts with the when <green flag> clicked block is being used as a random score generator and will help you create values to test the script before you add it to an existing project. The key point is that the Show Score script is called via a broadcast message when you want to display the score. This allows you to synchronize the score display to some event in the game, be it at the end of the game, between levels, or as often as you'd like. If you have a fast-action game with a lot of quick score updates, it might be possible that the variable will update quicker than your Show Score script can display the changes with the broadcast() and wait blocks.

As the Show Score script shows, you need two variables to make this work. The Score variable holds the number you want to display, and the digit variable identifies each place value in the Score variable so that the script can identify each digit of the number.

The Score variable has the potential to change continually, so it helps to have a way to always know the exact length of the number inside the variable. The length of(Score) block reports a number value that identifies how many digits are in Score. For example, if Score contains the number 10, then its length is 2. If Score contains the number 4324, then its length is 4.

However, it's the repeat block that makes this script flexible enough to handle any size number. By using 10 costumes and the repeat(length of(Score)) control, you never have to think about how many digits your number has. The script always knows.

Each time the script loops through the Score variable, it calculates the appropriate costume to use by evaluating the letter() of() block. Walking through an example, assume Score is 5. When the script runs, the digit value is set to 1 to represent the starting number. Before the switch to costume() block can identify the correct costume, it evaluates letter(digit) of(Score) to find which number occupies the first place in Score. The answer is 5; therefore, the block becomes switch to costume(5) and displays costume number 5.

Selecting costumes in this manner is not an obvious Scratch 1.4 feature. Remember how I started out with an explanation of Scratch's matching rules when I was naming the costumes? Scratch first looks for a costume named 5 and if it does not find one by that name, it will select the fifth costume. If there is no fifth costume, it will select the last costume listed in the Costumes tab for the sprite. If you set up the costumes correctly, my example will stamp the number 5 costume onto the screen.

At then end of the loop, the digit variable increases by one, but its only role is to select the next position value in Score, if the loop runs one more time. The repeat() block keeps track of how many times it has run and will continue to run as long as the repeat value is less than the length of the Score variable. If you want to stamp an ever-changing score continually, the same script will work using a broadcast message. Send the broadcast each time you want to update the score display.

Beyond Numbers

I suspect at this point you might be wondering if this only applies to numbers. That's a good question. The script doesn't actually know it's selecting a number. It just displays and stamps the image it finds on any given costume. In my previous example, I used 5 as a Score to select the fifth costume. If the fifth costume was a picture of a chicken, that's what would have displayed.

Also the letter() of() block can work with any text values, not only numbers. If I was writing a script to stamp out a word, I might create a sprite with 26 costumes to represent each letter of the English alphabet. How I name the variable gives some context to what I'm trying to do, so digit and Score provide that insight.

Buy this article as PDF

Express-Checkout as PDF

Pages: 4

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content