Scratch game making tutorial 114

Auto-bots 2

In this tutorial you will programme a robot (bot) to find a reward in a maze.

 

1. Create a new game and then right-click the cat thumbnail ("sprite1") to delete the cat sprite.

 

 

2. Select the stage and switch to the "Backgrounds" tab. You need a maze background with a few short black walls (like this image) ..

 

You can either paint your own, or download, save and then import this background image ..

 

When you have finished your maze, delete the blank white background.

 

 

3. Keep the stage selected and create these variable ("for all sprites") ..

The "bot speed" variable will allow you to control the speed of the bot by dragging an on-screen slider.

Right click the "bot speed" display on the stage and select "slider" ..

Now right click it again and select "set slider min and max" and set the values to these ..

You will be able to set the speed of your bot between 0 and 5.5 steps.

Click OK and then position the variable slider on the edge of the stage.

 

 

4. Use "File>Save as" to save your game. Name it "Auto-bots 2".

 

 

5. Add these scripts to the stage ..

When the game starts the bot's speed will be set to 1 step. You will be able to change it with the slider.

Press the "B" key to test your slider. Press "E" to end when you are done.

 

 

6. We will use a Timer to see how long it takes the bot to find the reward. Add this script to the stage ..

Position the timer display in a white gap at the edge of the stage.

Press the "B" key to test your timer. Press "E" to end when you are done.

 

 

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

 

 

8. Time to script the bot. You can draw your own bot by clicking on the "Paint new sprite" button under the stage. Or download and save this sprite ..

.. and then use the "Choose new sprite from file" button under the stage to load it ..

 

 

9. Make sure you bot is named "bot 1", then add this script to the bot ..

This script will place the bot somewhere random on the stage when the game begins.

 

 

10. Now for the clever bit. We need the bot to move, but change direction when it hits a maze wall. Add this script ..

Notice how the bot turns around when it hits a wall (turn 180 degrees) and then chooses a random direction to move off in.

Press the "B" key to test your scripts. Press "E" to end when you are done.

 

 

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

 

 

12. We are going to put a spinning reward somewhere in the maze and see how long it takes for the bot to find it.

You can draw your own Reward sprite by clicking on the "Paint new sprite" button under the stage. Or download and save this sprite ..

.. and then use the "Choose new sprite from file" button under the stage to load it ..

 

 

13. Make sure you Reward is named "reward", then add these scripts to it ..

This script will place the reward somewhere random on the stage when the game begins.

If you have painted your own reward sprite, you may need to choose your own size %.

Now add this script to make the reward spin, and the game stop when the bot touches it ..

 

 

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

 

 

15. Well done. You've finished. Could you turn this exercise into a full game?