GameMaker Lesson

Frogger-Style Game

Moving Obstacles, Lanes, Collision Detection, and Level Goals

Project Name

Your GameMaker project must be named:

PX_lastname_Frogger-Style

Replace lastname with your own last name.

Example:

PX_Smith_Frogger-Style

Learning Objectives

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

  1. Create a player object.
  2. Move the player using the keyboard.
  3. Create moving obstacle objects.
  4. Organize obstacles into traffic lanes.
  5. Make obstacles move at different speeds.
  6. Make obstacles move in different directions.
  7. Detect collisions between the player and obstacles.
  8. Reset the player after a collision.
  9. Create a goal area.
  10. Detect when the player reaches the goal.
  11. Track lives and score.
  12. Create a level or win condition.

The Goal

You will create a game inspired by the classic PX_lastname_Frogger arcade game.

The player begins near the bottom of the screen and must move toward the top.

The player must cross several lanes filled with moving obstacles.

If the player touches an obstacle:

If the player reaches the goal:

The goal is to safely cross all lanes and reach the goal area.

Part 1 - Create the Project

  1. Open GameMaker.
  2. Create a new project.
  3. Save the project using the required filename.
PX_lastname_Frogger-Style
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 Player character
spr_car Moving car obstacle
spr_truck Larger moving obstacle
spr_goal Goal area
spr_lane Optional road or lane graphic

Suggested Sprite Sizes

Set the player sprite origin to Middle Center.

Part 3 - Create the Objects

Object Sprite
obj_player spr_player
obj_car spr_car
obj_truck spr_truck
obj_goal spr_goal
obj_controller No sprite

Part 4 - Create the Room

Create a room called:

rm_game

Suggested room size:

The room should contain:

---------------------------------------
             GOAL AREA
---------------------------------------

<-- Cars moving left

--> Cars moving right

<-- Trucks moving left

--> Cars moving right

---------------------------------------
            START AREA
---------------------------------------

Your game should contain at least 4 traffic lanes.

Part 5 - Player Movement

Select:

obj_player

Add a Step Event.

var move_speed = 4;

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

Optional WASD Controls

You may also allow:

var move_speed = 4;

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

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

if (keyboard_check(vk_up) || keyboard_check(ord("W")))
{
    y -= move_speed;
}

if (keyboard_check(vk_down) || keyboard_check(ord("S")))
{
    y += move_speed;
}

Part 6 - Keep the Player Inside the Room

Add this code to the player's Step Event:

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

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

Checkpoint 2:
Run the game and demonstrate that the player moves in four directions and stays inside the room.

Part 7 - Create Moving Cars

Select:

obj_car

Add a Create Event.

For a car moving from left to right:

hspeed = 4;

For a car moving from right to left:

hspeed = -4;

Understanding hspeed

hspeed controls horizontal movement.

Part 8 - Make Cars Wrap Around the Screen

Without wrapping, a car will leave the room and disappear.

Add a Step Event to obj_car.

if (x > room_width + sprite_width)
{
    x = -sprite_width;
}

if (x < -sprite_width)
{
    x = room_width + sprite_width;
}

The car will now reappear on the opposite side of the room.

Part 9 - Create Multiple Traffic Lanes

Place several car objects in each traffic lane.

Lane 1:   CAR --->      CAR --->

Lane 2:      <--- CAR       <--- CAR

Lane 3: TRUCK --->          TRUCK --->

Lane 4:      <--- CAR       <--- CAR

Each lane should have:

Part 10 - Different Lane Speeds

Slow lane:

hspeed = 2;

Medium lane:

hspeed = 4;

Fast lane:

hspeed = 6;

Use negative values for lanes moving toward the left.

hspeed = -5;

Part 11 - Create a Truck

Select:

obj_truck

Add a Create Event:

hspeed = -3;

Add a Step Event:

if (x > room_width + sprite_width)
{
    x = -sprite_width;
}

if (x < -sprite_width)
{
    x = room_width + sprite_width;
}

Trucks can be:

Part 12 - Save the Player's Starting Position

Select obj_player.

Add a Create Event:

start_x = x;
start_y = y;

These variables remember where the player started.

Part 13 - Player and Car Collision

In obj_player, add a Collision Event with:

obj_car

Enter:

global.lives -= 1;

x = start_x;
y = start_y;

When the player touches a car:

Part 14 - Player and Truck Collision

Add another Collision Event in obj_player with:

obj_truck

Enter:

global.lives -= 1;

x = start_x;
y = start_y;

Part 15 - Add Lives, Score, and Level

Create:

obj_controller

This object does not need a sprite.

Place one instance of obj_controller in the room.

Add a Create Event:

global.lives = 3;
global.score = 0;
global.level = 1;
global.goals_reached = 0;

Part 16 - Check for Game Over

Add a Step Event to obj_controller.

if (global.lives <= 0)
{
    show_message("GAME OVER");
    room_restart();
}

When the player's lives reach zero, the game restarts.

Part 17 - Create the Goal

Create:

obj_goal

Place the goal near the top of the room.

The player must safely cross the traffic lanes and touch the goal.

