Scratch game making tutorial 142

Scratch cat and code blocks video screenshot Scratch coding video lessons

Our Scratch coding online video lessons are an ideal way for your child to experience our lessons at home. Try a free lesson.



Snooker

In this tutorial you will create a snooker game.

 

NOTE: You can use any version of Scratch.

 

Complete these steps ..

 

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

 

 

2. Download, save and then open this game file ..

 

 

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. Select the stage Code/Scripts tab and add this code/scripts to begin and end your game ..

 

 

5. Use the File menu to save your game. Name it "Snooker v1" ..

 

 

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

Add this code/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.

 

 

7. Select the Cue sprite. We need to add code/script to allow the player to position it around the cue ball and use it to move the cue ball.

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

 

Now add the code/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 the code/script now.

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

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

Drag it into the forever code/script under the "if key left arrow pressed?" code/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.

 

 

8. Use the File menu to save your game.

 

 

9. 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 code/script ..

 

Now re-test your game.

 

 

10. OK, time for the complex code/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 code/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 code/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 code/script.

Re-select the Cue ball and add the following code/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.

 

 

11. Use the File menu to save your game.

 

 

12. We have one final code/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 code/script ..

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

 

 

13. Now select the "foul shot" sprite and add this code/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.

 

 

14. Use the File menu to save your game.

 

 

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

 

Now add the code/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 code/script and understand what it does. It's nice and straightforward. Go ahead and test it now.

 

 

16. 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?" code/script to make the Red ball react when hit by the Yellow or Blue balls.

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

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

 

 

17. We need to add the code/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 this code/scripts ..

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

 

 

18. Use the File menu to save your game.

 

 

19. The Yellow ball and Blue ball sprites have very similar code/script to the Red ball. Duplicate the Red ball code/script to these balls and carefully edit them to these ..

 

 

20. Use the File menu to save your game.

 

 

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