Scratch game making tutorial 142

Snooker

In this tutorial you create a snooker game.

 

Complete these steps ..

 

1. Open Scratch, OR create a new game (from the top menu choose "File>New").

 

 

2. Select the cat sprite under the stage, and then right-click to delete it.

 

 

3. Create the following variables ("for all sprites") ..

Name Function
Points Used to keep score
cue angle Used to ensure the cue ball moves in the same direction as the cue
cue ball angle Used to ensure the coloured balls move in the correct direction when the cue ball strikes them
cue ball speed Used to calculate the deceleration of the cue ball and the initial speed of any other ball it hits
red ball angle Used by the other balls to determine which direction to move when hit by the red ball
red ball speed Used to calculate the deceleration of the red ball and the initial speed of any other ball it hits
yellow ball angle Used by the other balls to determine which direction to move when hit by the yellow ball
yellow ball speed Used to calculate the deceleration of the yellow ball and the initial speed of any other ball it hits
blue ball angle Used by the other balls to determine which direction to move when hit by the blue ball
blue ball speed Used to calculate the deceleration of the blue ball and the initial speed of any other ball it hits

Leave just the Points variable visible on the stage.

 

 

4. You need to import an image of a snooker table for your background.

Download, save, and then use the "Import" button in the Backgrounds tab of the stage to import the following background image ..

 

Notice how this background of a snooker table has 6 pockets to pot the balls into.

Delete the white background.

 

 

5. Select the stage Scripts tab and add these scripts to begin and end your game ..

 

 

6. Use "File>Save as" to name and save your game. Name it "Snooker" ..

 

 

7. You need to import the sprites for your game.

Download and save the following sprites ..

 

Now use the "Choose new sprite from file" button under the stage to import them ..

 

 

8. Select the "Instructions" sprite. This sprite will show for 8 seconds at the start of the game. Add this script ..

Press the "B" key on your keyboard to begin your game and you will see the Instructions show for 8 seconds before hiding again.

Press the "E" key to end your game and continue working on it.

 

 

9. Select the Cue sprite. We need to add scripts to allow the player to position it around the cue ball and use it to move the cue ball.

Add the first script to set its size and initial direction ..

 

Now add the script to allow the player to move and angle it ..

 

Press the "B" key on your keyboard to begin your game and then move the mouse-pointer and try the left and right arrow keys on your keyboard.

Notice how the Cue is a little distance away from the mouse-pointer. This is because its costume center point has been set away from the end of it. This will make the cueing animation work. Let's add it now.

Press the "E" key to end your game and continue working on it, and then create the following "if mouse down?" animation script on your Cue sprite ..

This script will make the cue animate when the mouse is clicked.

Drag it into the forever script under the "if key left arrow pressed?" script ..

 

Press the "B" key on your keyboard to begin your game and then move the mouse-pointer, try the left and right arrow keys on your keyboard, and click the mouse.

Notice how the Cue animates like a real Cue hitting a ball.

Press the "E" key to end your game and continue working on it.

 

 

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

 

 

11. Let's add a simple target sprite so we can see exactly where the Cue will strike.

Select the "Cue target" sprite and add this simple script ..

 

Now re-test your game.

 

 

12. OK, time for the complex scripts!

Select the Cue ball sprite. Here's what this sprite must do ..

  1. move when hit by the Cue
  2. lose speed slowly over time
  3. transfer it's energy to any coloured ball it hits
  4. disappear temporarily when it accidentally goes into a pocket
  5. lose extra speed when it hits any coloured ball
  6. bounce off the cushions (stage edge)

Add the first script to set its starting position ..

 

Notice how the Cue sprite has a blue pointed end. You will need to sample this colour as you create the next script.

Also notice we are using the "hit ball" broadcast sent from the Cue sprite. Go and find where this broadcast is in the Cue sprites scripts.

Re-select the Cue ball and add the following script to control its direction, speed and deceleration ..

 

Press the "B" key on your keyboard to begin your game and test the Cue and Cue ball. If it doesn't work as expected, refer to the image above to find the error and correct it.

Press the "E" key to end your game and continue working on it.

 

 

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

 

 

14. We have one final script to add to the Cue ball sprite. This script makes it disappear temporarily when it accidentally goes into a pocket. You will need to sample the colour of one of the brown pockets on the stage background image (Table). Add this script ..

Notice how the player loses a point. The Cue ball is not allowed into a pocket in snooker.

 

 

15. Now select the "foul shot" sprite and add these scripts ..

 

Press the "B" key on your keyboard to begin your game and deliberately send the Cue ball into a pocket to test the scripts.

Press the "E" key to end your game and continue working on it.

 

 

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

 

 

17. Time to script the coloured balls. Select the Red ball sprite and add the script that sets its initial properties ..

 

Now add the script that controls what happens to the Red ball when it is hit by the Cue ball ..

You should be able to read through this script and understand what it does. It's nice and straightforward. Go ahead and test it now.

 

 

18. You now have the Red ball moving when it is hit by the Cue ball, but what about if it is hit by the Yellow or Blue ball? You need two more "if touching ? ball?" scripts to make the Red ball react when hit by the Yellow or Blue balls.

Duplicate the "if touching Cue ball?" scripts twice and stack them above one another. Then edit the duplicates ..

We can't completely test this script until we have scripted the Yellow and Blue balls.

 

 

19. We need to add the script for when the Red is potted. Add this script ..

Notice how the player gains a point and "ball potted" is broadcast. The "Great shot" sprite responds to this broadcast. Select it now and add these scripts ..

Test your game again and try and pot the Red ball. You should see "Great shot" flash on the stage when you do.

 

 

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

 

 

21. The Yellow ball and Blue ball sprites have very similar scripts to the Red ball. Duplicate the Red ball scripts to these balls and carefully edit them to these ..

 

 

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

Well done, you've finished this tutorial. Play the game!