Your first game! Part 1: Moving Around

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


The Player Object

So this is the fun part where we actually create the game part of our game. Where we define how the player can interact with the game, and what the outcome of those interactions should be! Go ahead and right click on Objects and create a new object. I called mine “obj_player“.

When the object editor appears, be sure to set the sprite using the drop down menu to our spaceship sprite (spr_player). However you should avoid changing the default tickboxes. Make sure that “visible” is ticked and that “solid”, “persistant” and “uses physics” remain unticked. These can be useful options (well, except for Solid which is usually more trouble than it’s worth) but for now ignore them as they will only cause problems.

As I explained in the basics article, objects (and therefore, all of your game’s mechanics) work on the principle of events and actions. You establish that when event X happens, we carry out action Y. The first thing we’re going to do is make it so our spaceship can turn left when we press the left arrow key.

So right click in the Events section of the object editor, hit “Add Event” and select Keyboard -> <Left>.

be sure to select from the “keyboard” section and not the “key press” or “key release” sections. More on those events in another tutorial.

Now we need to associate an action with our left arrow key event. Because these tutorials are based around learning GML scripting, there is actually only one action we will typically use and that’s the “Execute a piece of code” action which is found in the control tab in the actions menu on the far right of the screen.

All we’re doing when we use this action, is instead of using one of the pre-built drag and drop actions in GameMaker we’re defining one of our own. We can write instructions using GML code for our game to carry out. In this case we’re going to write a line of code to rotate our player object’s sprite.

Go ahead and drag this little paper square icon into your actions section, this will then open the code editor.

So what on earth do we write in this magical, intimidating code editor? Well all we’re going to do is what a lot of the vast majority of code (and honestly the vast majority of programming) seems to boil down to, we’re going to take one number, and turn it into another one!

Precisely, we’re going to take our current direction, and adjust it by a small amount to the left.

Our sprite is drawn to the screen based on where our object is in the game (it’s x and y coordinate) and based on the “origin” of our sprite. But our object also contains a value that controls what angle to draw the sprite at. This value is called image_angle.

By manipulating this value we can make our spaceship draw it’s sprite facing different directions!

By default image_angle equals zero. Which is based on the assumption that it is facing to the right. Now as this number increases, the the image is drawn at an angle rotating anti clockwise at a corresponding number of degrees. If image angle were to equal 90, our spaceship would be facing straight up. 180 = left, 270 = down and finally 360 would be facing right again.

So all we need to do is increase image_angle in order to turn left, so the line of code we’re going to write is:

image_angle = image_angle + 2;

Or in otherwords, image_angle equals itself plus 2.

We end the line in a semi-colon as good programming practice to tell GameMaker where the line of code officially ends. I should also point out that the code ignores spaces between words. I could just as easily write:


And GameMaker would read it the same way. Adding spaces between words and commands in code just makes things easier on the eye and will help when reading your own code later.

So in the code editor you should have something that reads like this:

Go ahead and click the tick to close the code editor. You’ll notice that the “Left” event you created is now associated with one “Execute a piece of code” action. You can double click that action at any time to bring back the code editor and change your code.

For now though, let’s go and see our first line of code in action!

Open up your room again, and go to the objects tab. Select your player object from the drop down list and click anywhere in your room to add your spaceship. If you add too many, simply right click on them and hit delete.

Once you’ve done this you can press F5 or click the little green arrow in the top left of GM in order to compile and run your game! This might take a second or two depending on how fast your PC is, but when your game window appears, press the left arrow key to see your spaceship spin around!

Congratulations! You just wrote and executed your first line of GML code!

Pages: 1 2 3 4