Game Maker For Beginners: Part I
Main Topics: Sprites, Objects, RoomsLevel: Total newb to Game Maker. Suitable for programming nubs, also.
Abstract: We're going to make a simple, one-level side-scrolling shoot 'em up using Game Maker and GML. In this first part we'll create a player ship and let you move it around. We'll also learn some basic concepts behind Game Maker and programming.IntroductionHaving played with Game Maker for awhile, I have to say, I'm pretty impressed. I can see why so many people use it! And I'd like to see more. I think it's a great way to make games and to learn how to make games. It puts you in the right frame of mind to move on to "real programming" if you ever wanted to.
That said, the official tutorials for Game Maker aren't very good. So let's try to make a better one!
Q: What is Game Maker? It's a game development tool for Windows, originally developed by Mark Overmars. Now owned by YoYoGames. The latest version is 7.0.
Q: What are the pros? It's fast to develop in, it's cheap ($20 for the full version), it's fairly stable, and it uses GML (Game Maker Language), a built-in scripting language that gives you a lot of control over your game. Compare this to Multimedia Fusion, which costs 10 times as much and uses a clunky "check-box" interface. (That said, MMF2 is probably more powerful overall, just not pound-for-pound, or dollar-for-dollar, as it were.)
Q: What are the cons? It's Windows-only, there are issues with Vista, and it is relatively slow when it comes to object handling. It has relatively poor support of arrays. There are also issues with scaling up your graphics (more on this later), but they can be worked around. Maybe one of the worst things about Game Maker is that it can easily be decompiled because the code is interpreted at runtime.
Let's Get Started!1. First, go to YoYoGames and
download GM7. Then install it. The "Lite" version is free to use, forever, and you can compile games (with a Game Maker logo on it), but is missing a lot of features, like [fill in].
If you're at all serious about using Game Maker, I'd recommend buying the Pro version. To go Pro, simply download Game Maker Lite and then upgrade it from inside.
2. Open up Game Maker! Here's the screen you should be greeted with:
First things first, IMMEDIATELY turn on "Advanced Mode," which you can access from the "File" menu. This enables a ton of stuff, like Scripts, which are very handy.
3. Look around. The left side of the screen is the "Resource Explorer," where all your resources are located. The right side of the screen is where you'll actually be doing most of your work on the game.
The Components of a Game Maker GameThere are three main parts to a Game Maker game:
1. Sprites - The graphics, essentially. Each sprite is a picture, or animation, that can be attached to an object.
2. Objects - The "actors" or "entities" in your game, that interact with one another.
3. Rooms - The "stage" on which your objects interact. The playfield. You might have a separate room for your title screen, and a separate room for each level of your game.
Create a RoomLet's start by creating a room, since we can't have a game without one. Right-click on the "Rooms" folder in the Resource Explorer and then "Create Room." Then double-click your newly created room. The Room Editor should open up on the right:
That's your room!
By default, the room is 640x480 pixels, but let's make it 320x240. Go to the "Settings" tab, and change the width and height to 320 and 240, respectively. While we're at it, let's name the room "rLevel1."
A helpful notation to use is to preface room names with "r." Likewise, we'll preface sprites with "s" and objects with "o." That makes code easier to interpret. It makes naming, faster, too.
I think we're good for now. Moving on!
Create a SpriteThis game is going to need a player ship, and that ship is going to need a sprite.
Right-click on the "Sprites" folder and "Create Sprite." Then double-click the newly created sprite to open up an empty "Sprite Properties" window. For name, put in "sPlayerShip" or something like that.
Then, hit "Edit Sprite" to open up the Sprite Editor, which should look like this:
The Sprite Editor lets you create frames of animation ("images") for your sprite and edit them. As you can see, there's already an image there waiting for you, a green 32x32 image.
Go to the "Transform" menu above, and click "Resize Canvas." Uncheck "Keep aspect ratio" and change the dimensions of the sprite to 32x16. Then hit "OK" and go back.
Double-click the Image to go into the Image Editor:
Now draw the sprite! I'll leave that up to you.
One thing to note is that Game Maker always makes the color of the lower-left pixel the transparent color. I don't know why it does this, but it does. It can be annoying at first, but you can always work around it.
Hit the check-mark when you're done. Now go to "Images" and you can "Crop" the image so that there isn't so much space wasted.
Create an ObjectNow that we have the sprite, let's make the object. Right-click "Objects" and "Create Object." Then open up the object in the Object Editor:
Here's where a lot of your work is going to go, so let's try to understand what we're seeing here (by the way, your Object Editor is not going to look like this when you start, it's going to be blank).
There are two main components to an object,
Events and
Actions. Events describe when actions are to be performed. Actions are what to do when an event occurs.
There are a few important events. Here they are:
The Create Event - This is called right when the object is created, or instantiated. Objects that are placed directly in the room will call this event when the room starts.
The Create event is a nice place to define variables that the object will be using over and over again, like "hitpoints" or "bullets" or what have you. It's where you want to set up your object.
The Step Event - You can think of a game like a big loop. And every time it goes through this loop, it updates itself. Mario may move one pixel forward, the screen will get redrawn, etc. So the Step event is called every time the game loops like this. It is arguably the most important event, since it's where all the action is going to happen.
The Draw Event is also called during the game loop, and it's where you define how your object is drawn. HOWEVER, you don't have to have this event. If you don't, Game Maker will draw the object with whatever the current sprite is.
Let's start by renaming the object "oPlayerShip" and changing its sprite to "sPlayerShip."
Set It Up - Create EventOkay, let's get this bad boy movin'! We're going to set up some variables we think we'll need and then make it so that the player can control it.
Click "Add Event" and then add a Create event. Then, on the far-right, select the control tab. Game Maker has all these built-in actions that you can use, but they are unwieldy, in my opinion, and hard to understand. Instead, let's just use one action, the "Execute Code" action (
). This action lets you write your own code, in GML.
Drag that over to the Actions area and then double-click it to open up the GML editor.
Alright, so let's think about what attributes our ship is going to need to have. Well, for now, let's say that we want the ship to have 3 levels of shooting power. It will collect power-ups and then get stronger, but only up to 3 levels. To keep track of that, we'll create 2 variables, powerUp and powerUpMax.
When you define a variable in Game Maker, you have to give it a starting value (i.e. you have to "initialize" it. For powerUp and powerUpMax, let's define them like this:
powerUp = 0;
powerUpMax = 3;
The ship will start at the lowest level, and it will be able to achieve a maximum level of 3.
As you can see, I ended each line with a semi-colon. In Game Maker, this is not necessary, but it's a good practice. In real programming languages like C, C++, Java, etc., they are mandatory.
Programmers: as you probably have guessed, there is no need to declare a variable's type in GML. All variables are considered doubles.
That's good for now!
More on Objects and VariablesAn
object is actually more of a definition, rather than a "thing" itself. The "things" are called
instances. For example, "Human Being" might be the object, and then "Derek" would be an instance of that object. Right now we're just defining what an oPlayerShip is. We haven't actually created one.
Variables always have
scope, which describes where they will be relevant. For example, a variable defined within an object has a scope within that object. Another object will not recognize what a "powerUp" is, unless you define a "powerUp" variable in that object, too. And then those two powerUp variables will be separate.
As you might have guessed, two instances of an object will have their own values for their variables. Two instances of oPlayerShip can have different powerUp values, for example.
Make It Move - Step EventWe set up a couple variables. Now create a Step event. Again, drag an Execute Code action (
) into the Actions area, and open it up.
Let's pop in this code and take a look at it:
// Controls
if (keyboard_check(vk_up))
{
y -= 2;
}
if (keyboard_check(vk_down))
{
y += 2;
}
if (keyboard_check(vk_left))
{
x -= 2;
}
if (keyboard_check(vk_right))
{
x += 2;
}
What this code does is it lets us move the ship in 8 directions. It uses the built-in
keyboard_check() function to detect key presses, and then it moves the ship in the right direction.
"x += 1" is shorthand for "x = x + 1" and is equivalent to adding one to variable x. This also works for -, *, and /.
Let's think about it in terms of the game loop. What this is actually doing is checking for keypresses each time through the loop, and if you are holding a key down, it will move the ship 2 pixels in that direction.
vk_up,
vk_down,
vk_left, and
vk_right are built-in
global variables whose values correspond to the keyboard key values. That's why they're brown colored.
x and
y are built-in variables that every object has. That's why they're blue. They define where the object's position is on the screen.
"// Controls" is a comment. Comments are not read by Game Maker, but they exist to make your code easier to understand. If you put "//" somewhere, everything on the line after it will be a comment.
On If Statements and FunctionsAn
if statement is a very useful "expression" in programming. It basically lets you say "if something is true, then do this." e.g. "If the player is hit by a missile, reduce his hitpoints by 10." The format is:
if (condition)
{
statement;
}
These are also acceptable, so long as the action is only one line long:
if (condition) statement;
if (condition)
statement;
You can extend an if statement to an "if-else" statement. Which basically means, "if this happens, do this, otherwise do this, or this, or this." If you wanted to make the ship only move in 4 directions instead of 8, a series of if-else statements would have done the trick:
// Controls
if (keyboard_check(vk_up))
{
y -= 2;
}
else if (keyboard_check(vk_down))
{
y += 2;
}
else if (keyboard_check(vk_left))
{
x -= 2;
}
else if (keyboard_check(vk_right))
{
x += 2;
}
Functions are code blocks that generally perform a specialized action, given arguments, and then return a value.
Arguments are just the inputs to the function. A function can have one, many, or no arguments. And it can return something or nothing.
For example, the keyboard_press function takes in one argument, a key value, and then returns either
true, if the key is being pressed, or
false, if it isn't.
true and
false are values, also.
There are a lot of built-in functions in Game Maker that you'll need to learn, such as keyboard_press(). But you can also write your own. These are called "Scripts," and we'll learn about them later.
Change the SpeedIf you wanted to make your ship move faster, you could change those 2's to a higher value, like 3 or 4. But hey, let's make it a variable so that we can just change that variable instead of replacing the number each time.
Replace those 2's with "speedUp," and then go back to the Create event and define a "speedUp" variable like so:
speedUp = 2;
Now, we can just change the value of speedUp in the Create Event and it will change the speed of the ship. Also, if we wanted to, say, add speed power-ups to the game, we could do that easily.
Generally, when programming, you want to use variables for any values you might want to change later.
Note: I called the variable "speedUp" instead of "speed" because there's already a built-in
speed variable for objects that is used when you're using their built-in actions.
Add the Ship to the RoomAlright, now we have a ship that we can move around. Let's create an instance of the ship in the room. Open up "rLevel1," go to the "Objects" tab. "oPlayerShip" should be selected already, since it's our only object. Now click someone on the room, and it will create an instance of the object.
By default, Game Maker will snap your objects to a grid composed of 16x16 blocks. You can change this above, if you like.
While you're at it, click on the "Background" tab and change the background color to black. Your room should look something like this (I turned off the grid lines):
Now hit the green arrow at the very top to compile your game and run it. If everything worked right, you should be able to move your little ship around using the arrow keys.
Sweet, you just made a friggin' awesome art game that explores the inherent emptiness of human existence!
[Download the Source for this Tutorial]Next Up: We'll let the player shoot an enemy and make it explode!
[link]