1. Coding a game program with Scratch Download the ‘Desert Rally.sb2� file which is located in the Home Page and save it to NUC. Then bring the file from Scratch.
Now, enjoy the game using the right, left, and up arrows on keyboard. To start the game, press the Green flag button.
Could we also program these kinds of games on our own? In order to do so, you should be able to use the information we have learned so far and analyze the important coding concepts in the game.
There are two ways the game ends. It ends when all four characters are gone with a car explosion or arriving at the pyramid safely.
In the 'Sprite List', select ‘Giza’ sprite, and click ‘Scripts’ tap in the 'Palette' section. You can figure it out that this 'Costume' will appear 60 seconds or a minute later. On the bottom, there is a command block called ‘broadcast’, and it has a strong function that gives notifications to all objects.
2. Variable, Loop and Broadcasting Click the background icon in the 'Sprite List' section. You will find that there are eleven game backgrounds if you click the ‘Backdrops’ tab of the 'Palette' section. Click the ‘Scripts’ tab, which has a program that is divided into three areas.
We decided a variable named ‘Time’ in upper area of the program. Set the variable by clicking the function ‘Data’ and choosing ‘Make a Variable’. Name the variable ‘Time’ and you will find that a few command blocks have been created for the variable. The value of the variable will increase every second. As you saw before, the program ends when the 'Time' variable reaches to 60 seconds.
Let’s now analyze the area in the middle. Among the blocks of ‘Control’ function,
‘forever’ and
‘repeat’ command are called as ‘Loop’. This means that it is repetitive. Even if the number of the day backgrounds is fewer, when you repeat it 8 times, you will see that it lasts longer than the night background.
On the bottom, there is the ‘receive’ command block that receives the message that Giza object has broadcasted. It works the same way as each household receives signals from the broadcasting stations by changing TV channels.
3. Coordinates To understand the x, y coordinates of a sprite, select ‘New’ in ‘File’ menu. Among the four 'New backdrop' buttons on the left hand side of the 'Sprite List' section, select 'Choose backdrop from library'. In the ‘Other’ category, find ‘xy-grid’ and double click it.
Instead of the clicking on the background, click the ‘Sprite 1’ and select ‘Scripts’ tap of the 'Palette' section.
The x, y coordinates on the upper right corner of the 'Script Area' show the current location of the sprite. When a new sprite is created, the values of x and y coordinates are set to 0. Move the cat to see how the x and y values have changed.
The coordinates on the bottom right-hand side of the ‘Stage’ section show the location of the mouse curser. The smallest value that enables the mouse to move is called pixel, and the x-axis is composed of 480 pixels while the y-axis consists of 360 pixels.
4. The role of each sprite We are going to look at how each object is programmed by bringing the game source file again.
In the case of ‘Road’, we will create an optical illusion effect by reversing the road and making it look as if the car is moving forward.
In the case of ‘car’, there are two costumes.
It changes to ‘Boom’ only when the characters defined as ‘Life’ variable disappear.
When you click the right and left arrows on keyboard, it has the ability to go forward and backward.
The program keeps changing the y coordinate to create an effect of moving the car up and down, as if it is driving on a rough road. If you press the upward arrow key, the car can jump.
The four characters in the car are supposed to move along with the car. When the car hits a barrier, there is a predetermined order of who will disappear first.
In each obstacle, the number of characters is programmed to decrease when the car touches the obstacle.