Michael Badger

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.
[null, {"document_url": "http://www.raspberry-pi-geek.com/Archive/2016/17/Custom-stamping-game-scores-with-costume-numbers", "unformatted_src": "/var/rpi/storage/images/archive/2016/17/custom-stamping-game-scores-with-costume-numbers/scratch_cat21_123rf_kudryashka_clothes.png/24206-1-eng-US/Scratch_Cat21_123RF_Kudryashka_clothes.png_large.png", "src": "https://www.raspberry-pi-geek.com/var/rpi/storage/images/archive/2016/17/custom-stamping-game-scores-with-costume-numbers/scratch_cat21_123rf_kudryashka_clothes.png/24206-1-eng-US/Scratch_Cat21_123RF_Kudryashka_clothes.png_large.png", "formatted_filename": "Scratch Cat RF Kudryashka clothes"}, null, {"document_url": "http://www.raspberry-pi-geek.com/Archive/2016/17/Custom-stamping-game-scores-with-costume-numbers", "unformatted_src": "/var/rpi/storage/images/archive/2016/17/custom-stamping-game-scores-with-costume-numbers/figure-1/24209-1-eng-US/Figure-1_large.png", "src": "https://www.raspberry-pi-geek.com/var/rpi/storage/images/archive/2016/17/custom-stamping-game-scores-with-costume-numbers/figure-1/24209-1-eng-US/Figure-1_large.png", "formatted_filename": "Figure large"}, null, {"document_url": "http://www.raspberry-pi-geek.com/Archive/2016/17/Custom-stamping-game-scores-with-costume-numbers", "unformatted_src": "https://developer.android.com/images/brand/en_generic_rgb_wo_45.png", "src": "https://developer.android.com/images/brand/en_generic_rgb_wo_45.png", "formatted_filename": "en generic rgb wo", "alt_text": "Get it on Google Play", "rendered_width": 102, "rendered_height": 35}, null, {"document_url": "http://www.raspberry-pi-geek.com/Archive/2016/17/Custom-stamping-game-scores-with-costume-numbers", "unformatted_src": "https://developer.android.com/images/brand/en_generic_rgb_wo_45.png", "src": "https://developer.android.com/images/brand/en_generic_rgb_wo_45.png", "formatted_filename": "en generic rgb wo", "alt_text": "Get it on Google Play", "rendered_width": 102, "rendered_height": 35}, null, {"document_url": "http://www.raspberry-pi-geek.com/Archive/2016/17/Custom-stamping-game-scores-with-costume-numbers", "unformatted_src": "/var/rpi/storage/images/media/images/magazine-covers/rpg_23_cover_shop_us/30142-1-eng-US/RPG_23_Cover_Shop_US_listitem.png", "src": "https://www.raspberry-pi-geek.com/var/rpi/storage/images/media/images/magazine-covers/rpg_23_cover_shop_us/30142-1-eng-US/RPG_23_Cover_Shop_US_listitem.png", "formatted_filename": "RPG Cover Shop US listitem"}, null]