GameMaker Lesson

Breakout

Bricks, Collision Detection, and Object Destruction

Project Name

Your GameMaker project must be named:

PX_lastname_Breakout

Replace lastname with your own last name.

Example:

PX_Smith_Breakout

Learning Objectives

By the end of this lesson, you should be able to:

  1. Create sprites and objects in GameMaker.
  2. Move a paddle using keyboard input.
  3. Make a ball move automatically.
  4. Make a ball bounce from walls and the paddle.
  5. Create multiple brick objects.
  6. Detect collisions between objects.
  7. Destroy a brick when the ball hits it.
  8. Keep track of the player's score.
  9. Detect when all bricks have been destroyed.
  10. Test and debug a GameMaker project.

The Goal

You will create a simplified version of the classic Breakout arcade game.

The player controls a paddle at the bottom of the screen.

A ball moves around the room and bounces off:

When the ball hits a brick, the brick will be destroyed. The player receives points for each brick destroyed.

The goal is to destroy all the bricks.

Part 1 - Create the Project

  1. Open GameMaker.
  2. Create a new GameMaker project.
  3. Save the project using the required filename.
PX_lastname_Breakout
Checkpoint 1:
Show your teacher that your project has been created and named correctly.

Part 2 - Create the Sprites

Create the following sprites:

Sprite Purpose
spr_player Paddle controlled by the player
spr_ball The moving ball
spr_brick Brick that will be destroyed
spr_wall Walls around the playing area

Suggested Sprite Sizes

For the ball sprite, set the origin to Middle Center.

Part 3 - Create the Objects

Object Sprite
obj_player spr_player
obj_ball spr_ball
obj_brick spr_brick
obj_wall spr_wall
obj_controller No sprite

Part 4 - Create the Room

Create a room called:

rm_game

Suggested room size:

Place the paddle near the bottom of the room.

Place the ball slightly above the paddle.

Place several rows of bricks near the top.

[BRICK] [BRICK] [BRICK] [BRICK] [BRICK]

[BRICK] [BRICK] [BRICK] [BRICK] [BRICK]

[BRICK] [BRICK] [BRICK] [BRICK] [BRICK]


                 BALL


                PADDLE

Your game must contain at least 15 bricks.

Part 5 - Paddle Movement

Select:

obj_player

Add a Step Event.

Enter:

var move_speed = 8;

if (keyboard_check(vk_left))
{
    x -= move_speed;
}

if (keyboard_check(vk_right))
{
    x += move_speed;
}

Optional A and D Controls

var move_speed = 8;

if (keyboard_check(vk_left) || keyboard_check(ord("A")))
{
    x -= move_speed;
}

if (keyboard_check(vk_right) || keyboard_check(ord("D")))
{
    x += move_speed;
}

Understanding the Paddle Code

keyboard_check() determines whether a keyboard key is currently being pressed.

This statement moves the paddle left:

x -= move_speed;

This statement moves the paddle right:

x += move_speed;

Part 6 - Keep the Paddle on the Screen

Add this underneath your movement code:

x = clamp(x, sprite_width / 2, room_width - sprite_width / 2);

The clamp() function prevents the paddle from moving outside the room.

Checkpoint 2:
Run the game and demonstrate that the paddle moves left and right but cannot leave the room.

Part 7 - Create the Ball Movement

Select:

obj_ball

Add a Create Event.

speed = 6;
direction = choose(45, 135);

The choose() function randomly selects one of the supplied values.

Understanding Direction

          90
           ↑

180  ←           →  0

           ↓
          270

Part 8 - Ball and Paddle Collision

Select:

obj_ball

Add a Collision Event with:

obj_player

Enter:

vspeed = -abs(vspeed);

This forces the ball to begin moving upward when it hits the paddle.

Improved Paddle Collision

You can make the ball change direction depending on where it hits the paddle.

var difference = x - other.x;

hspeed = difference / 8;
vspeed = -abs(vspeed);

Hitting the left side of the paddle sends the ball farther left. Hitting the right side sends it farther right.

Part 9 - Create the Bricks

Place several instances of obj_brick into the room.

Example:

XXXXXXXXXX
XXXXXXXXXX
XXXXXXXXXX

Each X represents one brick.

Minimum requirement:

3 rows × 5 bricks = 15 bricks

For an additional challenge:

5 rows × 8 bricks = 40 bricks

Part 10 - Ball and Brick Collision

Select:

obj_ball

Add a Collision Event with:

obj_brick

Enter:

vspeed = -vspeed;

instance_destroy(other);

One of the most important commands in this project is:

instance_destroy(other);

This command destroys the brick that the ball collided with.

Understanding instance_destroy()

GameMaker uses:

instance_destroy();

to remove an instance from the game.

Inside the ball-and-brick collision:

instance_destroy(other);

The word other refers to the other object involved in the collision.

In this case, the other object is the brick.

Part 11 - Add a Score

Create:

obj_controller

The controller object does not need a sprite.

Place one instance of obj_controller in the room.

Add a Create Event:

score = 0;

Add Points for Destroying Bricks

Return to the collision between the ball and the brick.

