GameMaker Lesson: Dodge the Enemies

Random Movement, Lives, Collisions, and Game Over

Lesson Overview

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.

Learning Objectives

Part 1 - Create the Project

Create a new GameMaker project.

Name the project:

PX_lastname_DodgeTheEnemies
  • Create 2 files and where to drop them:
  • file one: PX_lastname_DodgeTheEnemies.png (Screen print of the program inside the game maker ide)
  • file two: PX_lastname_DodgeTheEnemies.mp4 (Video of you playing the game. I need at least 7 seconds)
  • Be sure to drop off the two file into your google drive for class under 2026_27KleinCollins\your class name
  • Drop your two files into google classroom.
  • Use the GameMaker Language (GML) project type.

    Part 2 - Create the Sprites

    Player Sprite

    Create a sprite named:

    spr_player

    Suggested size:

    32 x 32

    Your player could be:

    Enemy Sprite

    Create another sprite named:

    spr_enemy

    Suggested size:

    32 x 32

    Make the enemy easy to distinguish from the player.

    Part 3 - Create the Player Object

    Create an object named:

    obj_player

    Assign the sprite:

    spr_player

    Part 4 - Player Movement

    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;
    }

    Keep the Player Inside the Room

    Add this at the bottom of the Step Event:

    x = clamp(x, 0, room_width);
    y = clamp(y, 0, room_height);

    Checkpoint 1 - Show Your Teacher

    Checkpoint Points: 10

    Part 5 - Create the Enemy Object

    Create an object named:

    obj_enemy

    Assign:

    spr_enemy

    Part 6 - Random Enemy Movement

    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.

    Part 7 - Make Enemies Bounce

    Add a Step Event to obj_enemy.

    if (x <= 0 || x >= room_width)
    {
        hspeed = -hspeed;
    }
    
    if (y <= 0 || y >= room_height)
    {
        vspeed = -vspeed;
    }

    Part 8 - Random Direction Changes

    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 2 - Show Your Teacher

    Checkpoint Points: 15

    Part 9 - Create the Lives System

    Add a Create Event to obj_player.

    global.lives = 3;

    The player begins with three lives.

    Part 10 - Player and Enemy Collision

    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.

    Part 11 - Display the Lives

    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 3 - Show Your Teacher

    Checkpoint Points: 20

    Part 12 - Create the Main Room

    Create a room named:

    rm_game

    Suggested size:

    960 x 540

    Add:

    Part 13 - Create the Game Over Room

    Create another room named:

    rm_gameover

    Create an object named:

    obj_gameover

    Place obj_gameover inside rm_gameover.

    Part 14 - Draw the Game Over Message

    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"
    );

    Part 15 - Restart the Game

    Add a Key Press - Space event to obj_gameover.

    global.lives = 3;
    room_goto(rm_game);

    Checkpoint 4 - Show Your Teacher

    Checkpoint Points: 20

    Part 16 - Increase the Difficulty

    Experiment with enemy speed.

    Original:

    speed = random_range(2, 5);

    Harder:

    speed = random_range(3, 7);

    Part 17 - Optional: Add More Enemies Over Time

    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.

    Part 18 - Optional Survival Score

    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;

    Display the Score

    Add this to the controller's Draw GUI Event:

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

    Display Final 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);

    Student Challenge Activities

    Challenge 1 - Faster Enemies

    Increase enemy speed.

    Challenge 2 - More Lives

    Change the game from 3 lives to 5 lives.

    Challenge 3 - Different Enemy Types

    Create:

    obj_enemy_slow
    obj_enemy_fast

    Challenge 4 - Sound Effects

    Add sounds when:

    Challenge 5 - Invincibility

    Give the player two seconds of protection after getting hit.

    Challenge 6 - Difficulty Levels

    Create Easy, Normal, and Hard modes.

    Troubleshooting

    My Player Does Not Move

    Make sure the movement code is inside the Step Event of obj_player.

    My Enemies Do Not Move

    Check the enemy Create Event:

    speed = random_range(2, 5);
    direction = irandom_range(0, 359);

    My Player Instantly Loses All Lives

    Move the player away from the enemy after a collision:

    x = room_width / 2;
    y = room_height / 2;

    My Game Over Room Does Not Appear

    Make sure the room is named exactly:

    rm_gameover

    I Get an Error About global.lives

    Make sure the player Create Event contains:

    global.lives = 3;

    Game Requirements

    1. A controllable player.
    2. Arrow-key movement.
    3. At least five enemies.
    4. Random enemy movement.
    5. Enemy bouncing or direction changes.
    6. Player/enemy collision detection.
    7. At least three lives.
    8. Lives displayed on screen.
    9. A Game Over screen.
    10. A restart feature.

    100-Point Grading Rubric

    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

    Final Teacher Check

    Before submitting your project, demonstrate:

    Game Flow

    START GAME ↓ Player moves with arrow keys ↓ Enemies move randomly ↓ Player dodges enemies ↓ Enemy collision ↓ Lose one life ↓ Lives > 0? / \ YES NO ↓ ↓ Continue GAME OVER ↓ Press SPACE ↓ Restart Game