Hey there folks! In this tutorial we’re going to create a simple platformer in GameMaker: Studio, using GML (Game Maker Language). This is for beginners who want to learn GML. Without further ado, let’s start.
Make two 32×32 box sprites. Color them as you like. One will be spr_player (for the player) and the other spr_block (which will act as the walls/floor).
Now make objects for them. When you create the obj_block, tick the Solid option.
Place the objects in a room and make a level.
Now starts the fun. Open your obj_player and add the Create event. Add an Execute Code action from the control drop-down menu.
Add this code:
gravity = 1;
So now our player object has gravity and will stick to the ground (as long as it is there).
Add a collision with obj_block event and add this code:
Collision with obj_block:
vspeed = 0;
This will stop the player from falling (due to gravity) when it collides with the block object, by turning its vertical speed (vspeed) to 0.
Now, add a Step event. Before adding any code in there, take a look at this piece of code.
if (keyboard_check(vk_left)) hspeed = -3;
if (keyboard_check(vk_right)) hspeed = 3;
hspeed = 0;
If you’re not familiar with coding, this might seem a bit confusing. But it is actually very simple.
keyboard_check() is used to check if a key is being pressed on the keyboard, and hspeed is the horizontal movement speed of the object. So, we’re asking the software something…
hey, if on keyboard left key is pressed, set horizontal speed to -3.
if on keyboard right key is pressed, set horizontal speed to 3.
just set horizontal speed to 0.
So basically that’s what the code does. It checks if we’ve pressed the left key, then changes hspeed to -3, so the object moves to the left. If left key is not pressed, as an alternative it checks for the right key and turns hspeed to 3 so the player moves to the right. But if the right key too is not pressed, it just changes hspeed to 0, causing the player to stop.
So, we’re putting a condition (using “if ()”) and then changing hspeed if the condition turns out true.
Simple, isn’t it?
Now, look at this code:
if (keyboard_check(vk_left) and place_free(x – 3, y)) hspeed = -3;
if (keyboard_check(vk_right) and place_free(x + 3, y)) hspeed = 3;
hspeed = 0;
What has changed? Yes, there is one more condition, after the and. So now that there are two conditions, the hspeed will be changed only if both of them turn out true. But what does the second condition mean?
The place_free() function is used to check if the specified place is free from any solid objects. So, we’re checking if there is no solid object (here, obj_block) before us before we move.
The x is the horizontal position of the object, and y is its vertical position.
Now copy the code above and add it into your Step event.
If you want to, you can try playing your game. You’ll see that the player can move around.
Let’s make it jump now!
Open your Step event once more and append this code:
if (keyboard_check_pressed(vk_up) and not place_free(x, y + 1)) vspeed = -13;
This checks for 2 conditions and if they’re true, sets vertical speed (vspeed) to -13, so that the player will make a jump. Now let me explain to you the 2 conditions used.
The first condition is similar to the previous keyboard_check but only remains true when the key is initially pressed. On the other hand, keyboard_check remains true until the key is held. So, the player will not be able to jump while holding the up key.
The second condition is different from the previous one because of the keyword not. If the condition place_free has to turn true, it will instead turn to false and vice-versa with the keyword not. So, here we want to see if there is a block under the player. This is to ensure that the player only jumps when it is on the ground and not in midair.
Now, your Step event code should look like this:
Now let’s make the level restart when the player falls down. This is fairly simple – just add this code into your Step event:
if (y > room_height) room_restart();
This checks if the player’s vertical position (y) is greater than the room’s height, which implies that it has fallen below the boundaries of the room, and restarts the room.
Now you can play the game and jump around with your player.
The movement speed I used was 3, the jump speed I used was 13, and the gravity I used was 1. You can try playing with these values to get your intended results.
If you want to use the WASD keys, use the keyboard_check() condition this way:
keyboard_check(ord(“A”)) checks for A key
keyboard_check(ord(“S”)) checks for S key
and so on…
You can download my example here. Just import it into GameMaker: Studio.
Need some help, or have any suggestions? Comment below, or contact me through Facebook or e-mail: email@example.com.
Have a great day!