vspeed = -vspeed;

score += 10;

instance_destroy(other);

Every destroyed brick is worth 10 points.

Part 12 - Display the Score

Select:

obj_controller

Add a Draw GUI Event.

draw_set_halign(fa_left);
draw_set_valign(fa_top);

draw_text(20, 20, "Score: " + string(score));

The player's score should now appear near the upper-left corner of the game screen.

Part 13 - Ball and Wall Collision

Place obj_wall objects around:

Do not place a wall across the bottom if you want the player to be able to lose the ball.

Set obj_wall as a Solid object.

In obj_ball, create a Collision Event with obj_wall.

move_bounce_solid(false);

Part 14 - Losing the Ball

Select obj_ball and add a Step Event.

if (y > room_height + 50)
{
    room_restart();
}

If the ball travels below the room, the game will restart.

Optional Challenge - Add Lives

Instead of immediately restarting the game, create a lives system.

Inside the controller Create Event:

score = 0;
lives = 3;

When the player loses the ball:

lives -= 1;

Check for Game Over:

if (lives <= 0)
{
    room_restart();
}

Part 15 - Detect When All Bricks Are Destroyed

In the Step Event of obj_controller, enter:

if (instance_number(obj_brick) == 0)
{
    show_message("YOU WIN!");
    room_restart();
}

The function:

instance_number(obj_brick)

counts how many brick instances still exist in the room.

When there are zero bricks remaining, the player wins.

Part 16 - Test Your Game

Your finished game should allow the player to:

Debugging Checklist

Paddle Does Not Move

Make sure the keyboard code is inside the Step Event.

keyboard_check()

Ball Does Not Move

Check the ball's Create Event.

speed = 6;

Bricks Do Not Disappear

Check the Collision Event between obj_ball and obj_brick.

instance_destroy(other);

Ball Passes Through Bricks

Check:

Score Does Not Increase

Verify that your brick collision contains:

score += 10;

Required Features

  1. Correct project filename.
  2. Player-controlled paddle.
  3. Moving ball.
  4. At least 15 bricks.
  5. Ball-to-paddle collision.
  6. Ball-to-wall collision.
  7. Ball-to-brick collision.
  8. Brick destruction.
  9. Score system.
  10. Score displayed on the screen.
  11. Ball-loss system.
  12. Win condition.

Challenge 1 - Different Brick Colors

Create several types of bricks:

Challenge 2 - Strong Bricks

Create bricks that require two hits before being destroyed.

Brick Create Event:

health = 2;

Ball collision:

other.health -= 1;

if (other.health <= 0)
{
    instance_destroy(other);
}

Challenge 3 - Different Point Values

Brick Points
Red Brick 10
Blue Brick 20
Green Brick 30
Gold Brick 50

Challenge 4 - Power-Ups

Add power-ups that sometimes appear when a brick is destroyed.

Challenge 5 - Sound Effects

Add sound effects when:

Challenge 6 - Multiple Levels

Create several rooms:

rm_level1
rm_level2
rm_level3

Each level should become more difficult.

Vocabulary

Term Definition
Sprite The graphic representing an object.
Object A GameMaker resource containing game behavior.
Instance A specific copy of an object placed in the game.
Collision When two objects touch.
Event Something that causes code to execute.
Step Event Code that repeatedly executes while the game runs.
Create Event Code that executes when an object instance is created.
instance_destroy() Removes an object instance from the game.
instance_number() Counts how many instances of an object currently exist.
keyboard_check() Checks whether a keyboard key is being pressed.
speed Controls how fast an object moves.
direction Controls the direction an object moves.
hspeed Horizontal speed.
vspeed Vertical speed.
score Stores the player's score.
clamp() Restricts a value to a specified range.

Review Questions

  1. What is the purpose of instance_destroy()?
  2. What does other mean inside a Collision Event?
  3. What event should normally be used for continuous keyboard movement?
  4. What variable controls horizontal movement?
  5. What variable controls vertical movement?
  6. What does instance_number(obj_brick) return?
  7. Why do we check whether the number of bricks equals zero?
  8. What happens when the ball's vspeed changes from positive to negative?
  9. What is the difference between an object and an instance?
  10. Why is collision detection important in a Breakout game?

100-Point Grading Rubric

Requirement Points
Correct project filename PX_lastname_Breakout 5
Room properly designed 5
Paddle sprite and object 5
Paddle moves correctly 10
Paddle stays inside room 5
Ball sprite and object 5
Ball moves automatically 5
Ball bounces from paddle 10
Wall collision works 5
At least 15 bricks 10
Ball detects brick collisions 10
Bricks are destroyed correctly 10
Score increases 5
Score displayed 5
Win or ball-loss condition 5
TOTAL 100

Final Checkpoint

Before submitting your assignment, demonstrate the game to your teacher.

Your teacher should see:

Submission

Submit your GameMaker project according to your teacher's instructions.

Your project must be named:

PX_lastname_Breakout

Before Submitting

Final Goal

Create a working Breakout game that demonstrates:

Objects + Movement + Collision Detection + Brick Destruction + Scoring