Scratch - Ping pong

Tools

Computer with Scratch 2.0 or mBlock

Materials

Ping-pong rules 

Files

Step  1 : Add the scenario

  • Change the background  choosing the image from the gallery

Step  2 : Create small characters : 2 rackets and 1 ball

First racket : 

  • To create a new small character, you must click on the tool "Draw a new item" (number 1) as mentioned in the illustration
  • Draw a vertical rectangle (number 2)
  • Choose your color in the palette (number 4)
  • Color your rectangle with the tool "color a form" (number 3)
  • Move your small character to the middle of the window with your mouse
  • Change the name of the costume to facilitate the program (number 5)

Second racket : 

  • You don't need to create a new item 
  • Right click on the first one and chose "Duplicate" (number 6)
  • Chose a different color from the first one
  • Color your racket (number 7)
  • Change the name of the costume to facilitate the program (number 9)

Ball :

  • Click again on "Draw a new item" (number 10)
  • Draw a small circle in the middle of the window (number 11)
  • Chose a color different from the 2 rackets  (number 13)
  • Color your ball (number 12)
  • Change the name of the costume to facilitate the program (number 14)

Step  3 : Code the first player's racket

  • Activate the first player's racket (yellow)
  • Start to code with the block "when the green flag is clicked" 
  • Find, thanks to your mouse, the coordinates where the racket has to go at each program beginning and write it in the block "Go to x: .. y: .." in the movement category. 
  • Condition is "if...so" is used here to launch the racket movements when you push on a button (to go up or down)
  • Define, from the block "touche... pressé?" in the sensor, the name of the buttons you'll have to push to move the racket up or down (here it's a to go up, and w to go down)
  • Block "Add ...y" defines the number of racket movements (5 to go up, and -5 to go down)
  • Then, block "Repeat indefinitely" will permit to pursue the movements each time you will push on the buttons.

Step  4 : Code the second player's racket

It is more or less the same codes as the first player. 

  • Activate the second player's racket (red)
  • Start to code with the block "when the green flag is clicked"
  • Find, thanks to your mouse, the coordinates where the racket has to go at each program beginning and write it in the block "Go to x: .. y: .." in the movement category. 
  • Condition is "if...so" is used here to launch the racket movements when you push on a button (to go up or down)
  • Define, from the block "touche... pressé?" in the sensor, the name of the buttons you'll have to push to move the racket up or down (up arrow to go up and down arrow to go down)
  • Block "Add ...y" defines the number of racket movements (5to go up, and -5 to go down)
  • Then, block "Repeat indefinitely" will permit to pursue the movements each time you will push on the buttons

Step  5 : Code the ball movements

  • Click on the ball to activate it
  • Start the script with the block "when the green flag is clicked" 
  • Find, thanks to your mouse, the coordinates where the ball has to be at each beginning of the game (normally, at the middle of the scene), and write it in the block "Go to w: .. y: .." in the movement category
  • Use the block " s'orienter à ...degrés" in the movement category to define the ball direction at each game beginning
  • Let the computer chose the value of the angle orientation (between 1 and 360°) by choosing the block "Random number between ..... and .... degrees" which is located in "operator" category. 

Step  6 : Move automatically the ball

We started the ball scripts by its orientation at each program beginning. Here, we will define its speed and movements on the scene. 

Pursue the scripts that we have yet started in the last step with these new instructions : 

  • Repeat indefinitely the action "moving froward ... " the ball (here we chose its speed to 3)
  • If the scene edge is reached, bounce the ball

Step  7 : When the ball has touched the racket

We are still in the ball code. We will pursue when the ball has touched one of the racket (here we used the terms  baton_1 and baton_2 for the rackets)

  • Condition "If ... so" is used to define the orientation angle after the contact between the ball and the racket which is 360° minus its direction. 
  • Find the block 360°... in the operator category, and direction in movement category. 
  • Find baton_1/baton_2 touched? in the sensor category
  • Go back in Movement to take the block "s'orienter à .. degrés"
  • All these scripts have to be integrated into the "Repeat indefinitely" loop that we already mentioned in the steps before

Step  8 : Modify the background

To modify the background, we will draw 2 horizontal lines of different colors on each side of the background, as mentioned in the illustration. 

These 2 lines will allow us to know who will be the winner and loser. We will see that in the next step. 

In other words, the player who manages to touch the line behind his opponent wins the game.

We will proceed as follows : 

  • Click on the scene next to the small characters (number 1)
  • Click then on "backgrounds" tab next to "instructions" (number 2)
  • Draw now the 2 different color lines on the background (numbers 3 and 4)

Step  9 : Finish the ball code

Background is modifies, so let's continue and finish our ball code.  Don't change the scripts, we will pursue the one we already began. 

  • Click on the ball to activate it
  • Go back to the instructions tab
  • Put a condition "if ... so" to define if the ball touches the "black" color (use the block color touched?), the second player wins. 
  • However, if the ball touches the "violet" color (use the block color touched?), the first player wins. 
  • Find the block "color touched?" in the sensor category
  • Don't forget to add conversations bubbles in the appearance category to display the winner 
  • Stop the program by using the block "stop all" at the end of the game
  • All these instructions should be in the "repeat indefinitely" that we already used