← Yop2D

How to make a brick breaker game?

Beginner guide · no code

Français · English · Español · Deutsch · Italiano · Português · Русский · 中文 · 日本語

In a brick breaker, a ball bounces off walls and off a paddle you move with your finger, and breaks bricks. Here the ball moves thanks to two variables, vx (its speed to the right) and vy (its speed downward). When it touches a wall, we simply give it another number.

You need:
👉 This guide is written for a 1080 x 1920 portrait game. The images come from the free Puzzle (brick breaker) pack (Kenney, free to use, CC0). Download it in the Yop2D Library, tab Packs to download: you will find paddleBlu (paddle), ballBlue (ball) and element_blue_rectangle_glossy (brick, in blue, green, red, yellow, purple and grey). You can also use your own drawings.

Step 1: tags and variables

  1. Select a brick. In the inspector, field Tag (Label), type brick. Do the same for every brick.
  2. In the left panel, open the Variables section and create five variables, all with scope Local and type Integer:
👉 One brick more or less? Just change the starting value of bricks. If the number is too big, you never win; if it is too small, you win too early.

Step 2: the invisible walls

The ball bounces off walls you cannot see: four Zones placed around the screen. A zone is invisible during the game, but the ball can touch it.

  1. Add a Zone named left_wall: X = -300, Y = -400, width 300, height 2800.
  2. Add a Zone named right_wall: X = 1080, Y = -400, width 300, height 2800.
  3. Add a Zone named top_wall: X = -300, Y = -300, width 1680, height 300.
  4. Add a Zone named bottom_wall: X = -300, Y = 1920, width 1680, height 300.
👉 The walls are thick on purpose: a fast ball cannot go through them. The first three send the ball back; bottom_wall is used to lose a life.

Step 3: the paddle follows your finger

  1. Select paddle, touch ⚙ Comportement de l'objet and choose Dragged with a finger: the player takes it and moves it.
  2. Open the scene's Blueprint and add the “Every frame” event.
  3. Add “Set a property”: target paddle, Property = y, Value = 1700.
  4. Add “Clamp to screen”: target paddle, Margin = 0.
  5. Link the three nodes in order.
Every frame
→
Set a propertypaddle · y · 1700
→
Clamp to screenpaddle · 0
👉 Why these two nodes? “Dragged with a finger” lets the paddle move in every direction. On every frame, we put it back at the same height and keep it from leaving the screen: it only moves left and right.

Step 4: the ball starts

  1. Add the “On Start” event, then two “Modify Variable” nodes one after the other: variable vx, value 6, then variable vy, value -9.
  2. Add the “Every frame” event, then “Push object”: target ball.
  3. In the settings window of “Push object”, touch the 🔗 button next to Add X, choose “Variable value”, then choose the variable vx in the green node that appears.
  4. Do the same for Add Y with the variable vy.
  5. Link each event to its nodes and run the game: the ball goes up and to the right, then leaves the screen.
On Start
→
Modify Variablevx · 6
→
Modify Variablevy · -9
Every frame
→
Push objectball · X ← vx · Y ← vy
👉 A green node gives a value to a setting, without typing anything. Here, “Variable value” tells “Push object”: “move by vx to the right and by vy downward, on every frame”.
👉 Negative = up. In Yop2D, Y grows toward the bottom of the screen: vy = -9 sends the ball upward.

Step 5: bounce on the walls

Each wall gives the ball a new number. Add three “Collision A/B” events (Object A = ball):

  1. Object B = left_wall → “Modify Variable”: variable vx, value 6 (the ball goes back to the right).
  2. Object B = right_wall → “Modify Variable”: variable vx, value -6 (the ball goes back to the left).
  3. Object B = top_wall → “Modify Variable”: variable vy, value 9 (the ball goes back down).
Collision A/Bball · left_wall
→
Modify Variablevx · 6
Collision A/Bball · right_wall
→
Modify Variablevx · -6
Collision A/Bball · top_wall
→
Modify Variablevy · 9

