Moving Obstacles, Lanes, Collision Detection, and Level Goals
Your GameMaker project must be named:
PX_lastname_Frogger-Style
Replace lastname with your own last name.
Example:
PX_Smith_Frogger-Style
By the end of this lesson, you should be able to:
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.
PX_lastname_Frogger-Style
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 |
Set the player sprite origin to Middle Center.
| Object | Sprite |
|---|---|
| obj_player | spr_player |
| obj_car | spr_car |
| obj_truck | spr_truck |
| obj_goal | spr_goal |
| obj_controller | No sprite |
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.
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;
}
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;
}
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.
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;
hspeed controls horizontal movement.
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.
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:
Slow lane:
hspeed = 2;
Medium lane:
hspeed = 4;
Fast lane:
hspeed = 6;
Use negative values for lanes moving toward the left.
hspeed = -5;
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:
Select obj_player.
Add a Create Event:
start_x = x;
start_y = y;
These variables remember where the player started.
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:
Add another Collision Event in obj_player with:
obj_truck
Enter:
global.lives -= 1;
x = start_x;
y = start_y;
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;
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.
Create:
obj_goal
Place the goal near the top of the room.
The player must safely cross the traffic lanes and touch 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.
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.
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.
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:
Your game should now contain:
Make sure the movement code is in the player's Step Event.
keyboard_check()
Check the car Create Event.
hspeed = 4;
Make sure the wrapping code is in the obstacle Step Event.
Check:
Verify the player Create Event contains:
start_x = x;
start_y = y;
Check the collision code:
global.lives -= 1;
Check the goal collision:
global.score += 100;
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.
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.
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.
Make the player move with a log while standing on it.
This introduces more advanced collision and movement programming.
Create several goal positions across the top of the room.
GOAL GOAL GOAL GOAL GOAL
Require the player to reach each goal location.
Increase difficulty by:
Give the player a limited amount of time to reach the goal.
Display the remaining time on the screen.
Add sound effects for:
| 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. |
hspeed control?hspeed?
start_x and
start_y?
global.lives?
keyboard_check() and
keyboard_check_pressed()?
| 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 |
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_Frogger-Style
Create a working Frogger-Style Game that demonstrates: