FR

Réalités Parallèles

Ergonomie et conception de jeu vidéo

Gamemaker Studio Horizontal Shooter - Basics - Creating a player character

Creating the player object

We’ve made a sprite to represent the player. Let’s use it on an object that the players will be able to control.

On the objects folder, right click and create a new object. screenshot

Setting up the object basics

As usual, we’ll name the object following a convention

screenshot

First way of adding interactions

In the events column, you’re going to select what triggers an action. We want the player object to move left when the player presses left. To do this we can create an event from Keyboard > Left. This means as long as the Left arrow is pressed, the player object will continue doing whatever we assign to that input.

screenshot

Now you should have an event called “Left” in that column. Click on the control tab way on the right, then drag and drop the “Execute Code” block into the right “actions” area. This will allow you to write code, which will be executed whenever left is pressed. As soon as you release, it will automatically open a text editor on top of the object properties window, which should now look as below.

screenshot

Double click the “Execute a piece of code” item in the actions column if you need to re-open the text editor window. We’re now going to write our first bit of actual code. We’ll use 2 lines of code:

///move left

x = x - 4;

Now there is a lazy way to tell the computer to do exactly the same thing,

Instead of x = x - 4;

You can write x -= 4;

-= means update the existing value on the left side by substracting whatever is on the right side. If you wanted to add instead of substract, you can do so too by writing += instead too.

Adding the player object to the room

Double click the room your created earlier, and go to the objects tab.

In here, you can select the player object you just created, and add it to the room by clicking anywhere in it once.

screenshot

I put my player object somewhere in the middle for now to make it easier to test the controls in all directions.

screenshot

Practice

Create an event for the right key and make the player move towards the right.

Example solution

Event:

screenshot

Action:

screenshot

Coordinates in Game maker

Every object has a position defined by two coordinates x and y. In game maker, the origin of the axes (x=0, y=0) is the top left corner of the screen. The value of x defines an object’s horizontal position. The value of y defines an object’s vertical position. Points of the screen that are further away from the left edge of the screen have an increasingly higher x value. Points on the screen that are further down have an increasingly high y value.

axis with different examples of coordinates on it

Practice

Add events to the player object so that it moves up when the player presses the up key and down when the player presses the down key.

Tips:

Example solution:

Events:

screenshot

Code for moving up:

screenshot

Commenting properly

It’s good practice to add comments above each code block that does something, and on particularly complex lines of code.

Comments can be written with just // at the start of each line instead of ///.

The nice thing in game maker is that using /// on the first line of text will turn that comment into a title in the actions list. That can make it easier to find which code did what without opening it to check what’s inside.

As you can see, in my example, it shows “move left” instead of “Execute a piece of code” which is otherwise the default text.

screenshot

Posted by Cornelia on 2021-04-03. Last updated on 2021-03-05

Articles on similar topics

Game Maker Studio tutorials

Game development tutorial, Gamemaker development tutorials, Game Maker studio programming, Programming video games for beginners,

Gamemaker Studio Horizontal Shooter - Basics - Creating sprites

Game development tutorial, Gamemaker development tutorials, Game Maker studio programming, Programming video games for beginners,

Gamemaker Studio Horizontal Shooter - Basics - Creating a room

Game development tutorial, Gamemaker development tutorials, Game Maker studio programming, Programming video games for beginners,

Gamemaker Studio Horizontal Shooter - Basics - Prevent the player from leaving the screen

Game development tutorial, Gamemaker development tutorials, Game Maker studio programming, Programming video games for beginners,

Gamemaker Studio Horizontal Shooter - Basics - Scrolling backgrounds

Game development tutorial, Gamemaker development tutorials, Game Maker studio programming, Programming video games for beginners,

Game Maker Studio tutorials

Game development tutorial, Gamemaker development tutorials, Game Maker studio programming, Programming video games for beginners,