Game Maker Studio - Beginners Guide PDF
Game Maker Studio - Beginners Guide PDF
Game Maker Studio - Beginners Guide PDF
A Beginners Guide
To
GameMaker Studio
2014 Edition
Ben Tyers
Copyright © 2014 BEN TYERS
All rights reserved.
ISBN-13: 978-1497393837
ISBN-10: 1497393833
CONTENTS
i
Resources
Free Resources For Projects In This Book Are Available For Download At:
Http://www.GameMakerStudioBook.com/Resources
Chapter 1
Getting Started
BEN TYERS
Introduction
Gamemaker is an amazing piece of software that allows you to create games, ap-
plications and proof of concepts then export them to an array of different devices including
Windows, Windows 8 (Native , Javascript and Phone), iOS, Android, HTML5, Ubuntu
(linux), and Tizen (Javascript and Native) at the time of writing.
To start making games with GameMaker Studio you need little prior knowledge or skill.
Using the program’s built in game creation tool called Drag and Drop; you can create games
without writing a single line of code. As you get more proficient, you can start to learn the
scripting language called GameMaker Language (GML).
This chapter will prepare you better, ensuring you approach your game design and pro-
gramming in logical and methodical way, and answer some general questions about games,
game design and programming.
For the purpose of this book; references, drag and drop, and coding relate to the regis-
tered Windows GameMaker Studio Version.
To start off with you'll learn the basics of GameMaker (GM) using it's built in GUI
system known as Drag and Drop (D&D). This system allows you to create simple games
without the need for any programming or coding knowledge. As you work through this book
you'll learn how to replace this with the more flexible coding known as GML (Game Maker
Language). The first part of this book focuses primarily on D&D, and gradually introduces
GML as you work your way through.
You will build a game made up of several elements including sprites, sounds, objects,
events, rooms, and backgrounds. Scripts/coding (GML) and tilesets are covered later in the
book.
4
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Installation
Visit http://www.yoyogames.com/studio/download to download the trial version or to
buy the full version.
Instructions below.
5
BEN TYERS
Tour of GameMaker Studio:
Depending on which version of windows you’re using you can then start the program.
Upon starting GM for the first time, after entering your license code (if you bought a paid
version), you’ll be presented with a screen like that shown in Figure 1-3.
Note: The projects in this book assume you have the paid version of GameMaker Studio.
6
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
On the left you’ll see folders with names like Sprites, Objects and Rooms. This is the
asset area. When you create an asset it will be placed here.
The icons along the top are primarily used to create assets.
For example, clicking the create object icon, as shown in Figure 1_5:
After clicking this you’ll be presented the screen as shown below in Figure 1_6.
7
BEN TYERS
On the right you’ll see actions. These are used to make things happen, such as make an
object move or play a sound.
Clicking the ‘Add Event’ will bring up the following window as shown in Figure 1_7
below:
8
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
These events are used to test if something is happening, for example to check if the right
arrow key is pressed, as shown in Figure 1_8 below:
You can then use actions to make something happen when a key pressed, for example
make the object move to the right, as shown in figure 1_9.
9
BEN TYERS
10
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Objects
Objects are the main blood of GM. You use them in two ways, firstly to test if some-
thing is true, and secondly make things happen.
Display a sprite on screen.
Make a ball move.
Detect a key-press, mouse button or mouse movement.
Play a sound effect.
Create Artificial Intelligence (AI) for objects.
Detect collisions.
Change rooms.
Play background music.
Animate a sprite.
Display a message.
Respond to an event.
Change/Display the score/lives.
Draw something.
Create another object.
Create an effect.
Do / Create / change something else.
Throughout this book you'll gradually learn how to do all of the above, so I won't go into
any more detail here, but feel free to experiment with it as you go along.
Sprites
A sprite is basically a graphical element made up of one or more images that is displayed
on the screen. Sprites are displayed using objects. Examples of sprite graphics include:
Ball – Like the ball you'll create in the first part of this book that bounces around the
screen.
Key – Something you need to collect to open a door or go to the next level
Ghost – An enemy that follows you around and tries to kill you in a maze game.
It should be noted that objects and sprites are different. A sprite is an image that is dis-
played on the screen. An object is the tool to actually draw the sprite on the screen.
11
BEN TYERS
Events
An event, in simple terms, is something that happens. Events usually happen inside an
object. For the purposes of this book, I'll be focusing mainly on:
Create event (happens only once).
Key press.
Object hitting another object (called a collision event).
An alarm event (something happening after a set period of time).
A step event (something that occurs at regular intervals).
An object being destroyed.
Click a mouse button.
Draw event (draws the sprite, score, lives, text on the screen).
Events are then used to test, change or make something else happen, with an action:
Increase score.
Reduce lives.
Create a bullet.
Stop something.
Start something.
Change something.
Create something.
Move something.
Test something – like how many lives left or which level .
It's these interactions and changes that work together to create a game that interacts with
you, the player.
Using events and combining them with D&D allows you to manage, design and decide
what events happen if certain conditions are met. By combining the events and D&D in a
logical way, you can start to implement the design of your game. This can also be used as a
basis of an AI system to control objects not controlled by the player. Events will be dealt with
more later in the book.
Drag and Drop
Drag and Drop (D&D) is GameMaker's inbuilt GUI for creating games without any prior
programming knowledge. It allows for an easy way to create basic games. D&D also has its
GML (Game Maker Language) equivalent in code. As you get more advanced, you'll learn to
use a combination of D&D and GML, and in time move on to using just GML.
D&D is a great way to start developing games as the logic and approach is similar to
GML. As you progress through this book you'll learn most of the main functions of D&D and
be introduced to GML.
Using D&D you'll learn how to make things happen in response to another event, action,
or value, or occurrence.
For example:
Make a player move left if left cursor is clicked.
Decrease lives by 1 if player hits enemy.
Create a moving bullet if the fire button is pressed.
Make a ball bounce if it hits edge of screen.
Looking at the previous examples you'll see that each consists of two parts, part one
something happening, part two do something because of this. That's the basic of logical pro-
gramming, you're well on your way to becoming a great games programmer.
12
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
In this book you'll learn how to incorporate this into your design process. As you get more
advanced in D&D and coding you'll discover other logics, such as:
Do something if something isn't true.
Do something if (something and something else) is/isn't true.
13
BEN TYERS
Can I Sell My Game? How Do I Make Money?
Yes, you can sell your game. YoYo Games' Terms and Conditions for GM are pretty
relaxed and allow you to make and sell pretty much anything (except another Game Maker).
You don't have to acknowledge you used their software or pay any commission if you make
some or lots of money. Given that GM is so reasonably priced, you'd only have to sell a few
copies of your software before turning a profit.
With the ability to release your game on a variety of platforms, at time of writing – Win-
dows, Windows 8, Tizen Mac, HTML5, Android, iOS amd Ubuntu, the opportunity of
multiple revenue streams is available. You can for example release a demo version and charge
for the full version, or display on screen ads and earn ad revenue from each click or sale.
14
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Copyright
The following is brief outline only, as copyright laws vary from country to country. If in
any doubt contact a legal professional or the copyright, trademark or patent office of your
country. The following is not intended to be or replace professional legal advice.
Basically, if you didn't make something yourself or pay someone to make it for you, then
you don't own the copyright on it. A lot of people rip (copy) sprites and sounds from other
games to use in their own creations. Most major companies generally turn a blind eye if people
use this for home projects or educational reasons. If you start selling or otherwise start making
money, expect them to come knocking at your door.
Online there are a number of free repositories where you can get sounds, music and
sprites. Always check their terms & conditions or copyright policy before using.
If you're unsure, always email for permission before using.
15
BEN TYERS
How Do I Develop a Game?
The design process of any game is probably one of the most important steps in the devel-
opment process. Having a properly designed game speeds up the whole development process.
You may think that a simple game wouldn't need much planning, but as you'll find in Chapter
1, when you design a brick and ball game, having a well designed game makes the program-
ming of it much simpler. To start with you'll probably be designing and making your game by
yourself, so you won't need to consider team collaboration, but imagine what would happen
if you had a big team and no game design - it just wouldn't work. Having a well thought-out
plan will allow you to create resources for your game much more easily, set out rooms quickly
– as you'll know where everything needs to go, quickly track down bugs and errors, and easily
designate different parts of the process to other people. You will also know what events, con-
ditionals or interactions your game will require.
For a minute, consider the film industry. If you've ever watched one of those bonus DVDs
that come in some box sets, you would have seen something called a story board. Every scene
is planned out well in advance using a storyboard, like a comic, with each action or camera
angle in a different cell. This advance planning allows the actual filming process to be com-
pleted more accurately, and on time. Just imagine the chaos on set if no one knew where to
stand or where to point a camera?
I'm not saying you need to plan your games in as much detail as a film, just that planning
and design plays a very important part in the overall process.
It's also OK to ad-lib now and again if a great idea pops into your head when you're
programming – just remember to add it to your design notes.
16
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Chapter 2
Game Assets
17
BEN TYERS
Variables
What is a variable? In simple terms a variable is a value held in the computer's memory.
In GM there are two main types: Strings and Real Numbers
Here are some examples of strings:
“hello”
“big”
“What is your name?”
Here are some examples of real numbers:
1
7
3.14592
1999999
So strings are generally words, and real numbers are numbers.
Real values are signed floating point numbers. GM also allows hexadecimal
representation of real values in code (preceded by "$").
Below is an example for creating an object and adding together two string variables.
First create a new object by clicking on the create object button, a shown in Figure 2_1:
Next name the object obj_example, and click add event, as shown in Figure 2_2.
18
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
19
BEN TYERS
Navigate to the control tab, on the right of the screen as shown in figure 2_4:
Next click the VAR button, as shown in figure 2_5, and drag into the actions window:
Next assign a name and a value, as in Figure 2_6. Notice the quotation marks at the start
and end, this lets GM know it’s a string:
20
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Next click “Add Event” again and add a “Draw” event, as shown in Figure 2_9:
22
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Next select the “Draw” tab as shown in Figure 2_10:
Next click the “Var” button with the green outline, as shown in Figure 2_11:
23
BEN TYERS
Next set as follows, remembering to tick the relative box, as shown in Figure 2_12:
24
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Rooms
Next we’ll create a room. Click the create room button on top bar, as shown in Figure
2_13:
Go to the ‘Settings’ tab and set the name as room_example, as shown in Figure 2_14:
Now go to the objects tab, create one instance of the object by clicking in the middle of
room, as shown in Figure 2_15:
A room is the place where your game takes place. It will generally consist of a background
and objects. A game can have multiple rooms, for example; a game may have a menu room,
a room where the main game-play takes place and a shop to buy things. When you're starting
out it's fine just to have one room. As you get more advanced in GM you'll learn how to do
things such as: use multiple rooms, change the size of rooms, and create views for scrolling
games.
Finally, click the green triangle on the top bar, as shown in Figure 2_17:
If you followed the steps correctly, you should see something like that shown in Figure
2_18:
26
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
27
BEN TYERS
Now create a new object, as shown in Figure 20:
Now click the ‘Add Event’ button, followed by the ‘Create Event’, as shown in figure
2_22:
28
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
We will now program three variables, x y and z. This can be done by clicking the control
tab on the right and clicking the grey square variable as shown in figure 2_23:
29
BEN TYERS
Create the following, as shown in figure 2_24, by repeating this process three times.
Next create a draw event, as shown in Figure 2_25, by clicking on the ‘Add Event’ then
the draw event:
30
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Now select the ‘Draw’ tab and then the draw variable action, as shown in figure 2_26:
Program this draw action as shown in Figure 2_27, remember to check the ‘relative’
check box.
Figure 2_28. Creating new room and placing object in the room.
You can also go to the ‘settings’ tab and naming the room, room_1.
Click the green tick to save and close the room window.
Now save your game, by clicking File and naming it ‘example_game’.
To test your game, click the green arrow at the top of the screen. You should see some-
thing as shown in Figure 2_29.
32
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Sprites and Objects
A sprite is a graphical element that is displayed on the screen. Some examples would be
a ball that bounces around on the screen, or a bat the player controls.
You can create a sprite by clicking on the sprite image in the title bar, as shown in Figure
2_30.
After clicking on the create sprite you choose to load or edit it. Click the load button and
browse to resources file: Sprites and Background For Examples >> brick and ball>> spr_bo-
nus_2_0.png
Load it in and name the sprite, sprite_skull, and click ok, as shown in Figure 2_31.
After creating a sprite you then need to choose an image file and give it a name. You can
call the sprite anything you want, excluding words that are pre-assigned to GM (see appendix
in back of this book for information on this). You can import various image files including
GIF, JPG, BMP, PNG).
A good housekeeping practice is reduce/edit/crop the image the file size of your graphics
files using external software. This will reduce the overall size of your game, make it load
quicker and may reduce some problems when playing. Sprites are drawn by using an object
event or code.
33
BEN TYERS
Although you can call your sprite almost anything, it's good practice, especially when it
comes to coding, to be descriptive. For a sprite, start the name with sprite_ or spr_ . The main
reasons for this are:
That you may create a lot of sprites, so using this method allows you to easily identify it
and make changes.
When you do start using GM this will prevent errors in your code (if you use a space in a
name in your GML will not understand the coding and will think it's two separate items).
Easily distinguish between objects, sounds and sprites.
Do not use spaces in your description, instead use _ (underscore), as you may also create
similarly named objects or sounds, and having sprite_ or spr_, sounds_ or snd_, or object_ or
obj_, allows you to identify it as a sprite, sound or object.
Good examples of names are:
(for sprites)
spr_ball
spr_chicken
spr_enemy_ghost
(or for sounds)
snd_bounce
snd_background_music_track_1
(or objects)
obj_ball
obj_wall
It's possible to do a number of different things with sprites. The end of this chapter will
start with the basics and progress throughout the book to cover more advanced sprite handling.
Most of the time you'll just be setting sprites to objects and drawing them on them on the
screen, as shown in the following example.
Create a new object, and name it object_skull, and then sprite the sprite you just created
above.
In simple terms, you place an object in your room. You'll then program it, using D&D or
code, to do certain things, such as the examples above. In the most part you will make it react
to events, introduced in this chapter, and do something as a result.
A good name for the start of an object is obj_ or object_. Objects can be assigned a sprite
and made visible (using a draw event) or invisible without a sprite (such as a control object –
control objects will be dealt with at the end of this chapter).
As shown below in Figure 2_32. Do not click OK, as you’ll be adding some more events
to it.
34
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Now click Add event and select create event. As shown in Figure 2_33.
35
BEN TYERS
Next, select the ‘Move Tab’ and add jump to random position and click OK as shown in
figure 2_34.
Figure 2_34. Setting create event to make object jump to random position.
Next add a new event for detecting a mouse button release. Do this by clicking ‘Add
Event’ and then mouse, as shown in Figure 2_35.
36
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Next set as release event, as shown in Figure 3_36.
Next add a jump to random position action in this event, and click OK twice to close all
windows, as shown in figure 2_37.
Figure 2_37. Adding a jump to random position action on the left mouse button release event.
37
BEN TYERS
Now click the new room icon at the top of the page and select the settings tab name the
room as room_test. Next select the objects tab and place in instance of object_skull in room.
Click the green arrow to save and close the room. Save the game as Mini_Game_1. You can
get the GMZ file for this files in resource folder /GMZ files/.
Now play this game by pressing the green arrow at the top of the window, as shown in
Figure 2_38.
Each time you click on the skull it should jump to a new position.
38
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Sounds
Various sound files can be used in GM, the main ones being, mp3, wav, and ogg. Sounds
can be things such as sound effects (bounces, boings, animal sounds, and other effects, voices
or dialogues) or background music.
The latest version of GameMaker allows for much control over how the program/game
utilizes the sound files you use. You can choose to load sounds as needed either into memory
or straight from the storage device. Pre-loading sound effects will ensure they play quickly,
while loading background music only as needed will reduce the amount of memory that's
required.
GM can play an assortment of different sound files. You can program your game to play
sounds when certain conditions are met, for example when one object hits another or a certain
score is reached. Sounds can consist of effect sounds or background music.
You're probably going to need an external sound editor, as at the time of writing of writing
there isn't a built-in one included in GM. There are plenty of free ones available, I recommend
Audacity, which is free and it's quite easy to use its basic functions.
It's good practice, as before with sprites, to name your sounds as descriptively as possible,
starting with snd_ or sound_. A good example is 'snd_score_point', some bad examples are
'beep', 'extra life' and 'hello'.
A good housekeeping practice is to reduce the file size of your sound files using an ex-
ternal program. This will reduce the overall size of your game, make it load quicker and may
reduce some problems when playing.
We'll now add a sound effect to this mini game so it makes a sound when you click on
the skull.
Firstly we’ll load a sound. You’ll need to create a new sound, you can do this by clicking
on the add sound button on the title bar, as shown in Figure 3_39.
39
BEN TYERS
Name the sound, sound_ghost_hit and click the folder icon, as shown in figure2_40.
Load in the sound boink3 from the resources in the folder /Download_Pack/Sounds and
Voices/Pack 1, as shown in Figure 2_41.
40
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Your project tree should now look like Figure 2_42 below.
Now re-open object_skull by double clicking on it the directory tree and then click on the
Left Released Event, so your screen looks like that in Figure 2_43.
41
BEN TYERS
Select the ‘Main 1’ tab and add a play sound button, as shown below in Figure 2_44.
Set the sound to the sound you just created, sound_ghost_hit, as shown in Figure 2_45.
43
BEN TYERS
Add a create event by clicking on ‘Add Event’ then ‘Create’, as shown in figure 2_47.
Got to the ‘Score’ tab and then add a set lives action and set this to 5, as shown in Figure
3_48.
44
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Then set the score to 0, using the set score action button as shown in Figure 2_49.
45
BEN TYERS
Now in the object_control object that should still have a window open, add a draw event
by clicking the ‘Add Event’ and then the ‘Draw’ option, as shown in figure 2_51.
Select the ‘Draw’ tab, then the ‘Draw Life Images’, set the sprite as sprite_lives and check
the ‘relative’ box (checking this box will make the sprites be drawn where you place it in the
game’s room), as shown in figure 2_52.
46
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Next set the score to be drawn on the screen. Do this by adding the ‘Draw Score’ action.
Set it to relative and add put 50 in the y box, this will draw the score value just under the
images. See figure 2_53.
Now go to the control tag and add start and end blocks. Anything put between these
blocks will be executed if the score is equal to 20. Your screen should look Figure 2_56 after
you have added them.
48
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Now open the ‘Main 2’ tab and add a message as shown in Figure 2_57. If you need to
move the actions around you can click and drag them.
Now in the same tab, add a restart game action. The step event actions should now look
like Figure 2_58.
49
BEN TYERS
Now, open the room and select the objects tab. Place one instance of the object, ob-
ject_control, in middle of the room, just below the top. As shown in Figure 3_59. Click the
green tick to close this window.
Now open the object object_skull, click on the ‘Left Released’ event. Go to the score tab,
add a set score action and set it to relative 1, as shown in figure 2_60, then click OK. This will
add 1 to the current score when it’s clicked with the left mouse button.
Now save your game as mini_game_3 (a GMZ example is available in the resources
download).
Test your game by pressing the green arrow at the top of the screen. If you click 20 skulls
you’ll get a message that you’ve won. Upon clicking OK the game will restart.
We’ll now set up so that you only have a few seconds to click each skull. If you fail to
click it in time, you’ll lose a life.
We’ll use something called alarms. You can set a starting value of an alarm, for example
100, which will then reduce by one on each step the game makes. When the alarm reaches 0
50
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
you can program GM to make something happen, in this case reduce the number of lives by
one and restart the timer.
You can find the timer actions in the ‘Main 2’ tab, as shown in Figure 2_61.
51
BEN TYERS
Open the object, object_skull and a set alarm [0] to room_speed*3, as shown in Figure
2_62. This will set the timer to 3 seconds.
Next create an event for alarm[0], this will perform these actions if and when alarm[0]
has a value 0 (the time has run out). Do this as shown in Figure 2_63.
52
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Now add the ‘Set Lives’ action from the ‘Score’ tab, and set the number of lives relative
-1. This will subtract 1 from the current lives value. As shown in Figure 2_64.
Next reset the alarm to room_speed*3, as shown below in Figure 2_65. Click OK.
53
BEN TYERS
In the ‘Left Released’ event of object_skull add another alarm[0] reset to room_speed*3.
This will restart the alarm after the object is successfully clicked by the player. This is shown
in Figure 2_66 below.
Click OK, as needed, to close any open windows. Open the object, object_control, and
set as shown in figure 2_67 below.
Figure 2_67. Showing a message and restarting game if lives are equal to 0.
54
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Finally we’ll set a background for this mini game.
Click the ‘Add Backgound’ button as shown in Figure 2_68.
Load in the file ‘crumple’ in the resources folder: / Sprites, Backgrounds and Tile_Sets /
Backgrounds.
Set as shown below in Figure 2_69, naming it background_1.
55
BEN TYERS
Open the room, room_test. Select the background tab, as shown in Figure 2_70.
Next scroll down using the sidebar, and set the background as background_1, as shown
in Figure 2_71.
56
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Now test your game by clicking the green triangle at the top of the screen. Check that it
works as expected.
57
BEN TYERS
Debugging a Game
As with any game, no matter how simple, there may be some bugs, errors or objects doing
unexpected things. After you create any game, you need to test fully and make any changes
as needed. In this example, sometimes the skull object may be drawn at the edge of the screen,
so it won’t be visible and the player won’t be able to click it. This can corrected by making
the following changes:
Open the sprite, sprite_skull, and set the sprite origin as the center, as shown in Figure
2_72. Click OK.
58
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Review
You should now know some of the basics that make up a game:
Sprites: What a sprite is, how to load a sprite, and how to set it to an object.
Variables: How to use basic variables, set them, test them, change a value, and draw the
score and lives.
Objects: Create an object place it in a room. Assign a sprite to an object and draw it on
the screen.
Sounds: Load and use a sound, Make it play with an object is clicked with the mouse.
Backgrounds: Set a background for a level.
Events: Use the create event, alarm event, release of a mouse button, the step event and
the draw event.
Alarms: The basics of using alarms. Creating a new alarm and performing actions when
the alarm value reaches 0.
Control Object: Creating a control object and use it to display and test variables.
Rooms: Creating a new room, setting a name, setting a background and adding objects to
the room.
General: How to save a game and how to play a game.
59
BEN TYERS
Chapter 3
Chapter
60
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Design
After reading the first few chapters, you'll have a basic knowledge of the different ele-
ments that work together to make a game. Don't worry if you don't understand everything just
yet – this will come with time and practice, and will be introduced and explained throughout
this book. Later in the book you'll discover more ways to use these elements. At the moment
it's just important that you are aware of the basic elements that make up a game: Sprites,
objects, sounds, backgrounds, events, conditional structures and basic variables, such as score
and lives. If you're still a little unsure what the elements are, and their basic usage, please go
back and re-read chapter 1 again, and perhaps make a few notes in this book or note-book or
similar. This will be time well spent.
This chapter deals with how to design a game, and consider what elements will be used
in the game and how they will react to the player (user input) and with itself (the AI). The
importance with initial game design, similar to other industries where designing something,
is to get as many ideas and sketches down as quickly as possible. It's not an art competition,
in fact being a poor artist probably helps, crude basic sketches are just fine – so long as you
understand the idea trying to be portrayed.
In this chapter you'll be introduced to more Drag and Drop (D&D), these will be dealt
with continually throughout the rest of the book, and looked at in more detail. For the purpose
of this chapter you'll just be shown the D&D and a brief explanation of what it does. This
gradual exposure will help you think for yourself when creating your own games.
61
BEN TYERS
63
BEN TYERS
Chapter 4
64
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Sprites
Firstly we’ll create and load in sprites required for this game. Most sprites will have a
single image, though some will have more than one. These are called sub-images. This enables
you create sprites that can animate/change if something happens. The sprites below that have
more than one image will allow us to create objects that can be partially destroyed if it is hit
by the ball.
Sprites can be loaded in as images, or you can create them using the built in editor. For
the first two sprites we’ll create them using this editor.
How to create a new sprite
To create a new sprite, click the ‘create a sprite’ button on the top title bar, as shown in
figure 4_1 below.
65
BEN TYERS
Next open the sprite editor by clicking on ‘Edit Sprite’ This is shown in Figure 4_3.
Figure 4_4. Showing where to double click to open the image editor.
66
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
A pop up window will appear, as shown in figure 4_5.
Use the middle mouse scroll button (or CTRL+=) to zoom in so the image looks like
Figure 4_6.
67
BEN TYERS
Select ‘Draw a Line’ by pressing ‘L’ or click where shown in figure 4_7.
68
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Select a green colour by click the green square in the colour palette on the top right, as
shown in Figure 4_8.
Then draw some lines so the image looks like that shown in Figure 4_9.
All sprites in this game will have the Origin set to center. It is important that this is done.
This allows the sprite to centered when it is drawn on the screen.
Create a new sprite, spr_wall_bottom, and repeat the above process, except this time use
the colour red instead of green. Again, set the sprite origin to center, as shown in Figure 4_11.
70
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
In the resource folder / Sprites and Backgrounds For Examples / Brick and Ball/
You’ll find some more sprites for this game to load in, as shown in Figure 4_12.
Figure 4_12. Additional sprites that need to be loaded into the game.
To load in an image to use a sprite, click the ‘Create a Sprite’ button as shown previously
in Figure 4_1. Then follow the steps below.
Enter the name of the new sprite, then click ‘Load’ as shown in Figure 4_13.
71
BEN TYERS
Then navigate to resource folder and load in the sprite image, as shown in Figure 4_14.
72
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Create an object for each sprite, for example obj_ball (shown in Figure 4_20b). Repeat
this process for the sprites shown in Figure 4_20a, except the sprite spr_stone_blue which will
be done differently.
The sprite for spr_stone_blue is slightly different. It will consist of two sub-images.
One image will show the stone whole, one slightly damaged. When we add this to an object
later in the chapter we can set it so that it when it is hit once by the ball it shows some damage,
then is destroyed completely when hit again.
To create this, add a new sprite by clicking on the ‘Create a Sprite’ button as shown back
in Figure 4_1.
Name the sprite ‘spr_stone_blue’ and then click ‘Edit Sprite’. Click ‘Add From File’ or
press CTRL+A as shown in Figure 4_16.
73
BEN TYERS
Click Open. The click ‘Add From File’ again and select the whole blue stone, as shown
in Figure 4_18.
74
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Your project tree for sprites will look the same as Figure 4_20a.
Now is a good time to save your progress. Save this as ‘Brick_and_Ball_A’ , by clicking
file and then ‘Save As’.
75
BEN TYERS
76
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Chapter 5
Part 2
77
BEN TYERS
Creating The Game
Sounds
We’ll now load in the sound effects and music for this Brick and Ball Game.
The sound resources for this game can be found in the download resources folder /Sounds
and Voices/Brick_and_Ball Basic/.
You can add a new sound by clicking the ‘Create a Sound’ button on the top of the GM
windows, as shown in Figure 5_1.
When the window opens, type in the name of the resource, the first one is
‘snd_bounce_1’, click the folder icon and navigate to resource’s folder and load the correct
sound. When done click OK. This procredure is shown in Figure 5_2.
78
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Repeat the above procedure to load in all sounds as shown in Figure 5_3.
We’ll now create the objects for this game, and add to them the sprites that were created
in the previous chapter.
First we’ll create a control object. We’ll use this set up score and lives and to draw them
on the screen.
Click the ‘Create an Object’ button on the top bar, see figure 5_4.
79
BEN TYERS
Name this object obj_control, as shown below in Figure 5_5. This object does not require
a sprite.
Update – Please see page 121 for a correction for this object
80
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Next we’ll add events to this object. We’ll start with the create event. You do this by
clicking ‘Add Event’, followed by create event. The D&D or GML in the Create Event is only
execuated when this object is first created. This makes it’s ideal for setting initial variables
for game, in the case the starting score and the number of lives.This is shown in Figure 5_6.
Go the ‘Score’ tab on the right, set the score to 0, set the lives to 5. This is shown below
in Figure 5_7
Then go to the ‘Main 1’ tab and click the sound icon, this shown in Figure 5_8.
Set the sound to the following, as shown in figure 5_9. This will set the background music
to play in a loop. Setting as a loop means when the track comes to the end it will then restart
from the beginning again.
82
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Next go to the ‘control’ tab and add the action ‘Set Variable’. Set global.bat_speed to 4,
as shown in Figure 5_10.
Once you’ve completed the above, the object obj_control should look like that shown in
Figure 5_11.
83
BEN TYERS
Next we’ll create a step event. This can be done by clicking ‘Add Event’ and then Step,
then Step again. This is shown in Figure 5_12.
84
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
First go to the score tab and add the action ‘Test Lives’. This is shown in Figure 5_13
below.
Next add a ‘Start Block’ action from the ‘Control Tab’. Your window will now look like
Figure 5-14.
Also from the ‘Main 2’ tab drag across the ‘Restart Game’ action.
Finally go back to the ‘Control Tab’ and add an ‘End Block’ action.
When done, the ‘Step Event’’ for obj_control will look like Figure 5_16 below.
What this does: Each step of the game it will test how many lives are left. If there are no
lives (a value of 0) it will execute the actions between the start and end block. Placing the
actions between the blocks is very important, it will only execute them if lives are 0. If the
actions were not placed inside blocks then the actions would be executed every step of the
game (except the first action just after the testing of the variable).
Now Click the Green Tick / OK button to close the window.
Next is to add the draw event for the control object, obj_control.
Open the object obj_control by double clicking it on on resource tree on the left.
86
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Click ‘Add Event’ then ‘Draw’.
Add the ‘Draw Score’ Action (this has a green border), double click it to open it and set
as shown in Figure 5_17. Do not click the relative box.
Next draw the lives as image, by setting as shown in figure 5_18 below, again do not
click the relative box.
Next we’ll draw the value of the variable global.bat_speed and some text so the player
knows what the number is.
87
BEN TYERS
First the variable, go to the control tab and add the action ‘Draw Variable’ and set as
shown in figure 5_19.
Finally for this object we’ll draw some text infront of the variable being drawn. Go to the
‘draw’ tab and add the action ‘Draw Text’. This shown in Figure 5_20.
Click OK to close all open windows. The control object is now complete.
Let’s check that it works ok so far.
Click the ‘Create Room’ button on the top bar, as shown in figure 5_21. Name the room
game1.
Next add a ‘Create Event’ and add the ‘Set Variable’ action to set global.bat_speed to 1,
as shown in figure 5_25 below.
90
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
91
BEN TYERS
Then program the step event so it looks like that shown in figure 5_28 below. This keeps
the obj_player_bat on the screen. Checking the speed will keep the speed between 1 (mini-
mum) and 20 (maximum). Putting in place checks such as this is an important part of the game
development process and helps prevent unexpected things from happening. You should do
checks like this on any variable, global or local, that changes through the course of playing
the game. It would also be nice if current speed of the ball was displayed at the top of the
screen.
These actions will stop the players bat from going off the screen and keep the bat size and
speed within a certain range.
Go to the draw actions tab and drag across ‘Draw Sprite’, as shown in figure 5_29.
92
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Program this action as shown in Figure 5_30, followed by OK.
The final stage for this object is to program in the key presses that make the object move.
Click add event, Key press, then click ‘left’, as shown in Figure 5_31.
93
BEN TYERS
Go to the ‘Move’ tab and add the action move fixed. Program it as shown in Figure 5_32.
Figure 5_32. Programming a left key press event to make an object move.
Repeat the previous steps, but this time program for a right key press event and set the
movement to the right, as shown in Figure 5_33.
94
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Finally add one of this object to the room by double clicking the room on the project tree
on the left of the screen. Selecting Objects and adding a bat, as shown in Figure 5_34.
Now save and then test the game. If everything has been done correctly, you should be
able to control the bat left the left and right keys without the bat going off of the screen.
Next we’ll add some borders.
Create an object, obj_wall_tlr, and set the sprite spr_wall_tlr, check the solid tick box, as
shown in Figure 5_35. When done, click ok.
Next we’ll create some bonus objects. These objects will drop down when the ball hits a
bonus stone.
Create a new object, obj_bonus_1, assign the sprite spr_bonus_1.
In the create event, set it to start moving down the screen as shown in Figure 5_37.
Next create a collision event for with obj_wall_bottom, so that the object destroys itself
on contact with it. Click ‘Add Event’, then click ‘Collision’ then select the object
obj_wall_bottom, as shown in Figure 5_38.
Add the action ‘Destroy Instance’ from the Main 1 tab. Leave the check box as self. Your
screen will look that in Figure 5_39 below.
97
BEN TYERS
Add the actions to play the sound snd_bonus_collected, set the score relative to 250
(check the relative box), and destroy instance (self again). As shown in figure 5_40 below.
Figure 5_40. Adding actions for the collision event with obj_player_bat.
Next create a new object, obj_bonus_2, set the sprite to spr_bonus_2. In the create event
set it to move down with a speed of 2. Make a collision event with obj_wall_bottom. Make a
collision event with obj_player_bat, and the actions to increase the score by 500 (with relative
box checked), set the variable global.bat_speed to 2 relative. The next three screen shots show
how to do this, in Figures 5_41, 5_42 and 5_43.
Figure 5_41. Setting object to start moving using moved fixed action.
98
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Figure 5_42. Setting the ‘destroy self’ action in collision with obj_wall_bottom.
Figure 5_43. With collision obj_player_bat event. Set score relative to 500, set
global.bat_speed to relative 2 and destroy self actions.
Next create a new object, obj_bonus _3, assign the sprite spr_bonus_3.
In the create event, add the ‘move fixed’ action to make it move down at a speed of 2.
In a collision event with obj_wall_bottom, set it to destroy itself, using ‘Destroy Instance’
action.
In a collision event with obj_player_bat, set the variable global.bat_speed to -2 relative
(check the relative box). After this place a ‘instance destroy’ (self) action.
Next we’ll create the ball object. Create a new object and name it obj_ball. Add a create
event and set the ball to start moving in an up-left direction at a speed of 3, by using the moved
fixed action. Check the ‘solid’ check-box. See Figure 5_44 to see what this should look like.
99
BEN TYERS
Click ok to close the windows. We’ll come back to this object later in this chapter to add
some collision events, once the other objects have been created.
Next step is to make some stones that the player must try and hit.
Create a new object obj_stone_red and assign the sprite spr_stone_red.
Make a create event for this object and use the ‘Set Variable’ action to set a variable
my_health to 1, as shown in Figure 5_45. This is a local variable (note it does not have global.
in front of this). This variable keeps track of how many hits are needed to destroy it, it will be
programmed in the collision event to reduce the value of my_health by 1 each time it is hit by
the ball.
Create a step event and program as Figure 5_46 below (note that the score is set relative,
so you’ll need check the relative box)
:
100
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Next create a collision with obj_ball and add set as shown below in Figure 5_47. Set it to
reduce the value of my_health by 1, as shown below, remembering to check the relative box.
Figure 5_47. Collison with obj_ball, and showing solid checkbox ticked. Note that Set variable
is set to -1 with relative box checked.
101
BEN TYERS
Figure 5_50 Collision event with obj_ball for object obj_stone_pink. Remember to set Set
Variable Relative by -1 by checking the relative box.
Next create a new object, obj_stone_yellow and set the sprite spr_stone_yellow. Program
it the same as obj_stone_pink above.
Next create a new object, obj_stone_blue and set the sprite as spr_stone_blue.
Program it as shown below in Figures 5_51 to 5_54.
102
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Figure 5_53. Collision event with obj_ball for object obj_stone_blue. Set as relative using the
relative check box.
Figure 5_54. Draw event for object obj_stone_blue. This draw event sets it to draw the subi-
mage sprite, either 1 or 0, depending if it’s been hit or not.
Figure 5_57. Collision event with obj_ball for obj_bonus_stone_1. Set variable as -1 with rela-
tive box checked.
Next create a new object obj_bonus_stone_2, set the sprite to spr_bonus_stone_2 and
program as shown in Figure 5_58 to 5_60.
104
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Next create a new object, obj_bonus_stone_3, set the sprite, spr_bonus_stone_3, and pro-
gram as shown in Figures 5_61 to 5_63.
105
BEN TYERS
Figure 5_61. Create event for obj_bonus_stone_3.
106
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Now re-open obj_ball by double clicking it on the resource tree, shown in figure 5_64a
Create a collision event with obj_wall_tlr and set as shown in Figure 5_64b below:
107
BEN TYERS
Next create a collision event between the ball and the floor, as shown in Figure 5_64c
Add the action ‘bounce’, from the move tab, and set to not precisely, as shown in figure
5_66.
108
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Next go to the ‘Control’ tab and add the action ‘Execute Code’, as shown in Figure 5_67.
Next double click on this this action, as shown in figure 6_68. Type in the GML code:
move_bounce_all(true);
109
BEN TYERS
This code will help the ball bounce without getting stuck next to objects. It’s not a perfect
soloution, but will suffice for now.
Now repeat the actions from Figures 5_64 to 5_67 for all the other stone objects, when
done, the object obj_ball should look like that as shown below in 5_69.
110
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Finally open the room and add the objects so it looks like that as shown in Figure 5_70,
rename the room room_level_1.
Now save your game, then test it by clicking the green triangle on the top bar.
Check the game plays as expected, if not go back and check you haven’t made any mis-
takes.
The GMZ file for this game is in the download folder /GMZ Files/Brick and Ball and is
file book_1_brick_and_ball_A.
111
BEN TYERS
Chapter 6
112
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Refining The Game
Once you've got your basic game functioning, you can start refining the look and play of
it.
• Refine the game by replacing place holder sprites, adding more sounds
• Set a goal to the game
• Add more objects
• Add music
• Change the appearance of text
• Create Multiple Levels
You'll learn how to do all of these in this chapter.
Before you proceed any further, I suggest you go back and review chapters 1 through 5.
This chapter will assume that you've understood everything so far, and will contain less ex-
planations and screen shots.
We'll go back and edit some of the settings and make it more dynamic to allow the future
integration of other features, such as a bonus object that makes the bat move faster or change
the bat's size.
Upon playing your game you'll see that it's quite difficult to keep the players bat up with
the ball.
Open object obj_control and delete the set global.bat_speed 'set variable' action. Click
OK to close the open window.
Open object obj_player_bat, add a 'set variable' action and set the variable
global.bat_speed to 10, as shown in Figure 6_1. This will make the starting speed of the bat
much faster. Now open the step event in object obj_player_bat and change the test
global.bat_speed from 1 to 3. This will keep the speed of the bat between 3 and 10.
113
BEN TYERS
To make testing the game easier we'll program in some key press events that can be used
to make changes. In this case we'll program it so key 'a' slows down the bat and key 's' speeds
it up. We'll also add keypress of 'r' to restart the ball. For keypress events, click 'Add Event'
then 'Key Press' then 'Letters' as shown in Figure 6_2. This is shown in Figures 6_3, 6_4 and
6_5 below.
115
BEN TYERS
Figure 6_5. Setting key 'R' to restart ball. Note: This is set to apply to object obj_ball.
Now save and test your game. Check that minimum and maxium speeds of the players
are kept in range, by pressing 'a' and 's'. Check you can restart the ball by using 'r'.
116
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Set a Goal to the Game
The aim of this game is to get the biggest score you can before you lose all your lives.
You could of course have other goals such as:
• Collecting all the bonuses
• Getting to the next level
• Completing as quickly as possible
• Destroy all the bricks on a level
For the purpose of this introduction book, we'll set it so you have to destroy all bricks to
get to the next level. We'll need some method to keep track of how many stones are left on
each level. A good way to do this is to create a variable in the create event of each stone, as
shown in Figure 6_6 below, making sure you check the relative box is ticked:
Repeat above process with all of the other stone objects, including the bonus stones.
Now we need to reduce the value of global.stones_left each time a stone is destoyed.
117
BEN TYERS
Open up a stone and add a 'destroy event', by clicking 'Add Event' then 'Destroy'. Add the
action as shown below in Figure 6_7.
Repeat the above process with all the other stone objects, including the bonus stones.
Now open the object obj_control, and program the step event so it looks like that shown
in Figure 6_8. The action 'Display Message' can be set to "You Win".
Figure 6_8. Showing updated actions for step event in object obj_control.
118
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Next we'll create a new font, so the text displayed on the game looks different.
First click the 'Add Font' button on the top bar, as shown in Figure 6_9.
Figure 6_11. Add 'Set Font' action to the draw event for object obj_control.
Now save and test your game, you will now see the text drawn in the new font. Destroy
all of the bricks and you should get the message 'You Win'.
119
BEN TYERS
Next we'll create a few more levels. On the project tree, right click on room_level_1 and
select 'Duplicate', as shown in Figure 6_12.
Repeat this process twice and name the new rooms, room_level_2 and room_level_3, as
shown in Figure 6_13.
Now open the room room_level_2. Right click on each of the stones and select delete, or
hold Left Control and the right mouse button whilst moving the cursor over the stones.
Now add stones to make a new level, by selecting the object tab and selecting the stones,
then clicking in the room to position each one, as shown in Figure 6_14.
120
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Figure 6_14. Adding stones to the level.
When you've finished adding stones, you'll have new level, for example as shown below
in Figure 6_15.
A GMZ file for this game is available in the download, at Download Pack / GMZ Files /
Brick and Ball / book_1_brick_and_ball_B
121
BEN TYERS
122
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Chapter 7
123
BEN TYERS
In this chapter we'll create a very simple Spot the Difference type game.
This game will use background image. A background is applied to a room, and is gener-
ally a static image that is shown behind any objects. (As you become more advanced you'll
learn how to make backgrounds move, or use multiple backgrounds to make cool graphical
effects).
First you need to load in a background. You can create a new background by clicking the
'Create a Background' button the top bar, as shown in Figure 7_1.
Now we'll create a new room and assign this background to it.
Click the 'Create a Room' button on the top bar. This is shown in Figure 7_4.
125
BEN TYERS
Go to the settings tab, name the room room_level_example and set the width to 800 and
the height to 400. This is shown in Figure 7_5.
126
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Now go to the tab 'backgrounds' , make sure 'Background 0' is highlighted, check the
'visible when room starts' checkbox, click where it says <no background' and select
bg_level_example. These steps are shown in Figure 7_6.
When done your screen will look like that in Figure 7_7.
This game uses only 4 sprites, load in from the resource folder so your tree looks like that
shown in Figure 7_8.
Figure 7_9. obj_spot_found with draw event and action 'draw self'.
Next create a new object, obj_spot, and assign the sprite spr_spot. Create a mouse left
button released event. Add the action 'execute code' from control tab, as shown in Figure 7_10.
128
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
After adding this action, open it up by double clicking on it in the actions window.
Then enter code shown below.
///difference found
global.tofind-=1;
instance_create(x,y,obj_spot_found);
instance_destroy();
129
BEN TYERS
Figure 7_11. What code looks like on the screen.
Click the green tick at the top left of the window to apply.
Explanation of the code:
///difference found - This is comment. A comment on one line can start with // ///.
Using /// allows you set a name for any code that is shown in the actions window (see below
in Figure 7_12.)
Finally make sure the visible check box is unchecked, as shown in figure 7_13. This
means that when the game is run the objects will be present on the screen, but no sprite will
be drawn. This object will be placed where the player needs to click when they have found a
difference.
130
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Next create a new object, obj_you_win, set the sprite as spr_you_win and program as
below, using the event 'mouse left released' and the action 'Restart Game' from the 'main 2'
tab. This is shown below:
Figure 7_14. Setting sprite and programming left mouse released event.
Next create a new font by clicking the 'Add Font' button on the top bar and then set as
shown below in Figure 7_16.
131
BEN TYERS
132
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Figure 7_17. Showing GML code for create event of object obj_control_10_diff
Next create a step event and add the following code as shown below.
///Check If Won or Lost
if global.tofind==0
{
room_goto(room_you_win);
}
if global.attempts==global.attempts_max
{
room_goto(room_you_lose);
}
Code Explanation:
Next create a global left mouse button released event. Note that this is different from the
previously used mouse left button released event. A global mouse button event can be any-
where in the game's window, not just on the sprite for an object. This can be added as shown
in Figure 7_19 below.
133
BEN TYERS
Add a execute code action and add the code shown next.
///Attempt Made
global.attempts+=1;
Code explanation - This will add 1 to the variable global.attempts. This is the same as the
action 'set variable' 1 with relative box checked.
When done it will look like Figure 7_20 below.
Figure 7_20. GML code for global left released mouse button code action.
Finally create a draw event, and add the code as shown below.
When done, this will look like:
134
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
///Draw GUI
draw_set_font(font_gui_1);
draw_set_color(c_red);
if global.tofind>0
draw_text(10,320,"To Find: "+string(global.tofind));
if global.attempts>0
draw_text(10,340,"Attempts: "+string(global.attempts));
draw_text(10,360,"Max Attempts: "+string(global.attempts_max));
Explanation of code:
draw_set_font(font_gui_1); - This sets the font for drawing text/variables.
draw_set_color(c_red); - Sets the drawing colour as red.
if global.tofind>0 - checks to see if there are more than 0 differences to find based
on the value of global.tofind
draw_text(10,320,"To Find: "+string(global.tofind)); - this draws text
in the game window at position 10,320 (10 pixels across from the left, 320 pixels down from
the top). "To Find:" draws the text “To Find” and string(global.tofind); converts
a real number to a string so it can be added to and drawn as text.
if global.attempts>0 draw_text(10,340,"Attempts:
"+string(global.attempts)); - if more than one attempt left it will draw the text "At-
tempts:" followed by global.attempts which is converted to a string.
draw_text(10,360,"Max Attempts: "+string(global.attempts_max));
Displays maximum attempts allowed.
135
BEN TYERS
This will look like Figure 7_21.
Figure 7_21. Shown Draw event GML code for the object obj_control_10_diff
Now apply changes by clicking the green tick and click OK to close any open windows.
Next open the room room_you_win and place one instance of the object obj_you_win in
the center. Close the window. As shown in Figure 7_22.
Clcik the green tick to close this room. Next open the room room_you_lose and place one
instance of the object obj_you_lose in the center. Close the window.
136
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Finally open the room room_level_example. Place one instance of obj_control_10_diff
in white area at the bottom. Then place one instance of the object obj_spot in the places where
the differences are. When you're done they'll be ten instances of obj_spot.
This complete room is shown in Figure 7_23.
Figure 7_23. Showing room room_level_example with control object obj_control_10_diff and
ten instances of obj_spot.
137
BEN TYERS
138
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Chapter 8
139
BEN TYERS
In this chapter we'll add some extra features, such as multiple levels, a level select screen,
a splash screen, and some basic adverts.
140
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Splash Screen
First the splash screen. A splash screen can be used to show your game logo, an advert,
or any other info you'd like the player to see before starting your game. With a graphics editor
of your choice, create a simple image 800x400 pixels. (or load the splash_example file from
the resources download).
Create a new background by clicking the background button on the top bar. Load in the
image you created, name it background_splash_1, you can uncheck both the tile settings in
the Texture box.
Create a new object, obj_splash_1. Make a create event, add a code action, and enter the
GML code to set an alarm 0 for 90 steps. As shown in Figure 8_1.
141
BEN TYERS
Now create a new room, in the settings tab name the room room_splash_1 (see Figure
8_3) and change the width to 800 and the height to 400. In the backgrounds tab, set the back-
ground to background_splash_1, you can uncheck the tile checkboxes, (see Figure 8_4). Then
go to the objects tab and add one instance of the object obj_splash_1 (see Figure 8_5).
142
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
143
BEN TYERS
Your resource tree for the rooms will now look like that shown in Figure 8_6
You'll need to change the order of these rooms so that room_splash_1 is at the top. Click
on it with your left mouse button and drag it up to where it the green folder icon and Rooms
text is. It should now look like Figure 8_7.
You can now save and then test your game. The splash screen should show and then take
you to next room, room_level_example.
144
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Adverts
Next we'll create a very basic advert system which you can display adverts or messages.
Something like this is great if you have a trial version of your game and want the player to
visit a webspage to buy/upgrade/downlad the full version.
This example will use 3 different adverts, but you could easily add more or less.
Using your prefered graphics editor create 3 advert images of 200x50 pixels, or use the
sample resources).
Add a new sprite, name it spr_adverts, and add all three images as sub-images, and set
the origin to the centre, see Figure 8_8.
145
BEN TYERS
Code explanation:
alarm[0]=90; - resets the alarm to 90
current_ad+=1; increases the variable current_ad by 1
if current_ad==3 current_ad=0; Checks if current_ad equals 3, if so reset to 0;
Next add a mouse left button released event and add the following code:
if current_ad==0 url_open( 'http://wwww.google.com' );
if current_ad==1 url_open( 'http://wwww.yoyogames.com' );
if current_ad==2 url_open( 'http://wwww.gamemakerbook.com' );
Above code will take you a different website, depending on the value of current_ad.
Now make a draw event and enter the following code:
draw_sprite(spr_advert,current_ad,x,y);
Next we'll create a button so the player can exit the game and return to the menu.
Create a 200x50 pixel button with the word 'QUIT' on it, or load the example from the
resources, name this sprite spr_quit, and set the origin to the center.
Create an object, obj_quit, assign the sprite spr_quit. Make a mouse left button released
event and enter the following code:
room_goto(room_menu);
This will take the player to the room room_menu, which we'll create shortly. Place one
instance of this object in the bottom right of the room room_level_example.
Now create a new sprite, 200x50 pixels, with the word 'EXIT' on it, with origin center.
Create an object obj_exit, and in a mouse left button released event enter the following code:
146
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
game_end();
This will quit the game and return to the Operating System that was running the game,
ie. Back to windows or android screen for example. Place one instance of this on bottom right
of room_menu
Now we'll create the main level select room.
Create a new room, room_menu. Set the width to 800 and height to 400.
Click the green tick to close the window.
On the resource tree drag this room up so it's between room_splash_1 and room_level_ex-
ample, so it looks like figure 9_10.
147
BEN TYERS
Close the window.
Create a new object, obj_levels_1.
Program as shown below:
Create event code:
//set variables
my_id=1;
count=1;
image_speed=0;
Step Event:
///check if allowed
if global.level <= my_id-1
count=1
else
count=0
Draw Event:
draw_sprite(spr_levels,count,x,y);
draw_text(x,y,string(my_id));
Create Event
Sets variables needed for the object. My_id relates to which level can be unlocked, count
determines if object is locked 1 or unlocked 0.
Step Event
Mouse Event
Draw Event
In the tree on the left, right click on obj_level_1 and select duplicate, do this four times.
Name the new objects: obj_level_2, obj_level_3, obj_level_4 and obj_level_5.
148
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Open object obj_level_2. Change the code in the create event, my_id=1; change to
my_id=2;
In the left released event, change the code room_goto(room_level_1); to
room_goto(room_level_2);
Do this with the remaining 3 objects, change to 3, 4 and 5 respectively.
Now open obj_control_10_diff.
Remove the current code in the step event and replace with the following:
///Check If Won or Lost
if global.tofind==0
{
global.level+=1;
room_goto(room_menu);
}
if global.attempts==global.attempts_max
{
room_goto(room_you_lose);
}
149
BEN TYERS
Figure 8_12. Showing control object and 5 level objects in room room_menu.
Now open up each room for levels 2 through 5. Go to the object tab and then place an
instance of object obj_spot where each difference is. You can do this two ways: Firstly delete
all instances of obj_spot currently present by right clicking and selecting delete and then create
a new instance where each difference is, or secondly drag the existing instances of object
obj_spot by left clicking and dragging.
When done save your game and test it. Remember you can press 'L' to increase the level,
upon doing so each level should unlock in turn and lastly you'll be taken to the you win room,
room_you_win.
A GMZ file for this project is available in Projects Resources Folder - Spot_the_differ-
ence_pro .
151
BEN TYERS
Chapter 9
Hangman Game
152
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
In this chapter you'll learn about string variables and how to do various things with them.
You'll also learn of to use text files and load data from them, and creating graphic effects for
button. There will a lot of new GML, which willl be introduced and explained throughout this
chapter. We'll also introduce a script for processing and returning string variables.
First create a new sprite, spr_button and load in the following sprites from the resources
folder.
153
BEN TYERS
Sprites
This game uses 4 sprites, each of which has sub images.
First create a new sprite, spr_button and load in the following sprites from the resources
folder, as shown in Figure 9_1.
Next create a new sprite, spr_newWord, and set as shown below in Figure 9_3.
154
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Next create a sprite, spr_exit, and set as below in Figure 9_4.
155
BEN TYERS
Included File
This game uses an external file. In this case it's a text file with words that will be used for
the game.
Using a document editor, such as note pad. Enter the following words, which must all be
in capitals:
HANGMAN
HELLO
GAME
PROGRAM
JAVA
BOOK
CREATIVE
And save this document as dictionary.txt Note: This must be a .txt file. If you’re using a
wordprocessor such as Microsoft Word or Open Office make sure you save it in the txt format.
Now add this to your game. In the directory tree, right click on 'Included Files' (Figure
9_6) and left click on 'Create Included File' (Figure 9_7).
Then navigate to the location where you saved your file and load it in, which is shown in
Figure 9_8.
156
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
157
BEN TYERS
Game Fonts
Now add two fonts, as shown in Figure 9_9. These fonts should already present on your
computer, if they're not you can substitute for a similar font.
158
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Script
Next we'll create a script. You can create a new script by right clicking on the 'Script' in
the resource tree, as shown in figure 9_10 , and then selecting 'Create Script'.
159
BEN TYERS
Enter the following code:
var file;
if(file_exists(working_directory + "dictionary.txt")){
//open the dictionary file
file = file_text_open_read(working_directory +
"dictionary.txt");
if(file == -1){
//if loading the file failed return -1
return -1; //will end the script
}
var wordList, wordNumber = 0;
//make a list containing all words of the dictionary
while(!file_text_eof(file)){
wordList[wordNumber] = file_text_read_string(file);
file_text_readln(file);
wordNumber++;
}
file_text_close(file);
return wordList[irandom(wordNumber-1)]; //return a random word
}
Code explanation:
var file; This declares a temporary variable called file. Declaring a temporary varia-
ble means that once the script exits or returns the variable is removed from the computers
memory. Using temporary variables is good practice and should be followed when ever pos-
sible.
if(file_exists(working_directory + "dictionary.txt")) This checks if
the file dictionary.txt is present in the working directory. It's important to check a file exists
before trying to do things with it such as opening it (below), as this will help prevent some
errors. The working_directory is the path where included files are located on a device. Loca-
tions will vary depending if your testing your game, running from windows, or on another
device such as an android device.
{ This is the start block. Code within { and } will be executed if the above condition is
true (the file exists).
//open the dictionary file Just a comment. Putting comments in your scripts
and code is useful to remind yourself what something does, so when you return to update or
check code things are easier for you.
file = file_text_open_read(working_directory +
"dictionary.txt"); this opens the file, which we've already checks exists and assigns it
to file.
if(file == -1){
//if loading the file failed return -1
return -1; //will end the script
160
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
} This section checks if there is any error, if there is it
will return a value of -1. (return is explainded in more detail in
the paragraph below this code explantion.)
161
BEN TYERS
Arrays
Arrays are a useful way to store data. Arrays can store variables and strings, and other
information.
An example to create an array in GML is:
example[0]="cheese";
example[1]="bacon";
example[2]="pizza";
example[3]="tomato";
example[4]="cake";
162
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Objects
Now we'll create objects for this game.
Code explanation:
activated = true; Creates a variable with the value true. This allows easy testing
using conditionals to find out if something is true or false. This is known as Boolean, which
can be either true or false
letter = "A"; Sets the variable letter to the character 'A'
image_speed = 0; Sets how quickly sub-images are shown in animation. Setting as 0 stops
any animation and will only draw the sub-image stated in the draw event.
image_index = activated; sets the image_index to true, which is equivalent to 1.
if(activated){
//if the mouse hovers above the button
if(mouse_x>=x && mouse_x<x+sprite_width && mouse_y>=y &&
mouse_y<y+sprite_height){
//if the mouse left button is down
if(mouse_check_button(mb_left)){
image_index = 3;
}else{
image_index = 2;
}
//if the button is released
if(mouse_check_button_released(mb_left)){
activated = false;//deactivate button
//check if the letter is in the word
var newReveal = "", found = 0;
for(i = 1; i<=string_length(word); i++){
/*for each letter in the word
check if it is equal to the chosen letter*/
if(string_char_at(word, i) == letter){
found++;
newReveal += letter;
}else{
163
BEN TYERS
newReveal += string_char_at(revealedWord, i);
}
}
revealedWord = newReveal; //update the displayed word
//if the letter is not found in the word, update the
hangman
if(found == 0){
with(obj_hangman){
image_index++;
}
}
}
}else{
image_index = 1; //change image to activated button
}
}else{
image_index = 0; //change image to deactivated button
}
Code explanation:
if(activated) This checks if the variable activated is true. If it is then the code be-
tween the first and last { and } is excecuted. If the value is false it won't execute any code.
This basically means that if you've already clicked the button, you can't click it again.
{
//if the mouse hovers above the button
if(mouse_x>=x && mouse_x<x+sprite_width && mouse_y>=y &&
mouse_y<y+sprite_height){
//if the mouse left button is down
if(mouse_check_button(mb_left)){
image_index = 3;
}else{
image_index = 2;
} This code will change the sub image of the sprite when
the mouse cursor hovers over the button.
//if the button is released
if(mouse_check_button_released(mb_left)) checks for release
of mouse button
{
activated = false;//deactivate button Tells it you've
clicked the mouse (released left button) and sets activated to
false.
//check if the letter is in the word
var newReveal = "", found = 0;
for(i = 1; i<=string_length(word); i++){
/*for each letter in the word
164
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
check if it is equal to the chosen letter*/
if(string_char_at(word, i) == letter){
found++;
newReveal += letter;
}else{
newReveal += string_char_at(revealedWord, i);
}
} Checks if selected letter (different letter on each button) is present in the word and
replaces any _ with the selected letter
} Changes the sub image of the hangman image if your letter was not present.
}
}
}else{
image_index = 1; //change image to activated button
}
}else{
image_index = 0; //change image to deactivated button
draw_self(); draws the current subimage with all the default object
properties
draw_set_font(fnt_button); sets the font to draw text in
draw_set_halign(fa_center); centers the text at x position
draw_text(x+16, y+4, letter);
165
BEN TYERS
Draws the variable letter. x+16 draws the text 16 pixels to the right, y+4 draws the text 4
pixels down. This change in the x and y position will center the text over the sub-images.
Now create a new object obj_hangman and apply the sprite spr_hangman.
In a create event enter the following code:
randomize();
image_index = 0;
image_speed = 0;
gameFinished = false;
Code explanation
166
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
randomize(); This creates random seed based on computers clock. This ensures you
get different random words each time you play the game.
This creates a string with the same length as the word, using just _ in place of each letter.
image_index = 0;
image_speed = 0;
This will repeat the code between { and } below until i is more than 10.
{
var bt = instance_create(64+i*32+(8*i), 64, obj_button);
This will supercede the code letter = "A"; in obj_button and assign it the letter from the string
alphabet at string position i.
}
//middle row
var alphabet = "ASDFGHJKL";
for(i = 0; i<9; i++){
var bt = instance_create(72+i*32+(8*i), 104, obj_button);
bt.letter = string_char_at(alphabet, i+1);
}
167
BEN TYERS
//BOTTOM row
var alphabet = "ZXCVBNM";
for(i = 0; i<7; i++){
var bt = instance_create(88+i*32+(8*i), 144, obj_button);
bt.letter = string_char_at(alphabet, i+1);
}
The above code creates a keyboard on the screen. If you look back at the object obj_button
you'll see code letter = "A"; in the create event. To draw the keyboard on the screen you could
duplicate obj_button 26 times and set a different letter in the create event. The code above is
an easier way to achieve the same result.
gameFinished = false;
Now create a Begin Step. This is different from the normal Step event. Actions and code
within a Begin Step will be executed before any other object's step event. This is useful to
ensure a variable has been declared/changed before another object tries to use the variable.
The code for this Begin Step is:
if(!gameFinished && (image_index == image_number-1 || word ==
revealedWord)){
gameFinished = true; //end the current game
with(obj_button){
activated = false; //deactivate all buttons
}
}
The above checks if the game is not finished by checking two condtions, first if the im-
age_index is at the end (all available guesses are used) or the guessed word matches the word
the player is trying to find.
Finally create a draw event for this object and enter the following code.
draw_self();
168
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
draw_text(64, 280, "You lost!#The word was " + word + ".");
}
draw_set_color(c_black);
}
draw_set_font(-1);
Code explantion
draw_self();
Draws the currently defined sub image with all object properties as if the draw event was
not used.
Draws the revealedWord variable at a position 64 pixels across and 240 pixels down from
the top left.
If game is finished by the player guessing the word, draw the text 'You won!' in lime
colour.
else{
draw_set_color(c_red);//loss
draw_text(64, 280, "You lost!#The word was " + word + ".");
}
If the game is finished by player using all their guesses display 'You lost! The word
was…….'. # means create a line break so following text appears on the next line, as though
enter was pressed.
draw_set_color(c_black);
169
BEN TYERS
draw_set_font(-1);
}
}else{
image_index = 0; //change image to activated button
}
Code explanation:
170
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
//if the mouse hovers above the button
if(mouse_x>=x && mouse_x<x+sprite_width && mouse_y>=y &&
mouse_y<y+sprite_height){
//if the mouse left button is down
if(mouse_check_button(mb_left)){
image_index = 2;
}else{
image_index = 1;
} Changes the sub_image, depending if the mouse cursor is hovering over the
button.
//if the button is released
if(mouse_check_button_released(mb_left)){
//start the game over
//reset the hangman
with(obj_hangman){
image_index = 0;
gameFinished = false;
}
Tells the program to end the current game and reset on mouse button released.
}
}else{
image_index = 0; //change image to activated button
}
image_speed = 0;
171
BEN TYERS
//if the mouse hovers above the button
if(mouse_x>=x && mouse_x<x+sprite_width && mouse_y>=y &&
mouse_y<y+sprite_height){
//if the mouse left button is down
if(mouse_check_button(mb_left)){
image_index = 2;
}else{
image_index = 1;
}
//if the button is released
if(mouse_check_button_released(mb_left)){
//End the game
game_end();
}
}else{
image_index = 0; //change image to activated button
}
You should not need much explanation for the above code.
Finally create a room, rm_hangman, with a width of 800 and height of 400 then put the
objects in as shown below in figure 9_12. That's one instance of obj_buttonNewWord, one of
objButtonExit and one of obj_hangman.
172
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
173
BEN TYERS
Chapter 10
174
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
In this chapter you'll learn how to use ds lists to store and manipulate data.
Sprites
Load in the following sprites from the resources folder:
Create a new background and load in the file bg_wood, name this background bg_wood.
Create a script called mouse_in and put in the following code:
if (mouse_x > argument0 && mouse_y > argument1 && mouse_x <
argument2 && mouse_y < argument3) {
return true;
} else {
return false;
}
175
BEN TYERS
key_flipped=1;
//defaults:
click1=noone; //card number clicked
click2=noone; //card number clicked
matches=0;
attempts=ds_grid_width(deck);
show[0]=0;
show[1]=room_speed;
Code explanation:
//display options:
cols=4; //columns
gap=16; //padding between card sprites
Sets some variables so we know how many images to be drawn across the screen and the
gap between them.
176
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
//add 2 of each card to deck:
ds_grid_set(deck,0,key_sprite,spr_card_1);
ds_grid_set(deck,1,key_sprite,spr_card_1);
ds_grid_set(deck,2,key_sprite,spr_card_2);
ds_grid_set(deck,3,key_sprite,spr_card_2);
ds_grid_set(deck,4,key_sprite,spr_card_3);
ds_grid_set(deck,5,key_sprite,spr_card_3);
ds_grid_set(deck,6,key_sprite,spr_card_4);
ds_grid_set(deck,7,key_sprite,spr_card_4);
Places two of each sprite into the ds grid at the position indicated.
Sets each location (card) by adding a new a location in the ds grid and setting it to false.
//defaults:
click1=noone; //card number clicked
click2=noone; //card number clicked
matches=0;
attempts=ds_grid_width(deck);
show[0]=0;
show[1]=room_speed;
Sets some variables which will be used to keep track of how many cards are flipped and
create a timer that will used to keep non-matching cards visible for 1 second.
178
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
}
//end game:
if (matches>=ds_grid_width(deck)/2) {
room_goto(rm_win);
//instance_create(x,y,obj_memory);
instance_destroy();
} else if (attempts<=0) {
room_goto(rm_lose);;
//instance_create(x,y,obj_memory);
instance_destroy();
}
}
}
}
//draw score:
draw_set_font(font0);
draw_set_color(c_white);
draw_text(16,16,string(attempts));
179
BEN TYERS
Code explanation:
var i,xdraw,ydraw,x1,y1,x2,y2,cs,row,col;
This for loop repeats depending on how many cards in the deck.
Calculates where card should be drawn based on the row and column.
Draws a border around the card. Can be left in, though not required.
Detects a mouse click over current card sprite. If card is already flipped not click will be
registered.
If two cards are clicked it checks if the sprites are them same, if they are mark
both as flipped, if not set timer to start
//end game:
181
BEN TYERS
if (matches>=ds_grid_width(deck)/2) {
room_goto(rm_win);
//instance_create(x,y,obj_memory);
instance_destroy();
} else if (attempts<=0) {
room_goto(rm_lose);;
//instance_create(x,y,obj_memory);
instance_destroy();
}
}
}
}
End the game either when all matches are found, then go to room rm_win, or no more
guesses are allowed then and go to room rm_lose.
//show fail attempt cards for a while:
if (show[0] > 0) {
show[0]=max(0,show[0]-1);
if (show[0] <= 0){
ds_grid_set(deck,click1,key_flipped,false);
ds_grid_set(deck,click2,key_flipped,false);
click1=noone;
click2=noone;
}
}
Create a new object obj_easy_button, and set the sprite spr_easy_button in a mouse left
released button put the following code:
room_goto(rm_easy);
Create a new object obj_playagain, set the sprite spr_playagain. In a left mouse released
event put:
room_goto(rm_menu);
182
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Finally, create a new object obj_exit and set the sprite to spr_exit add the following code
to a mouse left button released event:
game_end();
183
BEN TYERS
184
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Chapter 11
185
BEN TYERS
In this chapter you'll learn how to create a basic tower defense type game.
Sprites
Load in the following sprites:
Sprites spr_enemy1, spr_enemy2 and spr_enemy3 each have 10 sub images, spr_explo-
sion has 7,for example:
Next load in a new background, and set as shown in Figure 11_3 on the next page, ensur-
ing you set as a tile set and width and height of 32:
186
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Now create a room, rm_lvl1, set as a width of 640 and height of 480. Go to the tile tab
and then use the tiles by selecting them to create a room the same as figure 11_4.
Next set snap x and y to 16, click the show room button, and select the room rm_lvl1, as
shown in Figure 11_6.
The program as below, in Figure 11_7, by entering the numbers into the boxes and click-
ing add after each.
188
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
You can create a new script by clicking the 'create a script' button on the top bar, as shown
in Figure 11_9.
Create a new script name it shootBullet (at the top) and enter the following code:
/*
argument0 : bullet type
argument1 : target
argument2 : x
argument3 : y
argument4 : range
argument5 : damage
*/
audio_play_sound(snd_shoot, 0, false);
with(instance_create(argument2, argument3, argument0)){
direction = point_direction(argument2, argument3, argument1.x,
argument1.y);
range = argument4;
damage = argument5;
}
Code explanation: Takes the variables from the sent arguments and creates a bullet.
Click the green tick at the top left to apply changes.
Create another script, shootGuided and program as below:
/*
argument0 : bullet type
argument1 : target
argument2 : x
argument3 : y
argument4 : range
argument5 : damage
*/
audio_play_sound(snd_shoot, 0, false);
with(instance_create(argument2, argument3, argument0)){
target = argument1;
range = argument4;
damage = argument5;
189
BEN TYERS
}
Code explanation: draws series of rectangles to give the effect of a pop up window. Then
close the window.
Next create some fonts:
fnt_default as arial bold size 12.
fnt_small as arial size 8.
fnt_title as arial size 64.
fnt_menu as arial size 50.
Next we'll create the menu system. You could easily create buttons like in previous games
in this book; however the system below shows another method of doing this.
Create an object obj_winGame.
In the create event put:
title = "Victory!";
options[0] = "Again!";
options[1] = "Exit";
190
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
selected = 1-selected;
audio_play_sound(snd_beep1, 0, false);
}
if(keyboard_check_pressed(vk_space) ||
keyboard_check_pressed(vk_enter)){
//if the player presses on enter or space ...
audio_play_sound(snd_beep2, 0, false);
if(selected == 0){
room_goto(rm_lvl1);
}else{
game_end();
}
}
draw_set_font(fnt_menu);
var strW = string_width(options[0])
if(mouse_y>256 && mouse_y<256+string_height(options[0]) &&
mouse_x>(room_width-strW)/2 && mouse_x<(room_width+strW)/2)
{
//if the cursor is above the first option, select it
if(selected != 0){
selected = 0;
audio_play_sound(snd_beep1, 0, false);
}
//if the player clicks, new game
if(mouse_check_button_pressed(mb_left)){
room_goto(rm_lvl1);
audio_play_sound(snd_beep2, 0, false);
}
}
strW = string_width(options[1])
if(mouse_y>352 && mouse_y<352+string_height(options[1]) &&
mouse_x>(room_width-strW)/2 && mouse_x<(room_width+strW)/2)
{
//if the cursor is above the first option, select it
if(selected != 1){
selected = 1;
audio_play_sound(snd_beep1, 0, false);
}
//if the player clicks, end game
if(mouse_check_button_pressed(mb_left)){
game_end();
audio_play_sound(snd_beep2, 0, false);
}
}
191
BEN TYERS
draw_set_font(fnt_default);
Code explanation: Draws the options on the screen, changing colour for selected option.
This another approach for a menu system you can use instead of creating objects that can be
clicked.
Next create a new object, obj_lostGame and set the parent as obj_winGame.
In the create event put:
title = "You Lose!";
options[0] = "Again!";
options[1] = "Exit";
Now we'll create the shop. Create an object obj_shop. Set depth to -1.
In a create event put:
selected = noone;
shopHeight = 128;
shopOpen = false;
money = 2000;
193
BEN TYERS
newTower.towerType = obj_EntitySniperTower;
newTower.towerPrice = 2000;
newTower.towerSprite = spr_tower3;
//close the shop
shopOpen = false;
}
if(money >= 5000){
//if the player clicks on the gatling gun
tower button
if(mouse_x > 272 && mouse_x < 368){
//purchase gatling gun tower
audio_play_sound(snd_beep2, 0, false);
var newTower = instance_create(mouse_x,
mouse_y, obj_TowerPlacer);
newTower.towerType =
obj_EntityGatlingGun;
newTower.towerPrice = 5000;
newTower.towerSprite = spr_tower2;
//close the shop
shopOpen = false;
}
}
}
}
if(selected != noone){
//if the player clicks on the upgrade button
if(mouse_x > 400 && mouse_x < 496){
//find price of the upgrade
var upgradePrice;
switch(selected.object_index){
case obj_EntityTower:
upgradePrice = 500;
break;
case obj_EntityGatlingGun:
upgradePrice = 2500;
break;
case obj_EntitySniperTower:
upgradePrice = 1500;
break;
}
upgradePrice *= selected.level;
if(money >= upgradePrice && selected.level <
3){
//if the player has enough money, get
upgrade
194
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
audio_play_sound(snd_beep2, 0, false);
with(selected){
level++;
range *= 1.1;
}
money -= upgradePrice;
}
}
}
}
}else{
//if the shop is closed and the shop button is clicked
//open the shop
if(abs(mouse_x-room_width/2)<=16 && mouse_y>=room_height-
16){
shopOpen = true;
audio_play_sound(snd_beep1, 0, false);
Code explanation: Checks if shop window is up, if it is draws towers depending if player
can buy or upgrade.
Code explanation. When selected this will draw a circle showing the range of the currently
selected tower.
195
BEN TYERS
And in draw GUI event put:
if(shopOpen){
draw_window(0, room_height-shopHeight, room_width-1,
room_height-1);
draw_sprite(spr_shop, shopOpen, room_width/2, room_height-
shopHeight);
Code explanation. If shop is open draws the towers so that the player knows which they
can afford to buy / upgrade.
Create an object obj_EntityBlock, set the depth to 10, set the sprite to spr_blockMask.
In a begin step event put:
if(instance_number(obj_TowerPlacer) > 0){
visible = true;
}else{
visible = false;
}
197
BEN TYERS
Create a new object obj_EntityTower, set the sprite to spr_tower1, set the parent to
obj_EntityBlock. Set the mask to spr_blockMask ( as shown in Figure 11_10).
Code explanation: Checks if player can shoot or not. Shoots a bullet if allowed.
Create a new object obj_EntityGatlingGun. Set the sprite to spr_tower2 and the parent to
obj_EntityTower and the mask to spr_blockMask.
In a create event put:
image_speed = 0; //stops the animation of the sprite
199
BEN TYERS
Next we'll create an object to place the towers once they've been purchased.
Create an object obj_TowerPlacer and set the depth to -2.
In a create event put:
image_speed = 0;
towerType = noone;
towerSprite = noone;
towerPrice = 0;
sp = 20;
range = 0;
damage = 40;
target = noone;
202
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Code explanation: Spawns random waves of enemies, also checks if player has HP(health
points) and ends the game if 0 or less.
In a draw GUI event put:
draw_window(5*room_width/6, 0, room_width, 31);
draw_text(5*room_width/6+8, 8, "Wave : " + string(level));
draw_window(room_width/2, 0, 5*room_width div 6, 31);
draw_text(room_width/2+8, 8, "Base integrity : " + string(baseHP) +
"%");
203
BEN TYERS
sp = 3 //enemy speed
path_start(path_lvl1, sp, 0, true);
Code explanation: Sets up variables for the object, tells object to start moving on the path.
Code explanation: Destroys self if own HP reaches 0. Reduces players HP if reaches end
of path.
Create a new object obj_EntityFastEnemy, set the sprite as spr_enemy2. Set the parent
as: obj_EntityEnemy.
In a create event put:
image_speed = 0.5;
maxHP = 50 + obj_EnemySpawner.level*5;
HP = maxHP; // enemy health
sp = 5 //enemy speed
path_start(path_lvl1, sp, 0, true);
sp = 1 //enemy speed
path_start(path_lvl1, sp, 0, true);
Place one instance of obj_shop and one of obj_EnemySpawner in the room rm_lvl1.
Save and test the game.
205
BEN TYERS
A GMZ file for this project is available in the download resources.
206
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Chapter 12
207
BEN TYERS
In this chapter you'll utilise all the skills you've learnt so far in this book, and learn some
new ones, to create a side scrolling shooter type game
This game uses a lot of voices and sound effects.
Start by loading in the sounds, as shown below in Figure 12_1 and Figure 12_2.
208
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Next load in the backgrounds. This game will have a splash screen for the main menu,
one for shop, one for each level of the game (which will be created in game, not from an
image), one provided some info for how to play and one providing info on the bonuses. All
backgounds will be 800x400 pixels.
Load in the backgrounds as shown in Figure 12_3 below.
Create five new rooms and set the according background, set all rooms to 800 x 400 pixels
in size, as shown in Figure 12_4 below.
209
BEN TYERS
Next create a new room, name it room_level_1, set it to a width of 15000 and a height of
400. As shown in Figure 12_5.
Figure 12_5. Setting name of room, and setting width to 15000 and height to 400.
Now go to the views tab and set up as shown below in Figure 12_6.
//Declare variables
score=5000;
health=100;
global.weapon=1;
global.level=1;
weapon_no=1;//handgun
global.weapon_info[weapon_no,1]="Hand Gun";//Weapon Name
global.weapon_info[weapon_no,2]=100;//Starting Number
global.weapon_info[weapon_no,3]=1;//Strength
global.weapon_info[weapon_no,4]=1;//Reload Speed
global.weapon_info[weapon_no,5]=1;//Cost
global.weapon_info[weapon_no,6]=5;//Aiming Speed
global.weapon_info[weapon_no,7]=spr_handgun;//Gun Sight for Weapon
global.weapon_info[weapon_no,8]=obj_damage_handgun;//Damage Area
for Weapon
global.weapon_info[weapon_no,9]=snd_handgun;//Sound When Firing
global.weapon_info[weapon_no,10]=snd_voice_handgun_selected;//snd
of voice weapon selected
global.weapon_info[weapon_no,11]=20//ammo pack size
weapon_no=2;//rifle
global.weapon_info[weapon_no,1]="Rifle";//Weapon Name
global.weapon_info[weapon_no,2]=25;//Starting Number
global.weapon_info[weapon_no,3]=2;//Strength
global.weapon_info[weapon_no,4]=2;//Reload Speed
global.weapon_info[weapon_no,5]=2;//Cost
global.weapon_info[weapon_no,6]=3;//Aiming Speed
global.weapon_info[weapon_no,7]=spr_rifle;//Gun Sight for Weapon
global.weapon_info[weapon_no,8]=obj_damage_rifle;//Damage Area for
Weapon
global.weapon_info[weapon_no,9]=snd_rifle;//Sound When Firing
211
BEN TYERS
global.weapon_info[weapon_no,10]=snd_voice_rifle_selected;;//snd of
voice weapon selected
global.weapon_info[weapon_no,11]=15//ammo pack size
weapon_no=3;//machinegun
global.weapon_info[weapon_no,1]="Machine Gun";//Weapon Name
global.weapon_info[weapon_no,2]=100;//Starting Number
global.weapon_info[weapon_no,3]=5;//Strength
global.weapon_info[weapon_no,4]=1;//Reload Speed
global.weapon_info[weapon_no,5]=5;//Cost
global.weapon_info[weapon_no,6]=2;//Aiming Speed
global.weapon_info[weapon_no,7]=spr_machinegun;//Gun Sight for
Weapon
global.weapon_info[weapon_no,8]=obj_machinegun;//Damage Area for
Weapon
global.weapon_info[weapon_no,9]=snd_machinegun;//Sound When Firing
global.weapon_info[weapon_no,10]=snd_voice_machinegun_selected;;//s
nd of voice weapon selected
global.weapon_info[weapon_no,11]=25//ammo pack size
weapon_no=4;//shotgun
global.weapon_info[weapon_no,1]="Shot Gun";//Weapon Name
global.weapon_info[weapon_no,2]=5;//Starting Number
global.weapon_info[weapon_no,3]=25;//Strength
global.weapon_info[weapon_no,4]=10;//Reload Speed
global.weapon_info[weapon_no,5]=50;//Cost
global.weapon_info[weapon_no,6]=4;//Aiming Speed
global.weapon_info[weapon_no,7]=spr_shotgun;//Gun Sight for Weapon
global.weapon_info[weapon_no,8]=obj_damage_shotgun;//Damage Area
for Weapon
global.weapon_info[weapon_no,9]=snd_shotgun;//Sound When Firing
global.weapon_info[weapon_no,10]=snd_voice_shotgun_selected;//snd
of voice weapon selected
global.weapon_info[weapon_no,11]=10//ammo pack size
weapon_no=5;//flamethrower
global.weapon_info[weapon_no,1]="Flame Thrower";//Weapon Name
global.weapon_info[weapon_no,2]=20;//Starting Number
global.weapon_info[weapon_no,3]=50;//Strength
global.weapon_info[weapon_no,4]=4;//Reload Speed
global.weapon_info[weapon_no,5]=50;//Cost
global.weapon_info[weapon_no,6]=4;//Aiming Speed
global.weapon_info[weapon_no,7]=spr_flamethrower;//Gun Sight for
Weapon
212
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
global.weapon_info[weapon_no,8]=obj_damage_flamethrower;//Damage
Area for Weapon
global.weapon_info[weapon_no,9]=snd_flamethrower;//Sound When
Firing
global.weapon_info[weapon_no,10]=snd_voice_flamethrower_selected;//
snd of voice weapon selected
global.weapon_info[weapon_no,11]=5//ammo pack size
weapon_no=6;//nuke
global.weapon_info[weapon_no,1]="Nuke";//Weapon Name
global.weapon_info[weapon_no,2]=1;//Starting Number
global.weapon_info[weapon_no,3]=1000;//Strength
global.weapon_info[weapon_no,4]=10;//Reload Speed
global.weapon_info[weapon_no,5]=1000;//Cost
global.weapon_info[weapon_no,6]=2;//Aiming Speed
global.weapon_info[weapon_no,7]=spr_nuke;//Gun Sight for Weapon
global.weapon_info[weapon_no,8]=obj_damage_nuke;//Damage Area for
Weapon
global.weapon_info[weapon_no,9]=snd_nuke;//Sound When Firing
global.weapon_info[weapon_no,10]=snd_voice_nuke_selected;;//snd of
voice weapon selected
global.weapon_info[weapon_no,11]=2//ammo pack size
Code explanation. This sets up the starting score (which will be used as money), the start-
ing health, the starting weapon number and starting level.
global.weapon array is used to store data for each weapon. This array uses two dimen-
sions the first being used for a weapon number, the second for information (variable, weapon
splash, sounds and sprites).
Place one instance of this object, obj_declare in the first room room_bonuses.
Create a new object, obj_splash_timer. No sprite is rquired. In the create event put the
code:
alarm[0]=90;
Place one instance of this object in each of the rooms: room_bonuses and room_tutorial.
Create a new object, obj_menu_control. No sprite is required. Create a global mouse left
button released event and enter the GML code required to go to the room room_shop. The
GML code is:
room_goto(room_shop);
213
BEN TYERS
Repeat this process for the rest of the gun sprites so you have the sprite resource tree
looking like that below in Figure 12_8.
Next we'll create some splash images. These will be used show how big the damage area
from a weapon shot is.
Load in the required sprites and set the origin as the center, for example:
214
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Next we'll create the objects for these splashes. As we'll be repeating the same process
over and over again, we'll use a parent object. You can assign a parent object from another
object. First we'll create the parent object, obj_parent_splash.
So make an object, obj_parent_splash.
Make an 'End Step' event (note this is different from a normal step event). Then add the
GML to set it to destroy itself, as shown in Figure 12_11.
215
BEN TYERS
Programming like this with an end step event will ensure other objects that collide with
it have their step event executed before it is destroyed.
Now we'll create some objects and assign the parent object.
Create a new object obj_damage_handgun and assign the sprite spr_handgun_splash, then
set the parent to obj_parent_splash, as shown in Figure 12_12.
216
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Figure 12_12. Showing sprite and parent object set for object obj_damage_handgun.
Repeat the process setting the appropriate sprites and setting the parent for the objects
shown in Figure 12_13 below, but do not do the machine gun splash.
217
BEN TYERS
For the machine gun we'll do something different. We'll set up so it shoots 10 bullets in
rapid succession.
First create an object obj_damage_machinegun and set sprite as spr_machinegun_splash
and the parent as object obj_parent_splash.
Close this object.
Create a new object obj_machinegun. In a create event put:
alarm[0]=4;
count=1;
else
{instance_destroy()}
This will check you have ammo if so it will create a weapon splash. Then this will repeat
10 times then destroy itself.
Create a new room, room_shop. Set the background as bg_shop and set as 800x400 in
size.
Create a control object for the shop, obj_shop_control.
In the create event put:
if (global.level==1) {global.level_speed=2}
if (keyboard_check_released(ord(string(ii)))) {
global.weapon=ii;
if (score>global.weapon_info[global.weapon,5]) ||
(score==global.weapon_info[global.weapon,5])
{
218
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
//take money off score
score=score-global.weapon_info[global.weapon,5];
//increase ammo no by one
global.weapon_info[global.weapon,2]+=1;
//play sound purchase complete
sound_play(snd_voice_purchase_complete);
}
else
{
//play not enough cash
sound_play(snd_voice_not_enough_cash);
};
}
if (keyboard_check_pressed(ord("0"))) score+=1000;
Code explanation: This will check for key press of 1 through 6. If you have enough money
it’ll add ammo to the weapon, reduce your cash and play the voice sound for purchase com-
plete. If you don’t have enough money it will play the voice sound not enough cash.
for(i=1;i<7;i+=1){
weapon_no=i;
draw_text(50,50+(weapon_no*50),global. weapon_info[weapon_no,1]);
draw_text(270,50+(weapon_no*50),global. weapon_info[weapon_no,2]);
draw_text(350,50+(weapon_no*50),global. weapon_info[weapon_no,3]);
draw_text(450,50+(weapon_no*50),global. weapon_info[weapon_no,5]);
draw_text(25,50+(weapon_no*50),weapon_no);
};
Place one instance of this object in object room_shop. This will draw some info from the
array onto the screen.
219
BEN TYERS
Next we'll create an object so that the player knows how to use the shop. In a graphics
program of your choice create an image 200x200 pixels with the following info,name as
spr_shop_info, as shown in Figure 12_14.
Create an object, obj_shop_info. Assign this sprite and place one instance in the shop
room (as shown in Figure 12_15).
Now save and test your game. You should now be able to go through the splash screens
and to the shop, which will look like figure 12_15. Check you can buy ammo using keys 1 to
5. Upon clicking the right mouse button you should be taken to the level 1 room, which will
appear as blue.
Next we'll create the main control object for game level.
220
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Firstly we'll create a new sprite, font_level, and set arial size 12 bold.
Create an object obj_weapon_control, no sprite is needed.
if (health<0) {room_goto(room_game_over)};
if (health>100) {health=100};
The above code will change the selected weapon based on key press. It will also check
the value of health, ending game if less than 0, or reducing it if it is above 100.
{
sound_play(global.weapon_info[global.weapon,9]);
global.weapon_info[global.weapon,2]-=1;
instance_create (x,y,global.weapon_info[global.weapon,8])
}
else
{
sound_play(snd_voice_no_ammo)
}
The above code will check you have any of the currently selected weapon's ammo. If
more than 1 it will create an instance of that weapon's splash, otherwise it'll play the voice for
no ammo.
In a global mouse right pressed event put:
global.weapon+=1;
if (global.weapon==7) {global.weapon=1}
sound_play(global.weapon_info[global.weapon,10]);
221
BEN TYERS
In a global mouse middle pressed also put:
global.weapon+=1;
if (global.weapon==7) {global.weapon=1}
sound_play(global.weapon_info[global.weapon,10]);
draw_set_font(font_level);
draw_set_color(c_blue);
//name
draw_text(view_xview+25,view_yview+540,global.weapon_info[global.we
apon,1]);
draw_text(view_xview+220,view_yview+540,"Ammo:");
draw_text(view_xview+350,view_yview+540,global.weapon_info[global.w
eapon,2]);
draw_text(view_xview+450,view_yview+540,"Damage:");
draw_text(view_xview+590,view_yview+540,global.weapon_info[global.w
eapon,3]);
draw_text(view_xview+630,view_yview+540,"Cash:"+string(score));
The above code will draw the weapons info at the bottom of the screen and the sprite for
the currently selected weapon.
Put in a draw health action from the score tab, as shown in Figure 12_16.
222
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Finally, set the depth to -60 as shown in Figure 12_17. Depth can be used to draw objects
in front of or behind other objects. So objects drawn -60 would draw above objects drawn at
0.
Now close any open object windows, then place one instance of object obj_weapon_con-
trol in room room_level_1. Close the room window to apply changes.
Now save and test your game so far. Check that each weapon fires, the machine gun does
multiple shots, and that the health bar is drawn in the correct place and you can't fire a weapon
once you've run out of ammo.
Next we'll create a new type of background called a tile, we'll then add this to the room.
Click the Create a Background button on the title bar.
The load in the brick image and set as follows in Figure 12_18.
223
BEN TYERS
Click OK to close the window. Now open the room room_level_1. Go to the tile tab, then
set snap to 50x50 as shown in Figure 12_19 below.
Figure 12_19. Getting things ready for placing tiles by setting the snap to x 150 and y 100 (the
tile we're using is 150x50).
224
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Place a couple of tiles so the room looks like that in Figure 12_20.
Next scroll the room along by clicking where shown below in Figure 12_21.
225
BEN TYERS
Now open the previously created object obj_follow_me. In the create event put:
226
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
motion_set(0,2);
The above will set the object moving right. When it reaches 14000 the game will restart
(we will change this later).
Place one instance of this object in the far left of room room_level_1 as shown in Figure
12_23.
Now close any open windows. Save and test your game.
Now we'll create some objects to shoot at. First we'll make a tree that shows damage
when hit by a weapon.
227
BEN TYERS
Create a sprite spr_small_tree, and load in two images as shown in Figure 12_24. Set the
sprite origin to the bottom middle of the tree.
228
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Now create another object, obj_big_tree using the larger tree images. Program the same
as above.
Do the same with the wooden barrel. Place several of these in the room for level 1.
Next we'll make a moving object to shoot at. We'll create a cloud that moves across the
screen.
Make it the same as the previous objects, but change the create event code to:
image_no=1;
motion_set(180,3);
The above code will make it move to the left at speed 3. Remember to set the draw event
to the new sprite:
draw_sprite(spr_cloud,image_no,x,y);
if (bbox_right<view_xview) instance_destroy()
This code will draw the sprite, and destroy the instance if it is to the left of the view.
This will help the game to run faster, as if the cloud has gone past the screen it will
never be seen again so it’s wasting processing time by existing.
Close any open windows, open the room and add some of these clouds and barrels in the
level. Close the room to apply changes, then save and test your game.
Next we'll create a duck bonus and bonus level.
229
BEN TYERS
First we'll create the duck bonus room.
Create new room room_duck_bonus.
Set it to 800x400, set the background bg_duck_bonus.
Create an object, obj_duck_rifle_splash, set the sprite to and put the followig code in an
end step event:
instance_destroy();
Step Event:
current_timer-=1;
x=mouse_x
y=mouse_y
if (current_timer<1) {room_goto(room_shop)};
Draw event:
draw_text(390,75,score);
draw_self();
//draw timer:
draw_text(10,10,current_timer);
Load in the duck sprite and name spr_duck, set the origin to the centre.
Create an object, obj_duck_bonus, and set the sprite to spr_duck.
Program as below:
Create event:
alarm[0]=50+random(150);
230
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
can_see=false;
Alarm 0 event:
can_see=true;
alarm[1]=5+random(50);
Alarm 1 event:
can_see=false;
alarm[0]=40+random(50);
Draw Event:
if (can_see==true)
{ draw_self()};
231
BEN TYERS
Save and test your game. Press D on the menu room to test the duck bonus level.
Now we'll set it up so if the player hits ten bonus ducks in the level, at the end they'll be
taken to the bonus duck level.
Create a new object obj_duck_for_level and set a new sprite spr_duck_level, setting
origin as center (note use the smaller duck image from the resource folder).
In a collision event with object obj_parent_splash put the code:
global.ducks_destroyed +=1;
instance_destroy();
Next we'll create an explosion effect that will be used on the enemies.
Create an object, obj_exp_1, and create and set the sprite spr_exp_1, setting the origin to
center.
In the create event put:
image_alpha=1;
The code will change the alpha value (transparancy) of the sprite. It will gradually fade
then destroy itself when faded to 0.
Next we'll create a blood splatter effect used when an enemy shoots back at you. Create
a sprite spr_blood_effect and load in blood effect sprite, as shown below. Set the origin to the
center.
A before the above code will gradually change the transparency, but also make it move
slowly down the screen.
Create a new object, obj_smoke, set the sprite as spr_smoke.
Create event put:
image_alpha=1;
motion_set(90,2);
if (image_alpha<0) {instance_destroy()}
The above will create the same effect as the previous objects, but will move up the screen.
Next load in two sprites, spr_orange_plane_whole, and spr_orange_plane_damaged.
Create an object obj_or_plane_whole and set the sprite spr_orange_plane_whole.
In the create event put:
motion_set(180,2);
my_health=global.level*5;
my_total_health=my_health;
alarm[0]=100+random(100);
234
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
instance_create(x,y,obj_or_plane_damaged);
instance_destroy();
}
if (my_health<1)
if (x <view_xview[0] -40) instance_destroy();
The above code will make a moving plane that randomly shoots back at the player.
Create another object, obj_or_plane_damaged and set the sprite as spr_or-
ange_plane_damaged.
In a create event put:
motion_set(270,4);
235
BEN TYERS
Place the following code in the step event:
if (x <view_xview[0] +840)
{
instance_create(x,y,obj_or_plane_whole);
instance_destroy();
}
The above code will create an instance of obj_or_plane_whole when it's just outside the
view (with view width is 800 so 840 is 40 pixels to the right). The purpose for this is to stop
the plane from shooting at the player when it's not visible on the screen. There are other ways
of doing this, but this method is easy to understand.
The above code will reduce enemy's health by the strength of the currrently selected
weapon.
Create a draw event and place in the following code:
if (my_health>0){
depth=-25;
draw_set_color(c_red);
draw_rectangle(x-50,y-30,x+50,y-36,false);
draw_set_color(c_lime);
draw_rectangle(x-50,y-30,x-50+ (my_health*100/my_total_health)
,
y-36,false);
depth=-20;
draw_self();
}
Close any open windows.
Now open object obj_or_plane_whole and set the object obj_en_parent as a parent, as
shown in Figure 12_30.
236
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Place several instances of object obj_spawn_or_plane in the level between the middle and
the top, as shown in Figure 12_31 below:
238
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Create an object obj_red_plane_damaged and assign the sprite spr_red_plane_damaged.
In a create event put:
motion_set(270,4);
Next we'll create some health bonuses, which the player must shoot to collect.
They are:
1 - Apple
2 - Banana
5 - Melon
10 - Strawberry
25 – Pineapple
Load in the sprites and create folder for them, as shown in figure 12_35 below.
240
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Repeat the above process with each health bonus, changing the value added to health for
each as required. Create a folder and place these items in them, as shown in Figure 12_37.
241
BEN TYERS
Repeat the above procress for all 6 weapons, changing the code weapon=1 to weapon=2
through to weapon=6.
Put these into a folder, so it looks like figure 12_39.
Next we'll create a friendly object that you must not hit with your weapon.
Load in the hot air balloon sprite set as spr_hot_air_ballon and the damaged hot air bal-
loon as spr_hot_damaged. Set the sprite origin to bottom middle of the balloon for each.
Create a path, by clicking the path button on the title bar, as shown in figure 12_40.
242
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Figure 12_40. Creating a new path.
Create a new object, obj_balloon_whole. Add a create event and the set path action from
the move-paths tab. Set as shown in Figure 12_42.
243
BEN TYERS
Figure 12_42. Setting the path to start in create event of obj_balloon_whole.
Next create a collision event with object obj_parent_splash. Put the code:
instance_create(x,y,obj_balloon_damaged);
sound_play(snd_voice_do_not_shoot);
health-=10;
instance_destroy();
244
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
Chapter 1 3
Book Review
245
BEN TYERS
Congratulations for working your way through this book. You should now have a basic
understanding of D&D and GML. The skills you've learnt should enable you to create some
of your own basic games. Continue your learning in the second book in this series.
You should now know how to do the following:
[ ]- Load and save a project
[ ]- Load in a sprite (both singular and sub-images)
[ ]- Load in a sound
[ ]- Load in a background
[ ]- Create a path
[ ]- Create a script
[ ]- Set a font
[ ]- Create an object
[ ]- Create a room, go to another room
246
GAMEMAKER STUDIO BOOK – A BEGINNER’S GUIDE TO GAMEMAKER STUDIO
[ ]- Using arrays
[ ]- Creating and using control objects
[ ]- Loading data from text files
[ ]- Placing objects in a room
[ ]- Setting room name, width and height
[ ]- Using backgrounds as tiles
[ ]- Creating and using basic views
[ ]- Using parent objects
[ ]- A few graphical effects
Extra Ideas
Using the skills you've learnt in the book, here are some extra ideas for you to try:
Brick and Ball Game
1) Create backrounds for each level.
2) Create moving blocks.
3) Create a multi-ball special feature.
4) Create a menu with unlockable levels.
5) Add some game music.
Hangman Game
1) Create extra wordlists which the player can choose from.
2) Improve the quality of the graphics.
3) Create a countdown timer.
4) Create a head-to-head two player game where one player sets the word for the other to
guess.
5) Add sound effects/voices for correct/incorrect guesses.
248