Advanced GameMaker - Rupert
Advanced Game Maker
Tony Forster Aug 07 for GameMaker version 6.1
May be copied or modified with acknowledgement of authorship
Tony Forster
23/7/07
Variables ............................................................................................................................. 2
Execute a piece of code................................................................................................... 2 Setting and testing variables ........................................................................................... 2 Common variables .......................................................................................................... 3 Scope and visibility ......................................................................................................... 4
Persistence....................................................................................................................... 5
Sprites and Drawing............................................................................................................ 6
Steering a car................................................................................................................... 6
The clock......................................................................................................................... 8
Individual health bars...................................................................................................... 9 Entering Data .................................................................................................................... 10
Miscellaneous.................................................................................................................... 10
Random ......................................................................................................................... 10
With............................................................................................................................... 11
Self, other, all, instance ................................................................................................. 11
What do these do? Try them ......................................................................................... 11 More reading ................................................................................................................. 12
Gamemaker Code – self paced tutorial ............................................................................. 12 Getting started ............................................................................................................... 12
Making Teddy Move..................................................................................................... 13 Something to shoot at.................................................................................................... 15 OK Lets Shoot!! ............................................................................................................ 18
Kill Ghosts .................................................................................................................... 20
Links.................................................................................................................................. 23
Resources .............................................................................................................. 23
Forum .................................................................................................................... 23
Kids work .............................................................................................................. 23
Book ...................................................................................................................... 24
Variables
Execute a piece of code Rather than drag and drop actions, you can use the more powerful code. The “execute a
piece of code” action looks like this:
You can type code into this window or paste to it from help or elsewhere. Note how the
colour changes when Gamemaker recognises something, for example:
if(mouse_x<200 && mouse_y<200) window_set_cursor(cr_hourglass) else window_set_cursor (cr_default)
light blue for variables
dark blue for functions
red for constants
magenta for objects
green for comments
bold for language words
If you don?t get the colour you expect, you have made a typing error
Try this,
new game
add an object
add event step
execute a piece of code
if(mouse_x<200 && mouse_y<200) window_set_cursor(cr_hourglass) else window_set_cursor (cr_default)
new room
place object in room
run
Setting and testing variables You can use code fragments in “set the value of a variable”
and test with “if a variable has a value” or “if an expression is true”
or
Common variables
x Is an object?s x-position.
y Is an object?s y-position.
Example:
new game
new sprite
new object
set the sprite for the object add event keypress set the value of a variable x=100
y=100
new room
place object in room
run
it jumps to position 100 100 when you press space
hspeed Horizontal component of the speed.
Example
In the keypress event
hspeed =10 move to the right speed 10 when you press right cursor
vspeed Vertical component of the speed.
direction Its current direction (0-360, counter-clockwise, 0 = to the right).
speed Its current speed (pixels per step).
X-coordinate of the mouse. mouse_x
mouse_y Y-coordinate of the mouse.
Example:
In the step event
x= mouse_x
y= mouse_y the object moves with the mouse
Scope and visibility
There are some reserved names
mouse_x
mouse_y
health
score
lives
room_width
room_height
which are global
most variables belong to the object within which they are defined
there is also the global object global eg global.x
So for code within object0 , x is the horizontal position
to refer to it from inside another object use object0.x (is the x value or horizontal
position of object0)
See
The object "inventory" is used to keep track of diamonds, the variable "diamonds" is the
number of diamonds collected Create Event: set variable diamonds to 0
When you collide with the diamond it is destroyed and the inventory count increases by 1 Collision Event with object object1: set variable inventory.diamonds relative to 1 for other object: destroy the instance
diamonds lives inside the inventory object, when referred to inside inventory, you can
just call it diamonds, when referred from another object, you must use the full name
inventory.diamonds
In the draw event for inventory, a box and the collected diamonds are drawn Draw Event: set the fill color to 16777215 and line color to 0 draw rectangle with vertices (0,0) and (200,40)
A white box is drawn
set variable i to 0 repeat next action (block) diamonds times at position (20*i,0) draw image -1 of sprite sprite1 set variable i relative to 1
The sprite is drawn "diamonds" times
i is increased each time it is drawn, it is drawn further to the right each time as i increases
Persistence
Objects and rooms can be persistent
Normally objects and rooms are created fresh every time See
The balls in room 0 stay where left
in room 1 they revert to original positions
See
Move the dog with the mouse
When the dog walks through paint, it is that colour in all rooms Turn off persistent for the dog and it starts anew in each room
Sprites and Drawing
Steering a car
We will rotate a car using its inbuilt variable “direction”
Add a sprite
edit sprite
double click image0
zoom in with magnifying glass
Draw a car facing left
Note the colour of the bottom left pixel becomes transparent
To draw this at 10 degree rotation, close with the green tick
animation|rotation sequence|counterclockwise
now add an object and room
put the object in the room
in the object?s step event
move in the direction of a point mouse_x, mouse_y at speed 1
change the sprite, sprite0, subimage direction/10
Now we will display some text
Delete the change sprite in the step event Instead use draw sprite in the draw event
It should work the same
Add a font, (Arial 12 is OK)
In the draw event,
set the font to font0
Draw a text: ""+string(direction), 0,20 relative
The clock
Add object
Add room
Room properties, settings 400x400 Place object in room
Draw event, execute a piece of code draw_circle(200,200,130,true) see help game graphics, drawing shapes
Run it, that?s the circle
minuteangle=2*pi*current_second/60
x2=200+100*sin(minuteangle)
y2=200-100*cos(minuteangle)
x2,y2 is the tip of the second hand and 200,200 the centre
draw_line(200,200,x2,y2)
Now repeat for minute and hour, a little shorter you will need current_minute and current_hour which are in help under gameplay,
timing
Add the hour marks
for(i=0; i<12; i+=1)
{
x1=200+110*sin(2*pi*(i)/12)
y1=200-110*cos(2*pi*(i)/12)
x2=200+125*sin(2*pi*(i)/12)
y2=200-125*cos(2*pi*(i)/12)
draw_line(x1,y1,x2,y2)
}
Completed clock at
Add the day and date, maybe even an alarm
Individual health bars
There is a global variable, “health” and you can draw the health for one object
New sprite
new object
new room
place object in room
draw event, draw a sprite, sprite0 0,0 relative
from the score tab, draw the health bar, -20,30,20,40 relative
But health is global and you can?t have instances with different health, test this with multiple
instances and eg mouse click reducing health
Delete: draw a health bar
create event, set the value of a variable, energy ,30 draw event
set the colour to green
draw rectangle –20,30,-20+energy,40, filled, relative (this is the energy) set the colour to black
draw rectangle –20,30,20,40, outline, relative (this is the outline)
test this with multiple instances with differing energy
See
Tony Forster
Entering Data
See
The rest are code
result = show_menu('menu0|menu1|menu2',0) show_message('menu '+string(result)+' selected') if(show_question('yes or no')=1)
show_message('you said yes')
else
show_message('you said no')
show_message('squared is' + string(sqr(get_integer('enter a number',0))))
show_message('you entered:' +get_string('enter text','no string'))
show_message('colour number '+string(get_color(0)))
Miscellaneous
Random
random(x)
random(x) Returns a random real number between 0 and x. The number is always
smaller than x.
With
In addition to the usual if () repeat () while () do until() for ( ; ;)
there is the with construction
with (object) repeats statement for all instances of object
Self, other, all, instance self // the object containing the code
other // the other object in the collision
all // all instances of all objects in the room
object0 // all instances of object0 in the room
What do these do? Try them (You can paste these from this document directly into the “execute a piece of code”
window)
In the step event:
if(mouse_x<200 && mouse_y<200) window_set_cursor(cr_hourglass) else window_set_cursor (cr_default)
in a keypress event
for (i=0 ; i<10 ; i=i+1) instance_create(x+10*i,y+10*i,object0)
What?s the difference between
with (object0) instance_destroy() and instance_destroy()when placed in object0?s code? in another object?s code
In a keypress event:
instance_create(random(400),random(400),object0)
In the step event:
if (x<0) hspeed=5
if (x>300) hspeed=-5
try this in the draw event:
draw_sprite(sprite0,-1,x-5,y)
draw_sprite(sprite1,-1,x+5,y)
instance_create
instance_create (x,y,obj) Creates an instance of obj at position (x,y). Example:
In a keypress event
instance_create (100,100,object0) create an object0 at position 100 100 of the screen
instance_change
instance_change (obj,perf) Changes the instance into obj. perf indicates whether to
perform the destroy and creation events.
Example:
instance_change (object0,false) change into object0 without performing the destroy and creation events
More reading
Go to gamemaker help contents and look at The Gamemaker Language, there are heaps
of useful things there.
Gamemaker Code – self paced tutorial
Getting started
Just like any other Gamemaker game, you will need a sprite
I?ll choose the teddy
You will need an object
You need to assign the sprite to the object
You need a room
You need to put the teddy in the room
Making Teddy Move
OK so far its all been the same as what we?ve done with drag and drop. Now we?ll make
teddy move with the arrow keys but we?ll use code.
Add event keyboard left
Drag an “Execute a piece of code” action into the action window
See how a window opens up
When we hit keyboard left we want teddy to move left, we want to set the horizontal
speed to a negative value because in Gamemaker, positive is too the right. There?s an
inbuilt variable for every object, the horizontal component of speed hspeed.
How did I know that? Its in the help.
All the code stuff is at the end under Game Maker Language (GML)
I found about hspeed in Gameplay – Moving around
hspeed Horizontal component of the speed
Lets make hspeed –5
Note how hspeed is in blue, Gamemaker recognised it as a variable
Just like hspeed is the horizontal component of speed, vspeed is the vertical component.
Now add event keyboard right, make hspeed =5
add event keyboard up make vspeed =–5
add event keyboard down make vspeed =5
See how we can move teddy with the cursor keys. Now I want teddy to stop when I?m not pressing a key.
Add event keyboard no key
And drag in the “execute a piece of code” action
Now I could type
vspeed=0
hspeed=0
but I read the help and noticed another variable speed so I?ll just type
speed=0
Ok it works. What next?
Something to shoot at
Shooting is not „politically correct?. Big problem. Ghosts don?t have rights. Lets shoot
ghosts.
Like before get a new sprite
Get a new object, assign the sprite to the object and put the object in the room
Ok, we want the ghost to move in a random direction
Add a create event for the ghost
Drag an “Execute a piece of code” action into the action window
We?ll give the ghost a fixed speed but a random direction
speed=5
direction=random(360)
How did I know about the variables speed and direction?
Found them same place as vspeed and hspeed
How did I know about the function random
random(x) Returns a random real number between 0 and x. The number is always smaller
than x
Lets keep the ghost in the room by making it wrap
Add event other / outside room
Add this code to make it wrap
move_wrap(true,true,0)
Where did move_wrap(true,true,0) come from?
move_wrap(hor,vert,margin) Wraps the instance when it has left the room to the other side. hor indicates whether to wrap horizontally and vert indicates whether to wrap vertically.
margin indicates how far the origin of the instance must be outside the room before the wrap happens. So it is a margin around the room. You typically use this function in the
Outside event.
Note how true is red, it is a pre-defined constant
OK Lets Shoot!!
We need a sprite for our bullet, an object for our bullet and we need to assign the sprite to the object. Do it. The bullet should be object2
Now, we?ll shoot bullets from the teddy when we press space
For object TEDDY add event keyboard space
And add the following code
instance_create(x,y,object2)
(I?m assuming that object2 is your bullet too)
Because we are inside the teddy object, x and y refer to the position of the teddy so the bullet appears where the teddy is, position x,y
Where did instance_create(x,y,object2) come from?
instance_create(x,y,obj) Creates an instance of obj at position (x,y). The function returns
the id of the new instance
Small problem, the bullet doesn?t move
For the bullet, add event create
speed=10
direction=object0.direction
The bullets are created with speed=10 and their direction is set to the same direction as
the teddy
What?s the dot all about?
In direction=object0.direction the dot “.” is important
If we are inside an object, x, y , vspeed, hspeed etc refer to that object, if we want to refer
to other objects we use the dot
Kill Ghosts
Ok when the bullet collides with the ghost we destroy the ghost. Easy.
For object ghost add the collision event with the bullet
And add the following code
instance_destroy()
Where did instance_destroy() come from?
In case its not working for you, here?s the pseudo code for my program. You can display
pseudo code with alt / edit / show object information
My teddy is object0, ghost object1 and bullet object2
Information about object: object0
Sprite: sprite0 Solid: false Visible: true Depth: 0 Persistent: false Parent: Mask: Keyboard Event for Key: execute code: speed=0 Keyboard Event for Key: execute code: instance_create(x,y,object2) Keyboard Event for Key: execute code: hspeed=-5 Keyboard Event for Key: execute code: vspeed=-5 Keyboard Event for Key: execute code: hspeed=5 Keyboard Event for Key: execute code: vspeed=5 ______________________________________________________
Information about object: object1 Sprite: sprite1 Solid: false Visible: true Depth: 0 Persistent: false Parent: Mask: Create Event: execute code: speed=5 direction=random(360) Collision Event with object object2: execute code: instance_destroy() Other Event: Outside Room: execute code: move_wrap(true,true,0) ______________________________________________________
Information about object: object2
Sprite: sprite2 Solid: false Visible: true Depth: 0 Persistent: false Parent: Mask: Create Event: execute code: speed=10 direction=object0.direction ______________________________________________________
Total for the above, 10 marks
Add a sound when you shoot 1 mark Hint:
You?ll find sound_play(sound0) at
When you shoot all ghosts, go to the next room 1 mark
Hint:
Use if , instance_number() and room_goto_next()
Make homing missiles 1 mark Hint:
move_towards_point()
Show the score 1 mark
Show a dialogue box congratulating you when you have won the game
1 mark
Show the help screen when you first start the game 1 mark
When you shoot the ghost boss, have 10 ghosts spawn at random places which you then
have to shoot 1 mark
Hint:
Use repeat () random()and instance_create()
In the above, instead of using repeat () use for()
1 mark
In the above, instead of using repeat () use do
1 mark
() use while() In the above, instead of using repeat
1 mark
Links
Resources
www.gamemaker.nl
Forum
Kids work
, Gamemaker year 1 to 8
, years 5 & 6
, Gamemaker games
High School, Tasmania Australia, Gamemaker
helps serve the communication and collaboration needs of an Australian Year 3 class. It is also an exploration and demonstration to help cater for the needs of other primary school classes
Overseas School of Colombo
Gamemaker games from El Paso Country Day School (K-12)
High School and New Plymouth Girls' High have been taught to create games that are fun to play while learning about programming, logic and graphics design. ~bbair/WIC/games4girls/ a workshop for 2006 Women in
Science day, called "Computer Games for Girls". Seventeen girls, ages 13-14, attended the workshop
in the course, Foundations of Interactive Game Design, taught at the Univ. of California, Santa Cruz, in Winter quarter 2006
/SummerCamp/2005/Games/Students
students from Kurwongbah State School traveled to Dakabin High School to use the computer program Game Maker to design and make our own computer games
Book
Beginners/dp/1590596153
本文档为【Advanced GameMaker - Rupert】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。