In this project, you will create a simple Dodge the Enemies game in GameMaker.
The player will move around the room while several enemies move in random directions. Each time the player touches an enemy, the player will lose a life. When all lives are gone, the game will display a Game Over screen.
Create a new GameMaker project.
Name the project:
PX_lastname_DodgeTheEnemies
Use the GameMaker Language (GML) project type.
Create a sprite named:
spr_player
Suggested size:
32 x 32
Your player could be:
Create another sprite named:
spr_enemy
Suggested size:
32 x 32
Make the enemy easy to distinguish from the player.
Create an object named:
obj_player
Assign the sprite:
spr_player
Add a Step Event to obj_player.
var move_speed = 5;
if (keyboard_check(vk_left))
{
x -= move_speed;
}
if (keyboard_check(vk_right))
{
x += move_speed;
}
if (keyboard_check(vk_up))
{
y -= move_speed;
}
if (keyboard_check(vk_down))
{
y += move_speed;
}
Add this at the bottom of the Step Event:
x = clamp(x, 0, room_width);
y = clamp(y, 0, room_height);
Checkpoint Points: 10
Create an object named:
obj_enemy
Assign:
spr_enemy
Add a Create Event to obj_enemy.
speed = random_range(2, 5);
direction = irandom_range(0, 359);
Each enemy now receives a random speed and a random movement direction.
Add a Step Event to obj_enemy.
if (x <= 0 || x >= room_width)
{
hspeed = -hspeed;
}
if (y <= 0 || y >= room_height)
{
vspeed = -vspeed;
}
Add this code to the enemy Step Event:
if (irandom(100) == 0)
{
direction = irandom_range(0, 359);
}
This gives each enemy a small chance to suddenly change direction.
Checkpoint Points: 15
Add a Create Event to obj_player.
global.lives = 3;
The player begins with three lives.
Add a Collision Event to obj_player with obj_enemy.
global.lives -= 1;
x = room_width / 2;
y = room_height / 2;
if (global.lives <= 0)
{
room_goto(rm_gameover);
}
Each collision removes one life and moves the player back to the center.
Create a new object:
obj_controller
The controller does not need a sprite.
Add a Draw GUI Event:
draw_set_font(-1);
draw_set_color(c_white);
draw_text(20, 20, "Lives: " + string(global.lives));
Checkpoint Points: 20
Create a room named:
rm_game
Suggested size:
960 x 540
Add:
Create another room named:
rm_gameover
Create an object named:
obj_gameover
Place obj_gameover inside rm_gameover.
Add a Draw GUI Event to obj_gameover.
draw_set_halign(fa_center);
draw_set_valign(fa_middle);
draw_set_color(c_red);
draw_text(
display_get_gui_width() / 2,
display_get_gui_height() / 2,
"GAME OVER"
);
draw_set_color(c_white);
draw_text(
display_get_gui_width() / 2,
display_get_gui_height() / 2 + 50,
"Press SPACE to Play Again"
);
Add a Key Press - Space event to obj_gameover.
global.lives = 3;
room_goto(rm_game);
Checkpoint Points: 20
Experiment with enemy speed.
Original:
speed = random_range(2, 5);
Harder:
speed = random_range(3, 7);
Add this to the obj_controller Create Event:
alarm[0] = room_speed * 10;
Add an Alarm 0 Event:
instance_create_layer(
irandom(room_width),
irandom(room_height),
"Instances",
obj_enemy
);
alarm[0] = room_speed * 10;
Every 10 seconds, another enemy appears.
Add this to the controller Create Event:
global.score = 0;
alarm[1] = room_speed;
Add an Alarm 1 Event:
global.score += 1;
alarm[1] = room_speed;
Add this to the controller's Draw GUI Event:
draw_text(
20,
50,
"Score: " + string(global.score)
);
Add this to the Game Over Draw GUI Event:
draw_text(
display_get_gui_width() / 2,
display_get_gui_height() / 2 + 100,
"Final Score: " + string(global.score)
);
When restarting, reset the score:
global.lives = 3;
global.score = 0;
room_goto(rm_game);
Increase enemy speed.
Change the game from 3 lives to 5 lives.
Create:
obj_enemy_slow
obj_enemy_fast
Add sounds when:
Give the player two seconds of protection after getting hit.
Create Easy, Normal, and Hard modes.
Make sure the movement code is inside the Step Event of obj_player.
Check the enemy Create Event:
speed = random_range(2, 5);
direction = irandom_range(0, 359);
Move the player away from the enemy after a collision:
x = room_width / 2;
y = room_height / 2;
Make sure the room is named exactly:
rm_gameover
Make sure the player Create Event contains:
global.lives = 3;
| Requirement | Points |
|---|---|
| Player sprite and object | 5 |
| Player movement works correctly | 10 |
| Player stays inside the room | 5 |
| Enemy sprite and object | 5 |
| Random enemy movement | 10 |
| At least five enemies | 5 |
| Enemy bouncing/direction changes | 10 |
| Collision detection | 10 |
| Lives system works | 10 |
| Lives displayed on screen | 5 |
| Game Over system | 10 |
| Restart system | 5 |
| Game is playable and polished | 5 |
| Student added one original improvement | 5 |
| Total | 100 |
Before submitting your project, demonstrate: