| Project | Skills |
|---|---|
| Maze Game | Walls, Collisions, and Rooms |
By the end of this lesson, students will be able to:
Create a maze game where the player uses the arrow keys to travel through a maze.
The player must:
Open GameMaker and create a new project.
Name the project:
PX_lastname_MazeGame
Choose a GameMaker Language (GML) project.
Create a new sprite and name it:
spr_player
A good starting size is:
32 x 32 pixels
Create a simple square, circle, character, robot, or other image to represent the player.
Create a new object and name it:
obj_player
Assign the following sprite to it:
spr_player
Add a Step Event to obj_player.
Enter the following code:
var move_speed = 4;
if (keyboard_check(vk_right))
{
x += move_speed;
}
if (keyboard_check(vk_left))
{
x -= move_speed;
}
if (keyboard_check(vk_up))
{
y -= move_speed;
}
if (keyboard_check(vk_down))
{
y += move_speed;
}
The command keyboard_check() checks whether the player
is currently holding down a keyboard key.
| Code | Movement |
|---|---|
x += 4 |
Move Right |
x -= 4 |
Move Left |
y += 4 |
Move Down |
y -= 4 |
Move Up |
spr_player.obj_player.Create another sprite and name it:
spr_wall
Use a size of approximately:
32 x 32 pixels
You can make the wall look like:
Create a new object named:
obj_wall
Assign:
spr_wall
as the wall object's sprite.
The player should not be able to walk through obj_wall.
Replace the original movement code in the player's Step Event with the following:
var move_speed = 4;
var hspeed_player = 0;
var vspeed_player = 0;
// Horizontal movement
if (keyboard_check(vk_right))
{
hspeed_player = move_speed;
}
if (keyboard_check(vk_left))
{
hspeed_player = -move_speed;
}
// Vertical movement
if (keyboard_check(vk_down))
{
vspeed_player = move_speed;
}
if (keyboard_check(vk_up))
{
vspeed_player = -move_speed;
}
// Horizontal collision
if (!place_meeting(x + hspeed_player, y, obj_wall))
{
x += hspeed_player;
}
// Vertical collision
if (!place_meeting(x, y + vspeed_player, obj_wall))
{
y += vspeed_player;
}
The GameMaker function:
place_meeting()
checks whether an object would collide with another object.
For example:
place_meeting(x + 4, y, obj_wall)
This asks GameMaker:
The symbol:
!
means:
NOT
Therefore:
if (!place_meeting(x + hspeed_player, y, obj_wall))
means:
Create a new room named:
rm_maze
A good starting room size is:
| Setting | Value |
|---|---|
| Width | 960 |
| Height | 640 |
Open rm_maze and place multiple instances of obj_wall in the room.
Create:
Where:
Place one instance of obj_player near the beginning of the maze.
spr_wall.obj_wall.rm_maze.Run the game and use:
Test the following:
Demonstrate that the player can move through the maze but cannot move through the walls.
Create a new sprite named:
spr_goal
Your goal could look like:
Create a new object named:
obj_goal
Assign:
spr_goal
to the object.
Place obj_goal at the end of the maze.
Add a Collision Event to obj_player.
Choose collision with:
obj_goal
Add:
show_message("You escaped the maze!");
When the player reaches the goal, GameMaker will display a message.
After displaying the message, restart the maze using:
show_message("You escaped the maze!");
room_restart();
Create another room named:
rm_maze2
Create a harder maze in the second room.
Change the goal collision code in Level 1 to:
show_message("Level 1 Complete!");
room_goto(rm_maze2);
Now the player progresses from Level 1 to Level 2.
Rooms are the areas where GameMaker games take place.
A room could represent:
For this project:
| Room | Purpose |
|---|---|
rm_maze |
Level 1 |
rm_maze2 |
Level 2 |
obj_goal.Create a sprite:
spr_coin
Create an object:
obj_coin
Create a Collision Event between obj_player and obj_coin.
Add:
instance_destroy(other);
Later, you can add a score variable to count how many coins the player collects.
Add the following code to the player's Step Event:
if (keyboard_check_pressed(ord("R")))
{
room_restart();
}
Now the player can press R to restart the maze.
Demonstrate at least three improvements that you added to your Maze Game.
| Term | Meaning |
|---|---|
| Sprite | The image used to represent something in the game. |
| Object | An item that contains game behavior and programming. |
| Instance | A copy of an object placed inside a room. |
| Room | The area or level where the game takes place. |
| Collision | When two game objects touch or overlap. |
| Step Event | An event that runs repeatedly while the game is running. |
| x | The object's horizontal position. |
| y | The object's vertical position. |
| place_meeting() | Checks whether a collision would occur. |
| room_goto() | Moves the game to another room. |
| room_restart() | Restarts the current room. |
x value control?y value control?place_meeting() do?| Requirement | Points |
|---|---|
| GameMaker project created correctly | 5 |
| Player sprite | 5 |
| Player object | 5 |
| Keyboard movement | 15 |
| Wall sprite | 5 |
| Wall object | 5 |
| Maze design | 15 |
| Wall collision works correctly | 20 |
| Goal object | 5 |
| Goal collision works | 5 |
| Game successfully completes, restarts, or changes rooms | 5 |
| Three student enhancements | 10 |
| Total | 100 |