GameMaker Lesson: Click the Target

Project: Click the Target

Level: Beginner

Estimated Time: 1–2 class periods

Main Concepts: Mouse input, score, random positions, variables, and timers

Learning Objectives

By the end of this lesson, students will be able to:

Game Description

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:

  1. The score increases.
  2. The target moves to a random location.
  3. The player tries to click it again.

The goal is to get the highest score possible before the timer reaches zero.

Part 1: Create the Project

  1. Open GameMaker.
  2. Select GameMaker Language (GML).
  3. Name the project:

    PX_lastname_ClickTarget
  4. Create 2 files and where to drop them:
  5. file one: PX_lastname_ClickTarget.png (Screen print of the program inside the game maker ide)
  6. file two: PX_lastname_ClickTarget.mp4 (Video of you playing the game. I need at least 7 seconds)
  7. Be sure to drop off the two file into your google drive for class under 2026_27KleinCollins\your class name
  8. Drop your two files into google classroom.
ClickTheTarget

Create a room named:

rm_game

A room size of approximately 800 × 600 works well.

Part 2: Create the Target Sprite

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.

Important: Set the sprite origin to Middle Center.

Part 3: Create the Target Object

Create an object named:

obj_target

Assign this sprite:

spr_target

Place one obj_target somewhere inside rm_game.

Part 4: Detect a Mouse Click

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.

Move the Target

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

Understanding random_range()

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.

Part 5: Create the Game Controller

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.

Part 6: Initialize the 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.

Understanding room_speed

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.

Part 7: Create the Timer

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:

  1. One second is removed from the timer.
  2. The alarm starts again if there is still time remaining.

Part 8: End the Game

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.

Part 9: Display the Score and Time

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

Part 10: Display Game Over

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

Complete obj_game Code

Create Event

score = 0;

time_left = 30;

alarm[0] = room_speed;

Alarm 0 Event

time_left -= 1;

if (time_left > 0)
{
    alarm[0] = room_speed;
}
else
{
    instance_destroy(obj_target);
}

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

Complete obj_target Code

Mouse → Left Pressed Event

score += 1;

x = random_range(50, room_width - 50);
y = random_range(50, room_height - 50);

How the Program Works

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

Student Testing Checklist

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.

Common Problems

Score Does Not Increase

Make sure the code is inside:

Mouse → Left Pressed

and contains:

score += 1;

Target Does Not Move

Check this code:

x = random_range(50, room_width - 50);
y = random_range(50, room_height - 50);

Timer Does Not Decrease

Make sure Alarm 0 restarts itself:

alarm[0] = room_speed;

Text Does Not Display

Make sure the display code is placed inside the Draw GUI Event.

Challenge 1: More Points

Change:

score += 1;

to:

score += 5;

Now each target is worth five points.

Challenge 2: Shrinking Target

After every successful click, make the target slightly smaller:

image_xscale *= 0.95;
image_yscale *= 0.95;

This makes the game harder as the score increases.

Challenge 3: Penalize Missed Clicks

Create a background object.

If the player clicks the background instead of the target:

score -= 1;

Now accuracy matters.

Challenge 4: Random Target Size

Add this code after the target is clicked:

var size = random_range(0.5, 1.5);

image_xscale = size;
image_yscale = size;

The target will randomly appear larger or smaller after each click.

Challenge 5: Bonus Target

Create another object:

obj_bonus_target

Make the bonus target worth five points:

score += 5;

The bonus target could appear only occasionally.

Challenge 6: Increase Difficulty Every Five Points

Add:

if (score mod 5 == 0)
{
    image_xscale *= 0.90;
    image_yscale *= 0.90;
}

Every time the player's score reaches another multiple of five, the target becomes smaller.

Challenge 7: Add a High Score

Create a high-score variable:

high_score = 0;

At the end of the game:

if (score > high_score)
{
    high_score = score;
}

Display the high score:

draw_text(20, 80, "High Score: " + string(high_score));

Discussion Questions

  1. What event detects the mouse clicking the target?
  2. What does score += 1 mean?
  3. What does random_range() do?
  4. What is the purpose of an Alarm event?
  5. Why must Alarm 0 restart itself?
  6. What happens when time_left reaches zero?
  7. Why do we use string(score) when displaying the score?
  8. How could you make the game easier?
  9. How could you make the game more difficult?
  10. What other games could use similar mouse input?

Extension Project

Create an improved version called:

ClickTheTarget_Advanced

Add at least three of these features:

Finished Game

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.

``` I formatted it as a complete standalone HTML page with student-friendly sections, code blocks, challenge boxes, and a testing table.