Make a platformer in GameMaker (any version)

A micro tutorial by @shaunspalding. Start by making a new project (GML project if GMS2). Then:

Set framerate

Make two sprites

Make two objects

Player code

Add the following code exactly as written:

grv = 0.2; //gravity
hsp = 0; //current horizontal speed
vsp = 0; //current vertical speed
hsp_walk = 4; //walk speed
vsp_jump = -5; //jump speed

Then add the “Step” event. If not in gms2 add the code action again as needed. Copy & paste the following code:

//Get inputs (1 = pressed, 0 = not pressed)
key_right = keyboard_check(vk_right);
key_left = keyboard_check(vk_left);
key_jump = keyboard_check(vk_space);

//Work out where to move horizontally
hsp = (key_right - key_left) * hsp_walk;

//Work out where to move vertically
vsp = vsp + grv;

//Work out if we should jump
if (place_meeting(x,y+1,oWall)) and (key_jump)
{
    vsp = vsp_jump; 
}

//Check for horizontal collisions and then move if we can
var onepixel = sign(hsp) //moving left or right? right = 1, left = -1.
if (place_meeting(x+hsp,y,oWall))
{
    //move as close as we can
    while (!place_meeting(x+onepixel,y,oWall))
    {
        x = x + onepixel;
    }
    hsp = 0;
}
x = x + hsp;

//Check for vertical collisions and then move if we can
var onepixel = sign(vsp) //up = -1, down = 1.
if (place_meeting(x,y+vsp,oWall))
{
    //move as close as we can
    while (!place_meeting(x,y+onepixel,oWall))
    {
        y = y + onepixel;
    }
    vsp = 0;
}
y = y + vsp;

Lots of stuff right? Don’t worry about it. Read it later at your own pace.

Create your gameworld

Go to your room (One is created for you by default in GMS2) by double clicking on it in the resource tree.

In GMS2:
In GMS2 select the “Instances layer” on the left and click and drag oPlayer from your resource tree into the world. Then left click once on oWall to highlight it. Then hold alt and click and drag over the world to paint walls.

In other versions:
Click the “Objects” tab in the room editor. Select the drop down list labeled “Object to add with left mouse” and select oPlayer. Add oPlayer to the room. Then select oWall and tick “Delete underlying”. Hold shift and control then click and drag to add many walls. Careful to not to overwrite your player! Just add it again if you do.

Run your game!

Press F5 and wait for your game to compile. If everything went right you now have a platformer! Control with left/right arrow keys and space bar. In the code editors you can middle click on any yellow/orange text (functions) in the code to open the manual on the appropriate page. Experiment and grow from here. You’ll probably run into lots of problems. Persevere!

You can use the room editor and view editor to make a bigger room and have the camera follow the player around without adding any more code. Have fun!

@shaunspalding
Patreon