Scratch game exercise 125a

Changing backgrounds

In this tutorial you will make a game that in which the backgrounds change as the cat walks off the left and right edges of the stage.

 

Complete these steps ..

 

 

1. Create a new game, select the stage, and add these scripts to begin and end our game ..

 

 

2. Select the cat sprite, give it a name, such as "Cat", and then click it's "only face left right" button ..

 

 

3. The Cat will only walk left and right, so let’s place it in the middle of the stage when the game begins. Add this script ..

 

 

4. Next, add the scripts to make the Cat walk left and right ..

Press the "B" key on your keyboard and then use the right and left arrow keys on your keyboard to test your game.

 

 

5. When the cat reaches the right edge of the stage we want the background to change to the next one. When the cat reaches the left edge of the stage we want the background to change to the previous one.

It will also be good if when the background changes, it looks as if the cat has just walked into view.

Press the "E" key to end your game and then add this script to the cat ..

Now, when the cat reaches the right edge of the stage it broadcasts "going right". When it reaches the left edge of the stage it broadcasts "going left". The stage will receive these broadcasts and change the background number.

Press the "B" key on your keyboard and then use the right and left arrow keys on your keyboard to test your game. Press "E" to end when you are done.

 

 

6. Use "File>Save as" to save your game. Name it "Moving backgrounds".

 

 

7. We need at least stage backgrounds for this game, but 7 or more will be better.

Press the "E" key to end your game and then select the stage and then the Backgrounds tab. Click the Paint and Edit buttons to paint some backgrounds. Be as imaginative as you like. Here are the ones we made ..

When you have finished take a minute to notice that each background has a name and a number. In the picture above the "sand" background is number 6, and the "trees" background is number 3.

 

 

8. he game needs to know which background is currently showing so it can switch to the next one correctly.

Select the stage, click on the Scripts tab, and then create this variable ..

We will use this variable to calculate which background to switch to next.

 

 

9. Add this script to the stage ..

We want to start with the middle background. We created 7 backgrounds so we will start on 4 ("road). You might have a different number of backgrounds.

 

 

10. When we begin the game, background number 4 will show ("road").

If the cat then walks to the right edge of the stage we want to switch to background 5 (in our game "rocks").

If instead the cat walks to the left edge of the stage we want to switch to background 3 (in our game "trees").

Add these scripts ..

Press the "B" key on your keyboard and then use the right and left arrow keys on your keyboard to test your game. Press "E" to end when you are done.

 

 

11. Use "File>Save" to re-save your game.

 

 

12. Well done. You've finished. Can you add ideas to make this into a complete game?