Scratch Part 7: Variables

A variable is a named object in a program that holds a value that can change in the course of a program. For example, you might have a variable called 'total' that changes by adding a value to it.

Variables are usually initialised at the start of a program to a starting or default value. This is often zero.

Add a Variable in Scratch:

Task 8: Use A Variable

A variable can be used to keep the score in a game.

Go back to the game you created in task 2. Remember, your character was dodging objects as they crossed the screen. You could make it so that some objects scored points for and other objects scored points against. When the total reaches a certain level you could switch backgrounds and sprites to another level. You could make some sprites disappear before they cross the screen. You could add movement in the X direction so that the character can go backwards and forwards to collect points.

In the race track game you would like to keep a count of the laps completed. How could you do this? Your solution may be flawed. How can you improve it? Make a lap counter for each car and set them both to zero for the start. Add 1 to each lap counter when a car completes a lap. Define a condition to end the race and declare a winner.

 

Task 9: Firing Missiles: Space Invaders

For a space invader game you need to have a weapon that moves across the bottom of the screen. You can probably write the code for the weapon moving from left to right at once but what about the missile? The missile should fire from the current position of the weapon so you have to keep track of where the weapon is on the x axis. The weapon sprite knows where it is on the x axis but the missile sprite has its own x and y positions. You need to keep the x position of the missile at the same value as the weapon: how can this be achieved?

Remember that this problem is in the section on variables so that must have something to do with it. When you create a variable you have the option of making it available to all sprites (called a 'global' variable) or to the current sprite only (called a 'local' variable). The value in a global variable is available to all sprites.

That's enough hints!

You may meet a problem in your code: the value of the variable keeps changing outside the range of the screen. You will have to add code that prevents this happening.

To complete a Space Invaders game you will have to program some sprites to attack the weapon with weapons of their own. You could make rows of invaders move slowly towards the base of the screen or have sprites flying across the screen trying to bomb your weapon. Good luck!

Task 10: A Visual Counter

In some games you will see a visual counter on the screen that indicates how much time remains or has passed. One way to place a timer on the screen is to create a sprite with a scale on it and then display a pointer to the scale as time passes.

For example, you could create a sprite with numbers. At 12 points you can fit numbers 1-30 across the screen, representing half a minute.

You can then advance another sprite across the numbers at one second intervals. For longer periods you would have to put longer intervals on the scale and wait longer to move the indicator.

One problem is that the single digits are half the width of double digits so you would either have to vary the size of step taken after 9 or spread the scale markers out evenly.

This would require a variable to count the number of seconds and the position of the indicator sprite.

Here is one possible solution that works with a row of numbers 1-30 along the bottom of the screen.

Task 11: More Games

You know enough now to develop a range of games. You could make a racing game where the cars start from two separate points and race each other to a finishing point; this could be in the form of a maze which is difficult to navigate.

Or you could make a game where one sprite chases other sprites around the screen, scoring points as it touches them and losing points if it touches obstacles.

Look at examples on the Scratch web site for inspiration. You should create a login at home so that you can download programs to see how they work.