Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411507 Posts in 69374 Topics- by 58429 Members - Latest Member: Alternalo

April 26, 2024, 02:31:54 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Game Maker For Beginners: Part I
Pages: [1] 2 3 ... 11
Print
Author Topic: Game Maker For Beginners: Part I  (Read 327319 times)
Derek
Bastich
Administrator
Level 10
******



View Profile WWW
« on: October 15, 2008, 04:36:26 PM »

Game Maker For Beginners: Part I

Main Topics: Sprites, Objects, Rooms

Level: 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.

Introduction

Having 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 Game

There 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 Room

Let'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."

Quote from: PROTIP
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 Sprite

This 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 Object

Now 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 Event

Okay, 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:

Code:
powerUp = 0;
powerUpMax = 3;

The ship will start at the lowest level, and it will be able to achieve a maximum level of 3.

Quote from: PROTIP
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.

Quote from: PROTIP
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 Variables

An 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 Event

We 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:

Code:
// 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.

Quote from: PROTIP
"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 Functions

An 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:

Code:
if (condition)
{
    statement;
}

These are also acceptable, so long as the action is only one line long:

Code:
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:

Code:
// 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 Speed

If 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:

Code:
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 Room

Alright, 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.

Quote from: PROTIP
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]
« Last Edit: October 01, 2009, 12:09:28 AM by Derek » Logged
ChevyRay
Guest
« Reply #1 on: October 15, 2008, 05:24:09 PM »

Nicely done, Derek.

Watch there be 25 side-scrolling shooters released after this is finished :D

It was a good little tutorial though Smiley
Logged
moi
Level 10
*****


DILF SANTA


View Profile WWW
« Reply #2 on: October 15, 2008, 05:28:18 PM »

Yeah, awesome.
The more I hear about this GM the more I think these two years of creating my arcade framework was a futile effort Huh?
But now at least, art games about the emptiness of human existence have become mainstream Evil
Logged

subsystems   subsystems   subsystems
ChevyRay
Guest
« Reply #3 on: October 15, 2008, 05:35:32 PM »

I guess it just depends on how much you enjoy programming :D I don't like doing all the system and framework stuff, I just like programming the game mechanics, effects, etc. themselves, which is why I use Game Maker. Also, the time I save from using it also gives me more time to pixel pretty graphics.
Logged
Tanner
Level 10
*****


MMPHM *GULP*


View Profile WWW
« Reply #4 on: October 15, 2008, 06:53:01 PM »

This is JUST what I needed!
Manhugs for Derek!
Logged

___
Vice President of Marketing, Romeo Pie Software
Level 10
*


View Profile
« Reply #5 on: October 15, 2008, 06:58:01 PM »

Isis some sorta multee medier fusion?

This is excellent, Derek.  Thanks Grin
Logged
Hideous
That's cool.
Level 10
*****


3D models are the best


View Profile WWW
« Reply #6 on: October 15, 2008, 08:45:45 PM »

Awesome! Can't wait for the rest Grin
Logged

GregWS
Level 10
*****


a module, repeatable in any direction and rotation


View Profile
« Reply #7 on: October 15, 2008, 09:27:55 PM »

Great to see you creating this for those that need it Derek!  Gentleman

Nice code you've got in there too, only slightly different from my own GML:
Quote
if(condition){
     statement;
}else if(condition){
     statement;
}
As someone who had a some coding experience in school before learning GML (and finishing high school and then forgetting almost all I knew about C and Java) I find it amazing just how messy and unreadable some peoples' code can be.  GML really is too easy on people sometimes.

Having played with Game Maker for awhile, I have to say, I'm pretty impressed.
Just our of curiousity, how long have you been messing around with it?

And I have to add:
Sweet, you just made a friggin' awesome art game that explores the inherent emptiness of human existence!
Stuff like this is why you and TIGS rock!  Kiss
Logged
Pishtaco
Level 10
*****


View Profile WWW
« Reply #8 on: October 15, 2008, 09:31:56 PM »

It would be great if you say something about how to set up a front end for the game, with, say, a splash and a level select screen. That was something I found tricky with GM.
Logged

ChevyRay
Guest
« Reply #9 on: October 15, 2008, 09:37:33 PM »

