Make Your First Platformer in GameMaker


Let's create a game together! It's going to be a simple platform game where you have to dodge spikes and reach the flag!

This will be the end result:

Even though it's simple, it'll be a great introduction to GameMaker! You can also follow along the video:

Embed of the video here

We'll start by creating a new blank project. Once you create your project there's quite a lot to explore, but don't worry! For now focus on the Asset Browser on the right side of the screen:

undefined

This is where all the assets of your game will be, like sprites, sounds, and objects. For this project, we will only use sprites, objects, and rooms.

You can delete the unused folders if you want, which is what I'm going to do here.

undefined

Working with Sprites

First, let's talk about sprites. Sprites are the images in your game. They represent your character, enemies, items, and even the background.

Let's create a sprite! Right-click on the empty space, go to Assets, select "Create Sprite" and name it sPlayer. You can name it whatever you want, but I usually go with sPlayer for the player sprite.

I recommend that you create your own sprite, but for this tutorial, I already prepared mine. I'll click "Import" and import my sprites into the game:

undefined

undefined

Next, let's create more sprites. Right-click on the sprites folder and select "Create -> Sprite". I'll create a sprite for each object of the game:

undefined

Working with Objects

Now let's move on to objects! In objects you define the behavior and interactions of your sprites.

We'll create one object for each sprite we made. Right-click on the Objects folder and select "Create -> Object".

Name this oPlayer and assign the player sprite to this object.

undefined

The area on the right is the event window where you write the code for your object. This is where you decide what happens when the character touches an enemy, picks up an item, advances to the next level, and more!

For now, let’s just create the other objects:

undefined

To each object we will assign its corresponding sprite.

Working with Rooms

Now, let's set up our game level. Open the “Rooms” folder and double-click Room1. This will show a black grid which is your level:

undefined

The default room size is quite large, so let's change it to 320 by 180 in the Inspector, on the bottom left corner of the screen:

undefined

This only appears when you have the room (Room1) selected in the Asset Browser.

Adjust the zoom and place your objects on the screen. This is done by selecting an object from the Asset Browser, and holding "Alt" (or Option on Mac) to paint them in the level. Or, you can drag the objects one-by-one.

The objects snap to the grid, which is too large, so change the grid size to 16 by 16 pixels:

undefined

Now place the player, ground, some spikes, and the flag.

undefined

Writing Code for Player Movement

Now we need to write some code. Add a Create Event to oPlayer:

undefined

If you get a choice between GML Code and GML Visual on this step, choose GML Code.

This code runs as soon as the object is created, which is when the game starts.

We'll write a function to set the game window size to 720p:

window_set_size(1280, 720);

Next, create two variables for horizontal and vertical speed, both set to zero:

xsp = 0;
ysp = 0;

Add a Step Event to handle player movement, this event runs every frame.

undefined

Let's write some code to give our character gravity by applying a downward vertical force. We'll also make sure the player remains stationary when no keys are pressed:

ysp += 0.1
xsp = 0

Now let's add the movement code, checking if the left or right key is being pressed and manipulating the horizontal speed of our object:

if keyboard_check(vk_left)
{
        xsp = -1
}

if keyboard_check(vk_right)
{
        xsp = +1
}

Let's make our character jump! We'll check if the character is on the ground using place_meeting.

In this code snippet, when the player is on the ground, their vertical speed is zero and if the player presses the up key, they jump:

if place_meeting(x, y+1, oSolid)
{
        ysp = 0
        if keyboard_check(vk_up)
        {
                ysp = -2        
        }
}

Lastly, we will enable the player to move and interact with oSolid:

move_and_collide(xsp, ysp, oSolid)

Read the move_and_collide() manual page for more info.

Testing and Adding Levels

Click "Play" or press F5 to test the game. You should be able to move the character:

However, the character still doesn't die when touching the spikes, or proceed to the next level! We need to add collision detection with the flag and the spikes.

Include the following code in the Step event, it will make the character transition to the next room when touching oFlag and reset the current room if it touches oSpike:

if (place_meeting(x, y, oFlag))
{
    room_goto_next();
}
if (place_meeting(x, y, oSpike))
{
    room_restart();
}

Run the game, and you can now lose from the spikes, or reach the flag to win.

However, when you win, the game crashes: this happens because there is no next level to move to.

So, all that's left now is to create more levels! But before that, let's try to make the game a bit prettier.

Creating the Background

To make the game look better, I will create a background sprite. It will be a simple 32x32 sprite:

undefined

Assign this sprite to the room's background layer and make it repeat horizontally and vertically. You can also change its color and add effects to make it look nicer:

Adding More Levels

Right-click on the room and duplicate it to create a new level:

undefined

Rename it to Room2. Add more spikes, move the flag, and change the background color. Just do the same thing with a few more rooms and you should have a complete game!

Conclusion

This is a very simple game, made to introduce you to GameMaker. Many things here weren't the most optimized, but they were done in the simplest, most understandable way.

Keep working on your projects, and feel free to expand this game with animations, new mechanics, and more!

What’s next: You’ll notice the game still crashes on finishing the last level. What if you added a game end screen with a button to take you back to Room1? (Tip: Repurpose the “Play” button created in that video to make a “Play again” button!)

Happy GameMaking!