Step 6: bounce on the paddle

  1. Add the “Collision A/B” event: Object A = ball, Object B = paddle.
  2. Add “Modify Variable”: variable vy, value -9 (the ball goes back up).
  3. Add a second “Modify Variable”: variable vx. Touch 🔗 next to the value, choose “Random number”, then set From = -6 and To = 6.
  4. Link the nodes in order.
Collision A/Bball · paddle
→
Modify Variablevy · -9
→
Modify Variablevx · ← random number (-6 to 6)
👉 On every bounce on the paddle, the ball leaves in a random direction: the game is never the same twice.

Step 7: break the bricks

  1. Add the “If object touches Tag” event: target ball, Tag brick.
  2. Add “Destroy Object” and choose The object involved in the event as the target: it is the brick the ball has just touched.
  3. Add “Condition” =vy < 0 (“the ball is going up”). Output True → “Modify Variable”: variable vy, value 9. Output False → “Modify Variable”: variable vy, value -9.
  4. From the “Then” output of the condition, add “Add to Variable”: variable score, Value to add = 10.
  5. Add “Add to Variable”: variable bricks, Value to add = -1.
  6. Add “Modify Text”: target scoreText, New text = ="Score: " + score.
If object touches Tagball · brick
→
Destroy ObjectImplied Object
→
Condition=vy < 0
Condition=vy < 0
True →
Modify Variablevy · 9
Condition=vy < 0
False →
Modify Variablevy · -9
Condition=vy < 0
Then →
Add to Variablescore · + 10
→
Add to Variablebricks · - 1
→
Modify TextscoreText
👉 Why =vy < 0? If vy is smaller than 0, the ball is going up: after breaking the brick, it goes down. Otherwise it is going down, and it goes back up.
👉 The = sign at the beginning of the text tells Yop2D to calculate the text instead of writing it as it is (see the guide Collect coins and count points).

Step 8: win

After the “Modify Text” of step 7, add:

  1. “Condition” =bricks <= 0.
  2. On the True output: “Modify Variable” vx = 0, “Modify Variable” vy = 0 (the ball stops), then “Small message at the bottom of the screen” with the message You win!, then “Wait” 2 seconds, then “Restart the scene”.
Modify TextscoreText
→
Condition=bricks <= 0
True →
Modify Variablevx · 0
→
Modify Variablevy · 0
→
Small message at the bottom of the screenYou win!
→
Wait2
→
Restart the scene
👉 “Restart the scene” puts the objects back in place and the Local variables back to their starting value: the game restarts with 3 lives, 0 points and all the bricks.

Step 9: lives and Game Over

When the ball touches bottom_wall (under the screen), the player loses a life.

  1. Add the “Collision A/B” event: Object A = ball, Object B = bottom_wall.
  2. “Add to Variable”: variable lives, Value to add = -1.
  3. “Modify Text”: target livesText, New text = ="Lives: " + lives.
  4. “Condition” =lives <= 0.
  5. Output True (no lives left): “Move Object” the ball to X = 518, Y = 1000, “Modify Variable” vx = 0, “Modify Variable” vy = 0 (the ball is put back in the middle and stops), then “Small message at the bottom of the screen” “Game over!”, “Wait” 2 seconds, “Restart the scene”.
  6. Output False (lives left): “Move Object” the ball to X = 518, Y = 1000, then “Modify Variable” vy = -9, then “Modify Variable” vx = 6.
Collision A/Bball · bottom_wall
→
Add to Variablelives · - 1
→
Modify TextlivesText
→
Condition=lives <= 0
Condition=lives <= 0
True →
Move Objectball · 518 · 1000
→
Modify Variablevx · 0
→
Modify Variablevy · 0
→
Small message at the bottom of the screenGame over!
→
Wait2
→
Restart the scene
Condition=lives <= 0
False →
Move Objectball · 518 · 1000
→
Modify Variablevy · -9
→
Modify Variablevx · 6
👉 Why stop the ball before waiting? During the 2 seconds of waiting, the game keeps running. If the ball stayed below the screen, it would cost a life on every frame, and the lives counter would drop very low. Put back in the middle and motionless, it no longer causes trouble.

Going further

If it doesn't work