Quote
if(condition){
     statement;
}else if(condition){
     statement;
}
Always good to keep your assignments in parenthesis. I never use the else if condition, though. I prefer to just enclose another statement all the time, because it keeps everything more manageable, and makes for a cleaner structure.

And we can never have enough art game that explores the inherent emptiness of human existence!


Quote
It would be great if you say something about how to set up a front end for the game, with, say, a splash and a level select screen. That was something I found tricky with GM.
That belongs in a different tutorial. Can't make a tutorial about EVERYTHING, else it's too hard for beginners to pick out what they want to learn from the tutorial.

Maybe I'll see what I can do about a title/menu screen tutorial.
Logged
Hideous
That's cool.
Level 10
*****


3D models are the best


View Profile WWW
« Reply #10 on: October 15, 2008, 09:52:52 PM »

Quote
if(condition){
     statement;
}else if(condition){
     statement;
}

Please, never ever format your code like this if someone else is going to read it guys. It's hard to read and it's just cluttered.
Logged

Inane
TIGSource Editor
Level 10
******


Arsenic for the Art Forum


View Profile WWW
« Reply #11 on: October 15, 2008, 09:55:03 PM »

I like
Code:
if ()
{
   if ()
   {
   }
}
else if ()
{

}

It's easy to read Well, hello there!
Logged

real art looks like the mona lisa or a halo poster and is about being old or having your wife die and sometimes the level goes in reverse
GregWS
Level 10
*****


a module, repeatable in any direction and rotation


View Profile
« Reply #12 on: October 15, 2008, 09:57:24 PM »

I don't know, I still think mine is pretty easy to comprehend, and depending on the complexity of my code, I'd have more spaces in between those lines.  I've seen much worse.
Logged
ChevyRay
Guest
« Reply #13 on: October 15, 2008, 10:05:47 PM »

I like
Code:
if ()
{
   if ()
   {
   }
}
else if ()
{

}
It's easy to read Well, hello there!
Not for me. Everything has to be tight for me to make sense of it. I can't keep things organized when it's like that, and I have a hard time telling what's in which set of brackets Tongue
Logged
Xion
Pixelhead
Level 10
******



View Profile WWW
« Reply #14 on: October 15, 2008, 10:29:11 PM »

so wait, how else do you do it?
Logged

Derek
Bastich
Administrator
Level 10
******



View Profile WWW
« Reply #15 on: October 15, 2008, 10:43:11 PM »

Woot!  Glad it's helpful.  It was fun to write. Smiley

Just our of curiousity, how long have you been messing around with it?

Around 6 months or so, I think?

I prefer Inane's method.  It shows how the statements are nested the best, imo.  Especially in a nice editor like Notepad++.
Logged
ChevyRay
Guest
« Reply #16 on: October 15, 2008, 11:44:43 PM »

Only recently did I really learn that EVERYBODY FUCKIN' ELSE codes like that Sad now I gotta go and try to reform a bad habit. It's gonna be a dizzy while.
Logged
GregWS
Level 10
*****


a module, repeatable in any direction and rotation


View Profile
« Reply #17 on: October 15, 2008, 11:53:05 PM »

Is having tight code such a bad habit though?  I learned to code the way I did at school from a very good teacher who I still have a lot of respect for; I think it's just a different approach.
Logged
ChevyRay
Guest
« Reply #18 on: October 16, 2008, 12:03:59 AM »

It is when other people want to read it, dude. It's just that the above approach is more commonly used, and like Derek said, it's easier to see opening and closing brackets/tags in code editors such as Notepad++ when the indentation of said markings line up.

Also better if you plan on getting a job coding, since you'll probably not be the only one looking at your code.
Logged
ஒழுக்கின்மை (Paul Eres)
Level 10
*****


Also known as रिंकू.


View Profile WWW
« Reply #19 on: October 16, 2008, 12:35:07 AM »

The brackets are optional.

if condition
   statement

works just as well as

if condition
{ statement }

you only need brackets if there is more than one statement after an if.

Important note: because this is an interpreted language, excess things like extra brackets and extra parenthesis will actually make your game go *slower*. So it's best not to use the unneeded ones.
Logged

Pages: [1] 2 3 ... 11
Print
Jump to:  

Theme orange-lt created by panic