In a Pong game, a ball bounces between your paddle (at the bottom) and the paddle of a robot (at the top). When the ball goes behind a paddle, the other player scores. As in the brick breaker guide, the ball moves thanks to two variables: vx (its speed sideways) and vy (its speed up or down).
You need:
- an Image named Ball, in the middle of the screen (for example 60 x 60 px);
- an Image named Player, near the bottom (for example 200 x 80 px, around y = 1690);
- an Image named Bot, near the top (same size, y = 140);
- two Text objects named ScorePlayer and ScoreBot, with the text 0;
- four Zones named WallLeft, WallRight, GoalPlayer and GoalBot.
👉 This guide is written for a portrait game of 1080 x 1920. For the pictures, take the free Puzzle (brick breaker) pack (Kenney, CC0) in the Library, Packs to download tab: paddleBlu (your paddle), paddleRed (the robot), ballBlue (the ball). You can also use your own drawings.
Step 1: names, behaviors and variables
- Select each object and write its name in the Inspector, exactly as in the list above. A name with a space or a different letter will not be found by the nodes.
- Select Ball, touch ⚙ Object behavior and choose Scenery. Do the same for Player and Bot: only the nodes will move them.
- In the Variables section, create two variables of type Number, scope Local: vx and vy, start value 0. Create two more of type Integer, scope Local: scorePlayer and scoreBot, start value 0.
👉 The most common mistake: a forgotten name or a forgotten behavior. Nothing breaks, but nothing happens. If your game does nothing, check this step first.
Step 2: walls and goals
The ball bounces on walls that you cannot see: four Zones around the screen. A zone is invisible during the game, but the ball can touch it.
- Add a Zone named WallLeft: X =
-300, Y =-400, width300, height2800. - Add a Zone named WallRight: X =
1080, Y =-400, width300, height2800. - Add a Zone named GoalBot: X =
-300, Y =-300, width1680, height300(above the screen). - Add a Zone named GoalPlayer: X =
-300, Y =1920, width1680, height300(below the screen).
👉 The walls are thick on purpose: a fast ball cannot pass through them. The two walls send the ball back; the two goals will count the points.
Step 3: the ball starts and moves
- Add the event “On Start”, then two “Modify Variable” nodes in a row: variable vx, value
6, then variable vy, value9. - Add the event “Every frame”, then “Push object”: target Ball, Add X =
vx, Add Y =vy. Just write the two names in the fields.
On Start
→
Modify Variablevx · 6
→
Modify Variablevy · 9
Every frame
→
Push objectBall · vx · vy
👉 How does it move? “Every frame” runs about 60 times per second. Each time, the ball moves by vx pixels sideways and vy pixels up or down. With 0 and 0, it stops. To change its direction, we only change these two numbers: that is what the bounces will do.
Step 4: my paddle follows my finger
- Add the event “Finger held down”: it repeats while your finger is on the screen.
- Add “Move Object”: target Player, X =
doigt.x - Player.largeur / 2, Y =1690. The finger position is in the list on the left of the settings window; we remove half of the paddle width so that the finger is at its centre. - Add “Clamp to screen”: target Player, Margin =
0, so that the paddle cannot leave through the sides.
Finger held down
→
Move ObjectPlayer · doigt.x · 1690
→
Clamp to screenPlayer · 0
Step 5: the bounces
A bounce is just a new number for vx or vy. We use random(a, b), which picks a number between a and b: no bounce is the same.
- “Collision A/B” (Object A = WallLeft, Object B = Ball) → “Modify Variable”: vx =
random(4, 12). A positive number: the ball goes right. - “Collision A/B” (WallRight, Ball) → “Modify Variable”: vx =
-random(4, 12). The minus sign makes it negative: the ball goes left. - “Collision A/B” (Ball, Player) → “Modify Variable”: vy =
random(-13, -9)(the ball goes up), then “Modify Variable”: vx =Ball.x / 10 - Player.x / 10 - 7. - “Collision A/B” (Ball, Bot) → “Modify Variable”: vy =
random(9, 13)(the ball comes down), then “Modify Variable”: vx =random(-6, 6).
Collision A/BWallLeft · Ball
→
Modify Variablevx · random(4, 12)
Collision A/BWallRight · Ball
→
Modify Variablevx · -random(4, 12)
Collision A/BBall · Player
→
Modify Variablevy · random(-13, -9)
→
Modify Variablevx · Ball.x / 10 - Player.x / 10 - 7
Collision A/BBall · Bot
→
Modify Variablevy · random(9, 13)
→
Modify Variablevx · random(-6, 6)
👉 Why this calculation for the paddle? It compares the position of the ball with the position of the paddle: in the centre, the ball goes straight; near an edge, it goes sideways. The
-7 compensates for the paddle being wider than the ball (so the centre gives 0).Step 6: the robot chases the ball
The robot is not clever: on every frame, it moves a little towards the ball.
- Add the event “Every frame”, then “Set a property”: target Bot, Property =
x, Value =Bot.x * 0.94 + Ball.x * 0.06 - 6. - Add “Clamp to screen”: target Bot, Margin =
0.
Every frame
→
Set a propertyBot · x
→
Clamp to screenBot · 0
👉 How does it work? The robot keeps 94 % of its own place and takes 6 % of the place of the ball (the
- 6 centres it on the ball). With 0.06, it is slow enough to be beaten. Try 0.03 and 0.97 (very easy) or 0.15 and 0.85 (very hard): the two numbers must add up to 1, and the - 6 becomes - 3 or - 15.👉 Careful: do not write a position in "speed X". A speed is a small number, a position is a big one: the robot would not move.
Step 7: goals and scores
- “Collision A/B” (GoalBot, Ball) → “Add to Variable”: variable scorePlayer, Value to add = 1 → “Modify Text”: target ScorePlayer, New text =
=scorePlayer. - Same for the robot: “Collision A/B” (GoalPlayer, Ball) → “Add to Variable”: scoreBot + 1 → “Modify Text”: target ScoreBot,
=scoreBot. - To serve again, add in a row: “Modify Variable” vx =
0, “Modify Variable” vy =0, “Move Object” Ball to X =510, Y =930, “Wait” 1 second, then “Modify Variable” vx =6and “Modify Variable” vy =9. - Link the end of the two goals to the first node of this serve: an input can receive several wires.
Collision A/BGoalBot · Ball
→
Add to VariablescorePlayer · + 1
→
Modify TextScorePlayer
Collision A/BGoalPlayer · Ball
→
Add to VariablescoreBot · + 1
→
Modify TextScoreBot
Modify Variablevx · 0
→
Modify Variablevy · 0
→
Move ObjectBall · 510 · 930
→
Wait1
→
Modify Variablevx · 6
→
Modify Variablevy · 9
👉 The
= in front of the text tells Yop2D to calculate the value instead of writing it as it is (see the guide Collecting coins and counting points).Go further
- Win at 5 points: after each goal, add a “Condition”
=scorePlayer >= 5, then a “Small message at the bottom of the screen” and “Restart the scene” (see the guide Lives and Game Over). - Sounds: add “Play sound” after each bounce.
- Your own look: change the pictures and the background; the names stay the same.
- The game does nothing? Go back to step 1: a name or a behavior is missing. The robot does not move? Check that its behavior is Scenery and that the formula is in Value.