1 of 13

Ping Pong Game (Multiplayer game)

Scratch

Abhinav (WiSE program)

2 of 13

In this Game……

We will make a multiplayer game (ping pong)

One player has the controls w and s to control the paddle

The other player has up arrow and down arrow to control the paddle

Concepts used – The concept of direction

3 of 13

Background

  • We make a backdrop with two different colours on either side. We use these two different colours for the scoring of the game

4 of 13

Sprites (Two Paddles)

  • We will make two paddle sprites and then give the code to move them

5 of 13

Code for player 1 to move the paddle

  • We first set the ‘x’ to the right corner and then set y to anywhere on the screen.
  • Then we put a forever loop and inside the forever loop we say ‘if up arrow key pressed’ then move the paddle upwards by 10 steps
  • We then say ‘if down arrow key pressed’ then move the paddle downwards by 10 steps
  • This code helps us to give the direction of the paddle

6 of 13

Code for Player 2 to move the paddle

  • We first set the ‘x’ to the left corner and then set y to anywhere on the screen.
  • Then we put a forever loop and inside the forever loop we say ‘if w key pressed’ then move the paddle upwards by 10 steps
  • We then say ‘if s key pressed’ then move the paddle downwards by 10 steps
  • This code helps us to give the direction of the paddle

7 of 13

Giving the code to the Ball to start moving

  • We first set the ball to the centre of the screen. We then tell the ball to take a random direction by giving a pick random. After it has taken a direction we will ask it to start moving and if it is on the edge we will ask it to bounce back

8 of 13

Initial Direction of the Ball

This is the initial direction of the Ball

180 degrees is facing downward

This is 250 (180 + 70). So the ball can start moving anywhere between this area

This is 110 (180 - 70). So the ball can start moving anywhere between this area

9 of 13

Code for Ball to bounce off the paddle

  • We will say to the Ball sprite, If touching right of left paddle point in direction 180 + whatever direction it was. We add an extra 10 to it so that the direction doesn’t be constant (eg. 10, 190, 10, 190) and changes everytime for the ball

10 of 13

Some examples

Lets say direction was 120. This means it goes in the direction of 120 and when it touches paddle it will reflect back on direction 310 = -50 (120 + 180 + 10). This will keep continuing so next direction will be 140 (180 + (-50) + 10) and so on……

Lets say direction was 210 (-150). This means it goes in the direction of -150 and when it touches the paddle it will reflect back on direction 40 (-150 + 180 – 10). This keeps on continuing and the next direction will be 230 (40 + 180 + 10) and so on......

11 of 13

Giving score to Player 1

  • We first create a variable ‘Player 1 score’ We say that if it is touching the colour opposite to it or behind player 2’s sprite it will give points. We use a wait to prevent it from giving extra points

12 of 13

Giving score to player 2

  • We first create a variable ‘Player 2 score’ We say that if it is touching the colour opposite to it or behind player 1’s sprite it will give points. We use a wait to prevent it from giving extra points

13 of 13

Link for the Ping Pong Game