In this lesson we:
On GitHub Desktop, click File | New Repository.
With the new repository selected, create a branch with Branch | New Branch.
Select Publish.
On the GitHub website, add any potential collaborators by selecting Settings | Collaborators | Add people.
In GameMaker select New | Game | Blank Game.
Save in your new GitHub repository, usually C:\Users\[You]\Documents\GitHub\[Your Repository]
On GitHub Desktop, type "Initial commit" for the commit summary and press Commit. This will baseline the repository with a blank GameMaker project and allow us to only capture the meaningful changes in our lesson commit.
Create an obj_character and obj_wall with a circle and square sprite respectively.
There are several ways to do this from more complicated (implementing fricton) to a more simple "on or off" movement. In this case we will go with the later.
We will use obj_character's Step event since it gives us flexibility in the implementation and runs every step.
GameMaker objects come with built in variables hspeed and vspeed which specify the speed on the horizontal and vertical axis. Following the GameMaker coordinate system, positive hspeed is to the right, and positive vspeed is down.
The intent is to move left or right when the respective keyboard key is pressed, otherwise to stop. We begin all code with writing our intent into a comment.
// Move left or right when the respective keyboard key is pressed, otherwise stop
How do we implement this intent? "Move left or right" can be controlled with the built-in variable hspeed. "when" indicates a conditional, and for this intent, we have positive cases "the respective keyboard key is pressed," and the negative case "otherwise stop".
For this conditional, let's utilize the if-else statement we learned. By chaining multiple if's and else's together, we can check for each condition and absence of all conditions.
Lets expand on our intent with some GML syntax:
// Move left or right when the respective keyboard key is pressed, otherwise stop
if keyboard_check(ord("A")) {
// Move left
}
else if keyboard_check(ord("D")) {
// Move right
}
else {
// Stop moving
}
What's keyboard_check(ord("A"))? keyboard_check() is a GameMaker function which will return true when the key is held down, otherwise false. The parameter, or argument, for the function, is a unicode value. We can use the function ord() to return a unicode value from a provided capital character string.
Now that we have our structure in place with the conditional, lets fill out each action utilizing the built-in hspeed variable we learned. We will create a local variable (with var) to avoid hard coding and to give our value a descriptive name.
var move_speed = 2
// Move left or right when the respective keyboard key is pressed, otherwise stop
if keyboard_check(ord("A")) {
// Move left
hspeed = -move_speed
}
else if keyboard_check(ord("D")) {
// Move right
hspeed = move_speed
}
else {
// Stop moving
hspeed = 0
}
Done! Now our platformer character should move left and right.
Again, there are multiple ways to accomplish this task with varying complexity. We will leverage the built-in vspeed variable that we already learn and keep things simple.
First, lets start with the intent:
// Jump when the respective keyboard key is pressed
Again, we have a conditional, so lets implement that with if.
// Jump when the respective keyboard key is pressed
if keyboard_check(vk_space) {
// Jump
}
This time we did not use the ord() function to calculate the appropriate unicode value for the parameter. This is because GameMaker has a built in set of "virtual keyboard" constants for us to reference when it is not a single capital character. vk_space coorelates with the space key.
Jumping is simple with vspeed, we will have the action set it to a large negative value, again using local varaibles to describe values:
var jump_inital_speed = -6
// Jump when the respective keyboard key is pressed
if keyboard_check(vk_space) {
// Jump
vspeed = jump_inital_speed
}
Now jumping would not be much fun if you never came back down, which is why we still need to add gravity. In this case, lets leverage the vspeed we are already working with. Always start with intent.
// Apply gravity while in air
For the conditional lets use an if. We will need a way to determine if the character is in the air. Lets use GameMaker collision functions, in this case place_meeting(). place_meeting() takes the current object's sprite, and temporarily moves it to the specified x and y coordinates to see if there would be an collision there.
So filling in the parameters to check ground below us:
// Apply gravity while in air
if not place_meeting(x, y + 1, obj_wall) {
// Apply gravity
}
Why use y + 1? That is because we should not be overlapping the wall when we are standing on it. It is only when we move down by one pixel that the collision should actually occur, and then we know that there is a wall directly below us.
The action of applying gravity can simply be a slight increase of vspeed each step in the downward direction.
var gravity_acceleration = 0.2
// Apply gravity while in air
if not place_meeting(x, y + 1, obj_wall) {
// Apply gravity
vspeed += gravity_acceleration
}
Notice we used the term "acceleration" and not "speed" to describe our value. This is because the speed is not fixed for gravity, but constantly changing by a fixed amount, acceleration. In the future we can add a max speed, just like how objects have a terminal velocity in real life.
Are we done? Not quite, if we test the game we will notice that the character can actually jump in air! How do we fix this? Lets revisit our jump code overall intent and add an extra conditional:
// Jump while not in air when the respective keyboard key is pressed
Does this extra "while not in air" sound familiar? It should! We just applied gravity while in air, and now we can leverage that previous conditional to add our extra check for jumping. Jumping is only possible when not applying gravity in air, so lets combine these two sections:
var gravity_acceleration = 0.2
var jump_inital_speed = -6
// Apply gravity while in air
if not place_meeting(x, y + 1, obj_wall) {
// Apply gravity
vspeed += gravity_acceleration
}
// Jump while not in air when the respective keyboard key is pressed
else if keyboard_check(vk_space) {
// Jump
vspeed = jump_inital_speed
}
Ta da! Now we are done.
Remember views? Well GameMaker has a built in way to have a view follow an object. Remember these tips:
In the room select Enable Viewports.
For Veiwport 0, select Visible
Set the Camera Properties to be half the room size and Viewport Properties. Set the Object Following as obj_character and the Horizontal Border as helf the camera width and Vertical Border as helf the camera height.
Now the view should scroll with the player, make sure you have platforms to jump on!
We end every lesson with a git commit and pull request. In GitHub Desktop, review each of the changed files from type to bottom, taking notes in the commit description of what intents you added. After you reviewed each changed files, reread your description and write an overarching summary for the commit. Finally select Commit to [Your Branch].
Finish with selecting Push to origin.
On the GitHub website, select Pull requests and then New pull request. Select [Your Branch] to compare against main and then Create pull request.
Usually we just have one commit, so the summary and description would match what we just commited, but due to the initial commit we have two. Combine all commit descriptions and write a new overarching summary.
To automatically close issues with a pull request, include the issue number following a keyword such as closes or fixes. Learn More