Project: Click the Target
Level: Beginner
Estimated Time: 1–2 class periods
Main Concepts: Mouse input, score, random positions, variables, and timers
By the end of this lesson, students will be able to:
The player will see a target somewhere on the screen. The goal is to click the target as many times as possible before time runs out.
Every time the player clicks the target:
The goal is to get the highest score possible before the timer reaches zero.
Name the project:
PX_lastname_ClickTarget
Create a room named:
rm_game
A room size of approximately 800 × 600 works well.
Create a new sprite named:
spr_target
Create or draw a simple target.
Your target could look like:
A sprite size of approximately 64 × 64 pixels works well.
Create an object named:
obj_target
Assign this sprite:
spr_target
Place one obj_target somewhere inside rm_game.
Add the following event to obj_target:
Mouse → Left Pressed
Add the following code:
score += 1;
GameMaker has a built-in variable named score.
This line increases the player's score by one.
Add the following code underneath the score code:
x = random_range(50, room_width - 50); y = random_range(50, room_height - 50);
The complete event should now be:
score += 1; x = random_range(50, room_width - 50); y = random_range(50, room_height - 50);
The random_range() function generates a random number between two values.
For example:
random_range(50, room_width - 50)
This chooses a random horizontal location while helping prevent the target from appearing partially outside the room.
Create another object named:
obj_game
This object does not need a sprite.
The game controller will manage:
Place one obj_game inside rm_game.
Add a Create Event to obj_game.
Enter:
score = 0; time_left = 30; alarm[0] = room_speed;
This creates a game that lasts approximately 30 seconds.
The value room_speed represents approximately one second worth
of game steps.
Therefore:
alarm[0] = room_speed;
sets Alarm 0 to activate approximately one second later.
Add an Alarm 0 Event to obj_game.
Enter:
time_left -= 1;
if (time_left > 0)
{
alarm[0] = room_speed;
}
Every time Alarm 0 activates:
Modify the Alarm 0 code:
time_left -= 1;
if (time_left > 0)
{
alarm[0] = room_speed;
}
else
{
instance_destroy(obj_target);
}
When the timer reaches zero, the target is removed from the room.
Add a Draw GUI Event to obj_game.
Enter:
draw_text(20, 20, "Score: " + string(score)); draw_text(20, 50, "Time: " + string(time_left));
The player should now see something similar to:
Score: 7 Time: 22
Modify the Draw GUI Event:
draw_text(20, 20, "Score: " + string(score));
draw_text(20, 50, "Time: " + string(time_left));
if (time_left <= 0)
{
draw_text(300, 250, "GAME OVER");
draw_text(300, 280, "Final Score: " + string(score));
}
score = 0; time_left = 30; alarm[0] = room_speed;
time_left -= 1;
if (time_left > 0)
{
alarm[0] = room_speed;
}
else
{
instance_destroy(obj_target);
}
draw_text(20, 20, "Score: " + string(score));
draw_text(20, 50, "Time: " + string(time_left));
if (time_left <= 0)
{
draw_text(300, 250, "GAME OVER");
draw_text(300, 280, "Final Score: " + string(score));
}
score += 1; x = random_range(50, room_width - 50); y = random_range(50, room_height - 50);
START GAME
Set score to 0
Set timer to 30 seconds
Display target
WHILE time is greater than 0
IF player clicks target
Increase score
Move target
END IF
Decrease timer
END WHILE
Remove target
Display final score
END GAME
| Test | Expected Result |
|---|---|
| Start the game | The target appears. |
| Click the target | The score increases. |
| Click the target | The target moves to a new location. |
| Wait one second | The timer decreases. |
| Timer reaches zero | The target disappears. |
| Game ends | The final score is displayed. |
Make sure the code is inside:
Mouse → Left Pressed
and contains:
score += 1;
Check this code:
x = random_range(50, room_width - 50); y = random_range(50, room_height - 50);
Make sure Alarm 0 restarts itself:
alarm[0] = room_speed;
Make sure the display code is placed inside the Draw GUI Event.
Change:
score += 1;
to:
score += 5;
Now each target is worth five points.
score += 1 mean?random_range() do?time_left reaches zero?string(score) when displaying the score?Create an improved version called:
ClickTheTarget_Advanced
Add at least two of these features:
A successful finished game should demonstrate these three major concepts:
Mouse Input → Score → Timer
The player clicks a target, earns points, and tries to score as many points as possible before the timer expires.