Scratch game exercise 125b

Changing backgrounds advanced

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.

This tutorial contains some extra scripts which prevent the Cat from walking beyond the last background, and allow new sprites to appear for the individual backgrounds.

 

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.

 

 

6. The 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 and to sense when the Cat has walked left to the first background and right to the last background.

 

 

7. Select the Cat and add this script.

The "if current background" scripts ensure the cat cannot walk further than the first and last backgrounds.

The "if x position" scripts switch the stage backgrounds with a broadcast and also re-position the Cat so it looks as though it is walking into the next background.

When you create your backgrounds you will need to adjust the number in the "current background < 7" script if you have more or less than 7 backgrounds..

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 .

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.

 

 

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

 

 

9. 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.

 

 

10. 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.

 

 

11. 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.

 

 

12. Now you have completed the basic exercise you can add extra sprites to your game. Let's add a Bee that appears and chases the Cat only when background 2 is showing.

Use the "choose new sprite from file" button under the stage to import a bee from the Animals folder, then add this script ..

 

 

13. Let's add another sprite that follows the Cat over 2 backgrounds.

Use the "choose new sprite from file" button under the stage to import a Grasshopper from the Animals folder, then add these scripts ..

Notice how the "when I receive" broadcasts are used to position the Grasshopper when the background changes.

 

 

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

 

 

15. Well done. You've finished. Now add more sprites, health or whatever you want to turn this into a complete game.