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
- This first step isn’t too important but the numbers we use are based on it.
- In GMS2, go to game settings (Cog symbol) -> Main options -> General and set “Game frames per second” to 60. Make sure you click on “Apply” afterwards.
- In other versions, create a new room by right clicking on “rooms” in the resource tree and selecting “Create room”. In the settings tab that will appear, set “Speed” from 30 to 60. Name the room whatever you like if you want to.
Make two sprites
- Right click on sprites in the resource tree and select “Add sprite”. Do this twice.
- Call one sWall, and the other sPlayer. Edit the images. Make them both solid colour boxes (important.)
- Close any sprite/image windows still open. Changes are saved automatically.
Make two objects
- Again, right click in resource tree, this time on objects. Click create object. Do this twice.
- Call one oWall and the other oPlayer
- Set each object’s sprite accordingly.
Player code
- In oPlayer, click “Add Event” and find the “Create” event.
- If not in GMS2: Find and add the “Execute a piece of code” action and drag it into the actions window, then double click it to get to the code editor.
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!