Start a blank GameMaker project, make one object, and write four lines that move it every frame an arrow key is held down.
This is the single-object slice: one sprite, one object, one Step event, and held-key movement, with no gravity, no collisions, no level design, and no camera. When your square glides while you hold an arrow key and stops when you let go, you are finished.
GameMaker’s interface gets updated regularly. The steps below match the current GameMaker manual where noted. If a button or menu sits somewhere else on your screen, search the manual for the feature name instead of guessing.
A quick word on the language
The current manual calls GameMaker’s language GameMaker Language, or GML. You can use it two ways. GML Visual chains action blocks together. GML Code means typing code yourself.
This page uses GML Code. The script is only a few lines, and typed code is easier to read, copy, and compare when something goes wrong.
Step 1: Create a blank project
Open GameMaker. On the Start Page, click New. The manual says this opens a menu where you pick a project type and then a template.
Choose Game as the project type. For the template, pick one of the two blank options. The manual lists Blank Game, which smooths edges in the final image, and Blank Pixel Game, which keeps pixels crisp. Either works for a plain colored square.
Name the project FirstObject, choose a save location, and confirm. The manual names the confirm button Let’s Go!
The manual also warns against saving projects inside a cloud-synced folder, because it can cause problems when saving and building. Pick a plain local folder.
The new project opens with an Asset Browser on one side and a starting room already created for you.
Step 2: Make one sprite
An object with no sprite is invisible, so you need something to look at first.
In the Asset Browser, create a new sprite. Right-clicking in the browser gives you a create option, but check the manual if your version groups it differently. Name the sprite spr_box.
Open the sprite’s image editor and fill the whole canvas with one bright color. The default canvas size is fine. You are not making art today. You only need a shape you can see against the room’s background.
In the sprite editor, set the origin to the middle center. The origin is the point GameMaker uses for the object’s x and y position. A centered origin makes movement feel more natural.
Close the sprite editor.
Step 3: Make one object
Create a new object in the Asset Browser and name it obj_box.
The manual explains the difference between an object and an instance. The object is a template in the Asset Browser. An instance is a copy of it placed in a room. You write behavior on the object, and every instance follows it.
In the object editor, find the sprite field and choose spr_box. The object now has a look.
Step 4: Add the Step event
The manual describes events as moments in the game loop when your code runs. Every frame, GameMaker runs a series of events for each instance. The Step event runs once per step, which is once per frame.
In the object editor, press Add Event at the bottom of the events list. Choose Step, then pick the plain Step event, not Begin Step or End Step.
GameMaker may ask you to choose between GML Code and GML Visual. Pick GML Code. The manual says you can tick a box so it stops asking for this project.
A code window opens next to the object.
Step 5: Write the movement
Type this into the Step event:
var spd = 4;
if (keyboard_check(vk_right)) x += spd;
if (keyboard_check(vk_left)) x -= spd;
if (keyboard_check(vk_down)) y += spd;
if (keyboard_check(vk_up)) y -= spd;
Here is what each piece does.
var spd = 4; makes a local variable that exists only while this event runs. It holds how many pixels the square moves per step. Change it later to make the square faster or slower.
keyboard_check is the function the manual describes for checking whether a key is held down. It returns true on every step the key stays down, not just once. That is what makes the square keep moving while you hold the key.
vk_right, vk_left, vk_down, and vk_up are the built-in constants for the arrow keys.
x and y are the instance’s position in the room. In GameMaker, y grows downward. That is why the down arrow adds to y and the up arrow subtracts from it.
Step 6: Put one instance in the room
Open the starting room from the Asset Browser. The room editor opens with its layers listed on one side.
Select the layer meant for instances. Then drag obj_box from the Asset Browser into the middle of the room. A copy of your square appears.
If GameMaker refuses to place it, you probably have a background or tile layer selected. Click the instance layer and try again.
Step 7: Run it
Press the Run button in the IDE toolbar. The manual says the Run command in the Build menu does the same thing.
A game window opens with your square in it. Click inside the window so it has keyboard focus.
Now test three things:
- Hold the right arrow. The square should glide right and keep going.
- Let go. It should stop at once.
- Tap the left arrow quickly. It should nudge only a little, because it moved for only a few steps.
Close the game window when you are done.
Step 8: Feel the difference between held and pressed
This one swap teaches more than a page of explanation.
Change the first keyboard_check to keyboard_check_pressed. The manual says that version fires only once, on the step the key goes down. Run the game and hold the right arrow. The square jumps a few pixels and stops, even though your finger is still down.
Change it back to keyboard_check before you continue. Held movement needs the held check.
Optional: add WASD
If you want letter keys too, the manual says keyboard_check also accepts ord() with a capital letter. You can extend a line like this:
if (keyboard_check(vk_right) || keyboard_check(ord("D"))) x += spd;
Repeat that pattern for A, S, and W. Note that the letter inside ord() must be uppercase.
If it does not work
You see an empty window. The object has no sprite, or no instance is in the room. Check both.
The square does not move. You may have put the code in a Create event instead of the Step event. Create runs only once.
It moves once and stops. You are still using keyboard_check_pressed.
Keys do nothing at all. Click inside the game window first, then try again.
Diagonal movement is faster than straight movement. That is expected with this simple code. Fixing it is a good next lesson, but not today’s.
Stop here
You made a project, a sprite, an object, a Step event, and one instance, and you watched it respond to held keys. That is the core loop of GameMaker: objects react to events, and instances in a room carry out that behavior.
Save the project. Tomorrow, try building it again from an empty project without this page open.