Part 18 - Detect the Goal

In obj_player, add a Collision Event with:

obj_goal

Enter:

global.score += 100;
global.goals_reached += 1;

x = start_x;
y = start_y;

The player receives 100 points each time the goal is reached.

Part 19 - Display Score, Lives, and Level

Select obj_controller.

Add a Draw GUI Event.

draw_text(20, 20, "Score: " + string(global.score));
draw_text(20, 50, "Lives: " + string(global.lives));
draw_text(20, 80, "Level: " + string(global.level));
draw_text(20, 110, "Goals: " + string(global.goals_reached));

The game will now display the player's score, lives, level, and completed goals.

Part 20 - Advance to the Next Level

Add this code to the Step Event of obj_controller.

if (global.goals_reached >= 3)
{
    global.level += 1;
    global.goals_reached = 0;

    show_message("LEVEL COMPLETE!");
}

The player must reach the goal three times to complete the level.

Part 21 - Make Higher Levels Harder

One way to increase difficulty is to make obstacles faster.

Example:

hspeed = 2 + global.level;

As the level number increases, the obstacle speed also increases.

You can also make higher levels harder by:

Part 22 - Test Your Game

Your game should now contain:

Debugging Checklist

Player Does Not Move

Make sure the movement code is in the player's Step Event.

keyboard_check()

Cars Do Not Move

Check the car Create Event.

hspeed = 4;

Cars Leave the Screen

Make sure the wrapping code is in the obstacle Step Event.

Collision Does Not Work

Check:

Player Does Not Return to Start

Verify the player Create Event contains:

start_x = x;
start_y = y;

Lives Do Not Change

Check the collision code:

global.lives -= 1;

Score Does Not Increase

Check the goal collision:

global.score += 100;

Required Features

  1. Correct project filename.
  2. Player-controlled movement.
  3. At least 4 traffic lanes.
  4. At least 2 different obstacle types.
  5. Obstacles moving left.
  6. Obstacles moving right.
  7. Different lane speeds.
  8. Object wrapping.
  9. Player-obstacle collision detection.
  10. Player reset after a collision.
  11. Lives system.
  12. Score system.
  13. Goal area.
  14. Goal detection.
  15. Level or win condition.

Challenge 1 - Grid Movement

Make the player move one space at a time instead of moving continuously.

if (keyboard_check_pressed(vk_up))
{
    y -= 32;
}

Use similar code for left, right, and down.

Challenge 2 - Add Water

Add a river section near the top of the level.

The player loses a life if they enter the water without standing on a safe object.

Challenge 3 - Add Moving Logs

Create:

obj_log

Logs move across the water in the same way cars move across the road.

The player should be able to safely stand on the logs.

Challenge 4 - Ride on Moving Logs

Make the player move with a log while standing on it.

This introduces more advanced collision and movement programming.

Challenge 5 - Multiple Goal Spaces

Create several goal positions across the top of the room.

GOAL    GOAL    GOAL    GOAL    GOAL

Require the player to reach each goal location.

Challenge 6 - Increasing Difficulty

Increase difficulty by:

Challenge 7 - Timer

Give the player a limited amount of time to reach the goal.

Display the remaining time on the screen.

Challenge 8 - Sound Effects

Add sound effects for:

Vocabulary

Term Definition
Sprite The graphic representing an object.
Object A GameMaker resource containing behavior.
Instance A specific copy of an object placed in the game.
Lane A horizontal area where obstacles travel.
Collision When two objects touch.
Step Event Code that repeatedly executes while the game runs.
Create Event Code that executes when an object instance is created.
hspeed Controls horizontal movement.
keyboard_check() Checks whether a keyboard key is currently pressed.
keyboard_check_pressed() Checks whether a key was just pressed.
clamp() Restricts a value to a specified range.
Global Variable A variable that can be accessed by multiple objects.
Goal The destination the player must reach.
Level A stage of the game.
Wrap Moving an object from one edge of the room to the opposite edge.

Review Questions

  1. What does hspeed control?
  2. What is the difference between positive and negative hspeed?
  3. Why do obstacles need screen wrapping?
  4. What is the purpose of start_x and start_y?
  5. What happens when the player collides with a car?
  6. What is the purpose of global.lives?
  7. How does the game detect that the player reached the goal?
  8. Why should different traffic lanes have different speeds?
  9. What are two ways to make higher levels more difficult?
  10. What is the difference between keyboard_check() and keyboard_check_pressed()?

100-Point Grading Rubric

Requirement Points
Correct filename PX_lastname_Frogger-Style 5
Room layout 5
Player sprite and object 5
Player movement 10
Player stays inside room 5
At least 4 traffic lanes 10
Moving cars 5
Second obstacle type 5
Obstacles move in both directions 5
Different obstacle speeds 5
Screen wrapping works 10
Collision detection works 10
Lives system 5
Score system 5
Goal detection and level condition 10
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_Frogger-Style

Before Submitting

Final Goal

Create a working Frogger-Style Game that demonstrates:

Player Movement + Moving Obstacles + Traffic Lanes + Collision Detection + Lives + Level Goals