Your first game! Part 1: Moving Around

Posted on: March 1, 2015 at 5:42 pm,


So now lets go ahead and do the exact same thing for when we press the right arrow key.

Close the game window down once you’ve marvelled at your programming genius for long enough, and open up your object again by double clicking it in the resource tree. Go ahead and add a new event, but this time select Keyboard -> <Right>.

You should now end up with this:

Now with the “Right” event selected, drag in another paper icon “code” action, which should open a new blank code editor. This time we’re adding the code:

image_angle = image_angle - 2;

Or in otherwords, image_angle equals itself MINUS 2.

This will cause our spaceship to rotate in the opposite direction, which is exactly what we want!

Now you could run this again to test if you like, but now let’s look at making our spaceship actually fly forwards when we press up.

You might have guessed it, but lets add an event for the Up arrow key! Add a new event and select Keyboard -> <Up>. Just as before, drag in a code action and open up the code editor.

Now we add the following line of code:


your font will be smaller than this by default, I made mine larger for clarity!

You’ll notice that as you finish typing “motion_add” it turns yellow. This is because GameMaker recognizes this as a “function” or “command”. You’ll have previously noticed that “image_angle” turns red, and this is because GameMaker recognizes image_angle as a built in “value” of an object. Numbers and strings will also turn red, but more on that in another tutorial.

These yellow commands allow us to do various complex things in gamemaker, while it still ultimately boils down to changing numbers around, these commands effectively allow us to change a lot of numbers at once! This time we’re giving our object a given speed in a given direction.

Whenever you use the line “motion add” you need to pass along two parameters in brackets so that GameMaker knows what to do. Some commands do not need any parameters, some need many, but all of them generally require you to open and close a pair of brackets. (for example, the line to exit a game in GML is “game_end();”) When you need to pass along multiple parameters, you must seperate them using commas. All commands and functions in GameMaker can be found in the GML reference by pressing the F1 key, with detailed instructions on their useage and parameters.

The first parameter we pass to “motion_add” is the direction we want to add the motion. This direction is to going to be our image_angle value that we manipulated earlier to make our player face the correct direction. The second parameter we pass is the speed that we want to add in that direction. Now 0.2 may seem like a very small value, but remember that this speed is going to be added every single frame that we are holding down the forward key! This will cause us to accelerate at 0.2 pixels per frame, per frame! At 60 fps we can very quickly hit high speeds this way!

So after writing this line we need to do something to prevent our speed from going too high in any given direction. To do this we’re going to compare our current speed to a given number (whatever we want the maximum speed to be) and fix our speed at that number if we would go over it.

The way to do a comparison or ask a question in GML script is to use an “if” statement. An if statement works by providing a “condition” in brackets, GameMaker will then analyse that condition when the code runs and if that condition is true it will carry out the code after the brackets. Otherwise, that code gets skipped.

if (speed &gt; 5) speed = 5;

Or in otherwords, IF our speed is greater than 5, THEN make our speed equal 5.

So our final code for this action should look something like this:

Click the green tick to save this code to the action.

Now there’s one last thing we need to take care of. Currently our spaceship can simply flight outside of our game room never to be seen again. In Asteroids, the spaceship would simply wrap around the screen, so let’s imitate that behaviour. Instead of coding this one ourselves, this is a good time to demonstrate how the other drag and drop actions work and where they can sometimes be helpful to do things quickly.

Go ahead and add a new event, this one is the “Outside Room” event and it is found under “Other”:

This event is triggered whenever our object’s position is no longer inside the game area. Now while there is a code function to allow us to wrap our object around the screen, it is simpler and faster for us to just use the drag and drop action that is set up to do this for us. You can find the “wrap” action in the move tab of the actions menu. It has this icon:

with the outside room event selected, go ahead and drag this action into the actions section. Then select “in both directions” and click ok.

…and that’s it! This action will cause our player object to wrap around the screen whenever he leaves horizontally or vertically!

If you run the game now your spaceship will be able to fly around the screen just as it would in Asteroids, able to turn and behave in the same way.


Congratulations! You made your first fully interactive player object! In the next part we’ll look at making this a bit fancier and see if we can make our spaceship fire a weapon!




Pages: 1 2 3 4