Custom stamping game scores with costume numbers

Lead Image © kudryashka, 123RF.com

Keeping Score

Add style to game score displays with stamp blocks and costumes.

Working with variables is a common task in Scratch, but do you ever get tired of the orange stage monitors (also known as watchers)? Although watchers can change their basic display style and be positioned on the stage, they're still relatively bland, and they're always on an orange background. I'm going to walk through a script that demonstrates how you can use the stamp block and costumes to add style to your game score displays.

Adding a Number Sprite with Costumes

The first task is to create a sprite with 10 costumes. Each costume should represent a number 0 through 9. You can click Import under the Costumes tab to import a costume into an existing sprite and browse to the Letters folder to find a number in one of the available fonts.

You can also draw the costumes yourself using the Paint Editor (click Paint), but don't forget to remove any costumes that do not represent a number.

Sequentially sort the costumes so that they start with number one and end with zero in the 10th position. The scripts I use in these examples will rely on the costume number (its position in the list) to display the correct value on the screen.

As you add the number costumes to your sprite, keep one rule in mind: Do not name your costumes according to the number they represent by typing the digit. For example, don't name your first costume 1, your second costume 2, and so on. When Scratch tries to match a costume via a variable, it will first attempt to match the costume by name. If no costume matches the name, Scratch will next attempt to match the position number.

I avoid this possible problem by spelling out the number when I name the costume. For example, my costumes are named one, two, three, and so on. This lets me identify the costumes in an appropriate, readable way while avoiding unintended problems when matching the correct costume in my script.

Counting Down

To begin, I'll write a simple script to initiate a countdown (Figure 1). You might use this when you want to show a short countdown to a rocket launch or display instructions before your game starts. I'll draw your attention to the switch to costume() blocks that are being used. The first block is selecting the block with the name six. By default, the switch to costume() block includes the names of each costume assigned to the sprite in a drop-down menu, so if you're selecting the costume by name, it's hard to make a mistake.

Figure 1: Two ways to select a costume in Scratch: by name and by number.

The switch to costume() block inside the repeat(5) block performs a calculation to count down through the numbered costumes. It's this calculation that clearly demonstrates how to select a costume by number. As long as you have the costumes sorted correctly, this script will visually count down from five.

This simple script demonstrates that costumes can work with either a number or a name value and shows the limitation of displaying numbers using costumes only. You're limited to representing single digits, unless you create more costumes to represent a wider range of numbers – which could become difficult to manage. In the next section, I take the numbered costumes I created and show you how to display a number of any length using the 10 costumes and the stamp block to display the score of a game.

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