Open GameMaker, select New > Game > Blank Game > Enter a name into the textbox > Let's go!.
On the right-hand side, you will see a number of folders.
Righ-click on the Sprites folder, select Create and from the sub-menu, select Sprite.
In the new window, rename the sprite as "sprite_player" in the upper-left textbox.
In the window that shows up, select Edit Image to bein drawing your sprite.
Using the tools and colours available draw your game's main character
BONUS: By right-clicking on the frames seen at the top, you can create an animation by making small changes to the sprite. In my example, I have the basic sprite set as the first frame, the left foot lifted on the second frame, the first frame again as the third frame and the right foot lifted for the fourth frame.
When played after each other, this gives the illusion of the character walking.
You can adjust the FPS of the animation to something sensible like 8 by double-clicking on the sprite in the sprite folder and modifying the FPS at the top.
You should back your game up as frequently as you can, and at the end of every lesson.
To back your game up, click File > Export Project > YYZ
Save one copy to your OneDrive/HWB, and another copy to your Desktop.
If you can't load your game as normal in the next lesson, open the File Explorer (the yellow folder in the taskbar at the bottom of the screen), click on your Desktop and OneDrive folders and search for "YYZ".
Checking the date will help you ensure it is the correct backup.
When you double-click the file, it will ask you to save this backup somewhere before you can carry on working on it.
NOW BACK YOUR GAME UP!
Right-click on the Objects folder and select Create, and then select Object from the sub-menu.
In the new window on the left, change the object's name from Object1 to object_player - (you can access the "_" symbol by holding shift and pressing the key next-door to the number 0.
To set the sprite click the No sprite button, then select the sprites folder and then select your sprite as shown.
Next, press the Add Event button and select Create.
Any code placed in the Create box runs as soon as the object is added to the room when playing the game.
If a message comes up asking which type of code to use, select GML code.
Slide the screen over to the Create code entry box - you can do this by using two fingers on your laptop's tracking pad - and enter the following code:
playerSpeed = 4
You can set the number as low as 1 or as high as you like. 4 is a reasonable amount though!
Find the Add event button again, select Step and then Step.
Any code in this section runs on every frame. By default, your game will run at 60 FPS, which means this will happen 60 times a second.
In the Step code editor, enter the following:
var xMovement = keyboard_check(vk_right)-keyboard_check(vk_left)
var yMovement = keyboard_check(vk_down)-keyboard_check(vk_up)
x+=xMovement*playerSpeed
y+=yMovement*playerSpeed
This code will create two variables called xMovement and yMovement.
In the case of xMovement, it will check if the right key is being pressed and check if the left key is being pressed. Because the computer accepts something being true as the number 1 and false as the number 0, xMovement will be 1 if the right key is being pressed alone, -1 if the left key is being pressed alone and 0 if both or neither key is pressed.
The x+= and y+= lines of code change the x and y positions of the player when the game is played.
NOW BACK YOUR GAME UP!
On the right-hand side of the window, expand the Rooms folder by clicking the arrow.
Double-click Room1 to open the first level which should look like the image below.
This looks a bit bland. To change the colour of the game's background, select the background layer on the right then, click on the black rectangle lower down to select a background colour. I opted for green!
Bonus
You can also set a sprite as the background image. Right-click the Sprite folder, create a new sprite and give it an appropriate name such as sprite_bg. Edit the image in a way that would look good when repeated as in my example shown on the left.
Next, double-click Room1 again and click on the background layer. Then, click on the box that currently says No Sprite.
From there, select your background sprite. You'll notice your background will only be visible as one square in the room's top corner. To fix this, tick the Horizontal Tile and Vertical tile boxes on the Background layer's options.
To place the player in the game, select the Instance layer on the left-hand side. This is where all objects will be placed from now own.
Then, drag your player object from the Objects folder on the side and onto the map.
To test your game, press the play button at the top of the screen and wait. Once the game is built, check that your player moves in all directions correctly when the arrow keys are used.
If not, scroll back up and re-check your steps.
NOW BACK YOUR GAME UP!
Create a new sprite for your wall and name it "spr_wall"
Create a new object for your wall and name it "obj_wall"
Double-click the Room1 level in the Rooms folder and drag your walls into the room.
You can stretch the walls by pulling at the sides when each wall is selected.
Bonus:
If you'd like your wall sprite to repeat as in the example shown, after drawing the sprite double-click on it in the Sprites folder, expand the "Nine Slice" section and copy the settings below.
Moving the pink guide lines selects which part of the image will repeat.
Locate your player object in the objects folder, double-click on it, and select the step code by double-clicking on Step.
Remove the x+xMovement *playerSpeed and y+yMovement*playerspeed lines of code and replace them with the following line of code:
move_and_collide(xMovement*playerSpeed, yMovement*playerSpeed, obj_wall)
Press the play symbol to run your game and check that your player can't walk through walls. If something goes wrong - check through the code above
Bonus:
If you have animated your player sprite you might find it a bit annoying that your player is still animated when standing still.
To fix that, add the following code to the bottom of the player's Step code.
if xMovement == 0 and yMovement ==0{
image_speed = 0
image_index = 0
}
else{
image_speed = 1
}
This code checks if the player should be moving on the Y or X axes - if not, the image_speed is set to zero - meaning the sprite stops animating - and the image index is set to 0, the first frame in the animation. If the player should be moving, the image_speed is set to 1, which is the default speed of the animation).
NOW BACK YOUR GAME UP!
To set a camera that follows the player around the maze up, double-click the Room1 level in the Rooms folder on the right.
In the settings panel on the left-hand side of the screen, expand the Viewports and Cameras option.
Tick the box to Enable Viewports, expand the Viewport 0 panel and tick the Visible box.
Then, set the Camera Properties' Width to 1024 and Height to 768.
Then, set the Viewport Properties' Width to 1024 and Height to 768.
Scroll down to the Object Following settings, click the No Object button and select your player's object.
In the Horizontal Border and Vertical Border settings, set them to 512 and 384.
NOW BACK YOUR GAME UP!
Now that the camera follows the player, rather than showing the whole level at a time, you can adjust the size of the room to have a larger canvas for designing your first level.
To do this, locate Room1 in the Rooms folder, double-click on it and in the Width and Height settings, set it to an appropriate size (measured in pixels).
I've chosen 2000 x 2000.
Making sure the Instances layer is selected (as seen in the top-left), begin designing your first level, following your plan if you have one. Make sure there is enough room for your player to move between walls and you have a few dead ends to catch players out!
Create and appropriately name the sprite for your enemy. If your sprite will change the direction it faces, draw it facing the right.
There are two sections below on setting up your enemy's movement; directional, which will make the enemy move either left and right or up and down, switching direction when they hit a wall and pathfinding which will attempt to move around obstacles to get to your player.
Create an object for your enemy named object_enemy and add the following code to a new Create event by Clicking Add Event > Create.
myDirection = 1
mySpeed = 4
These variables will be used to determine the direction the enemy is facing and the speed in pixels it will move per second.
Create a new Step event by click New Event and selecting Step > Step. In there, enter the following code:
if !place_meeting(x+myDirection*mySpeed,y,obj_wall){
x+= myDirection*mySpeed
}
else{
myDirection *= -1
}
This code will first of all check if it is touching the wall. If it is not, the x position of the player is moved in the current direction at the set speed. If it is touching the wall the direction will flip. This works because 1 times -1 = -1 and -1 times -1 = 1. By multiplying the direction by -1, the direction flips from either right (1) to left (-1) or left(-1) to right(1).
You can modify this to go up and down by using the following code instead:
if !place_meeting(x,y+myDirection*mySpeed,obj_wall){
y+= myDirection*mySpeed
}
else{
myDirection *= -1
}
Create and appropriately name an object for your enemy.
Add a Step event and add the following code:
mp_potential_step(object_player.x, object_player.y, 4, 0)
This code tells the the enemy to move towards the player's X location and Y location at a speed of 4 pixels per frame.
When you test the game, you will see that the enemy will move towards the player but it will move through walls.
To stop this from happening, double-click on the wall's object and tick the "Solid" option. This will make the wall a solid obstacle whenever pathfinding is used.
Add the following line to your player's Create code:
lives = 3
This sets GameMaker's built-in lives variable to the number of lives your player starts with.
Next, add a GUI draw event on the player by clicking Add Event > Draw > Draw GUI. The GUI is drawn over the game and is used for displaying information for the player. In the Draw GUI event add the following line:
draw_text(10,10, lives)
This will show the number of lives in the top corner.
Bonus
Alternatively, if you'd like to display an image instead follow this tutorial.
Add collision code to your player - this tells the game what to do if your player and enemy touch each other.
You can go down two routes here - to lose a life and go back to the start, use the following code in the new window:
lives -=1
x = xstart
y = ystart
other.x = other.xstart
other.y = other.ystart
This lowers your player's lives and moves the player and the enemy back to the start.
Alternatively, you could take some damage, and then wait a second or so before you can take damage again by using the following code:
if vulnerable = true{
lives -=1
alarm_set(0,60)
vulnerable = false
}
This takes a life away and sets an alarm - basically a timer - for 60 frames which, when the game is running at 60FPS as it should, takes one second.
Next, we need to add the vulnerable variable to the create code like so:
vulnerable = true
Then, you need to add what happens when the alarm is triggered like so:
vulnerable = true
Bonus
You might want your player to have to collect the weapon before they can use it. To do this, follow the steps below:
First, create a sprite for your weapon with a sensible name.
Next, add a variable to the player's create code called weaponcollected and set it to be false.
Then, create an object for the weapon and place it in the map.
Next, add a collision event with the weapon on your player and set weaponcollected to true when the gun is touched and add instance_destroy(other) to remove the weapon.
Change the Global Left Clicked code so that it checks if the weapon has been collected before allowing you to shoot, as shown below
Create a sprite for an item the player needs to collect.
Create an object for the collectable item and add a collision event with the player.
Add the following code, replacing the number with the number of points added.
score += 100
instance_destroy()
On the player object, add the following to the Create event:
score = 0
This sets the score to 0 at the start of the game
To display the score, find the player's Draw GUI code and add the following:
draw_text(10,50,score)