Bricks, Collision Detection, and Object Destruction
Your GameMaker project must be named:
PX_lastname_Breakout
Replace lastname with your own last name.
Example:
PX_Smith_Breakout
By the end of this lesson, you should be able to:
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.
PX_lastname_Breakout
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 |
For the ball sprite, set the origin to Middle Center.
| Object | Sprite |
|---|---|
| obj_player | spr_player |
| obj_ball | spr_ball |
| obj_brick | spr_brick |
| obj_wall | spr_wall |
| obj_controller | No sprite |
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.
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;
}
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;
}
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;
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.
Select:
obj_ball
Add a Create Event.
speed = 6;
direction = choose(45, 135);
The choose() function randomly selects one of
the supplied values.
90
↑
180 ← → 0
↓
270
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.
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.
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
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.
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.
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;
Return to the collision between the ball and the brick.
vspeed = -vspeed;
score += 10;
instance_destroy(other);
Every destroyed brick is worth 10 points.
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.
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);
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.
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();
}
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.
Your finished game should allow the player to:
Make sure the keyboard code is inside the Step Event.
keyboard_check()
Check the ball's Create Event.
speed = 6;
Check the Collision Event between
obj_ball and obj_brick.
instance_destroy(other);
Check:
Verify that your brick collision contains:
score += 10;
Create several types of 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);
}
| Brick | Points |
|---|---|
| Red Brick | 10 |
| Blue Brick | 20 |
| Green Brick | 30 |
| Gold Brick | 50 |
Add power-ups that sometimes appear when a brick is destroyed.
Add sound effects when:
Create several rooms:
rm_level1
rm_level2
rm_level3
Each level should become more difficult.
| 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. |
instance_destroy()?other mean inside a Collision Event?
instance_number(obj_brick) return?
vspeed changes
from positive to negative?
| 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 |
Before submitting your assignment, demonstrate the game to your teacher.
Your teacher should see:
Submit your GameMaker project according to your teacher's instructions.
Your project must be named:
PX_lastname_Breakout
Create a working Breakout game that demonstrates: