Introduction To Game Design & Programming in GameMaker Studio 2
Page 7
Step Event
/// @description Step Stuff
//Key Press
global.selected+=keyboard_check_pressed(vk_left)-keyboard_check_pressed(vk_right);
if keyboard_check_pressed(vk_left) or keyboard_check_pressed(vk_right)
{
audio_play_sound(snd_menu_select,1,false);
}
//Set Angle
rot-=angle_difference(rot,global.selected*(360/image_number))/ (0.2*room_speed);
//Set Selection
global.item=-global.selected mod image_number;
if (global.item)<0 global.item+=image_number;
global.selected+=keyboard_check_pressed(vk_left)-keyboard_check_pressed(vk_right);
These 2 lines detect a left or right arrow key press and change the value of global.selected accordlingly.
if keyboard_check_pressed(vk_left) or keyboard_check_pressed(vk_right)
{
audio_play_sound(snd_menu_select,1,false);
}
This play a sound effect when left or right is pressed.
//Set Angle
rot-=angle_difference(rot,global.selected*(360/image_number))/ (0.2*room_speed);
This the value of rot, which is used when in the draw event.
//Set Selection
global.item=-global.selected mod image_number;
if (global.item)<0 global.item+=image_number;
Sets which is the currently chosen option. Use when player makes a selection.
Draw Event
/// @description Draw Panels & Info
//Set Variables
var pr, i;
i=0;
//Create a List
pr=ds_priority_create();
//Add
repeat (image_number) {
ds_priority_add(pr,i,lengthdir_y(1,(rot-90)+i*(360/image_number)));
i+=1;}
//Draw
repeat (image_number) {
i=ds_priority_delete_min(pr);
draw_sprite_ext(sprite_index,i,menu_x+lengthdir_x(menu_width/2,(rot-90)+i*(360/image_number)),menu_y+lengthdir_y(menu_height/2,(rot-90)+i*(360/image_number)),1+lengthdir_y(menu_height/2,(rot-90)+i*(360/image_number))/(menu_height*2),1+lengthdir_y(menu_height/2,(rot-90)+i*(360/image_number))/(menu_height*2),0,c_white,1);
}
draw_set_color(c_white);
//Free Memory
ds_priority_destroy(pr);
//draw high
draw_set_font(font_menu);
draw_set_colour(c_black);
draw_set_halign(fa_center);
draw_text(300,20,"Highscore "+string(global.highscore));
var pr, i;
i=0;
//Create a List
pr=ds_priority_create();
//Add
repeat (image_number) {
ds_priority_add(pr,i,lengthdir_y(1,(rot-90)+i*(360/image_number)));
i+=1;}
//Draw
repeat (image_number) {
i=ds_priority_delete_min(pr);
draw_sprite_ext(sprite_index,i,menu_x+lengthdir_x(menu_width/2,(rot-90)+i*(360/image_number)),menu_y+lengthdir_y(menu_height/2,(rot-90)+i*(360/image_number)),1+lengthdir_y(menu_height/2,(rot-90)+i*(360/image_number))/(menu_height*2),1+lengthdir_y(menu_height/2,(rot-90)+i*(360/image_number))/(menu_height*2),0,c_white,1);
}
This code will make a list and add values which are then used to determine where and what size to draw each option.
ds_priority_destroy(pr);
This destroys the list as it is no longer. Destroying lists when done with is very important. Leaving them will eat memory and potentially crash your game.
draw_set_font(font_menu);
draw_set_colour(c_black);
draw_set_halign(fa_center);
draw_text(300,20,"Highscore "+string(global.highscore));
These lines set the drawing style and draws the currently held value of the highscore on screen.
Mouse Wheel Up Event
/// @description Raise Selection
global.selected+=1;
audio_play_sound(snd_menu_select,1,false);
global.selected+=1;
audio_play_sound(snd_menu_select,1,false);
Changes the selected option and plays a sound.
Mouse Wheel Down Event
/// @description Lower Selection
global.selected-=1;
audio_play_sound(snd_menu_select,1,false);
global.selected-=1;
audio_play_sound(snd_menu_select,1,false);
Changes the selected option and plays a sound.
obj_button
This is a button the player can click, which will then take the player to selected room.
This has spr_button assigned, as shown in Figure 7_25:
Figure 7_25: Showing spr_button set up
Draw Event
/// @description Draw Button & Text
draw_self();
x=room_width/2;
draw_set_font(font_menu);
draw_set_colour(c_black);
draw_set_halign(fa_center);
draw_text(x,100,"Scroll With Arrow Keys or Mouse Wheel");
draw_text(x,150,"Click Select To Play");
This sets the drawing style and position and draws the given text.
Left Pressed Event
/// @description Act On Selection
audio_stop_all();
audio_play_sound(snd_menu_button,1,false);
switch (global.item) {
case 0:
room_goto(room_game);
break
case 1:
room_goto(room_intro);
break
case 2:
room_goto(room_how_play);
break
}
This code will take the current value of global.item and use a switch to act accordingly. If you know ahead what a value is before testing it, in a lot of cases a switch is better than using if.
This Section Is For The Game’s Credits Room
obj_sparkle
This object will be used to star field effect in credits and how to play rooms. It is made to move out from a central point in the room.
This object uses spr_sparkle. As shown in Figure 7_26 below:
Figure 7_26: Showing set up for spr_sparkle
Create Event
/// @description Set Up
image_xscale=0.02;
image_yscale=0.02;
image_xscale=0.02;
image_yscale=0.02;
This sets the initial scale size of the sprite.
Step Event
/// @description Rotate and speed up
image_xscale+=0.002;
image_yscale+=0.002;
image_angle+=0.5;
speed*=1.002;
This gradually increases the size, slowly rotates it and increases the speed (in the direction it is already travelling in).
Outside Room Event
/// @description Destroy
instance_destroy();
This event will check if the instance is outside the room borders, if it is it will be destroyed. Deleting instances when no longer needed, for example when outside the room – it is important to destroy them, this helps prevent memory leeks.
obj_intro_control
This sets up the text needed to be shown. It adds this a line at a time to a list, then pulls off each line and processes it.
There is no sprite for this object.
Create Event
/// @description Set Up Text
credits=ds_list_create();
ds_list_add(credits,
"Left Mouse Button To Exit",
"",
"Graphics By",
"",
"GameDeveoperStudio.com",
"",
"",
"Music & Audio Effects",
"",
"SoundImage.org",
"",
"",
"Programmed By Ben",
"",
"Many Than
ks To The Following",
"Who Funded This Project",
"",
"Michał Kamiński",
"Corey Cuhay",
"Honey",
"Pedro Santos",
"Mark Porter",
"Dean Radcliffe",
"Mickey Everett",
"Vasco",
"Mike Cowell",
"Gaven Renwick",
"");
show_debug_message(string(ds_list_size(credits))+" << size");
alarm[0]=room_speed;
audio_stop_sound(snd_music_menu);
audio_play_sound(snd_music_credits,1,true);
The first part of the code creates a list and adds all the text lines that will be displayed.
alarm[0]=room_speed;
This sets an alarm at the room_speed (this is equal to 1 second).
audio_stop_sound(snd_music_menu);
audio_play_sound(snd_music_credits,1,true);
This stops any music playing and plays the sound for the credits on loop.
Alarm 0 Event
/// @description Get Any Text
if ds_list_size(credits)>0
{
text=credits[|0];
ds_list_delete(credits,0);
inst=instance_create_layer(x,room_height+100,"Instances", obj_intro_text);
inst.text=text;
alarm[0]=room_speed*1.5;
}
else
{
alarm[1]=room_speed*8;
}
if ds_list_size(credits)>0
{
text=credits[|0];
ds_list_delete(credits,0);
This checks if the list still has content, if it has it sets the value of text to the next value and then deletes from the list.
inst=instance_create_layer(x,room_height+100,"Instances", obj_intro_text);
inst.text=text;
alarm[0]=room_speed*1.5;
This makes an instance of obj_intro_text and sends it the value of text. It then sets the alarm to 1 and a half seconds.
else
{
alarm[1]=room_speed*8;
}
This sets the alarm 1 to 8 seconds if the list is empty. This 8 seconds gives time for any text on the screen to roll up.
Alarm 1 Event
/// @description Do Something When All Text Done
room_goto(room_menu);
Goes back to the menu room when triggered.
Global Left Pressed Mouse Event
/// @description Go To Menu Room
audio_stop_all();
audio_play_sound(snd_menu_button,1,false);
room_goto(room_menu);
If the player clicks the left mouse button, it will go back to the menu room.
obj_intro_text
This object has text sent to it when it is created. The text then moves up and reduces in size.
There is no sprite for this object.
Create Event
/// @description Set Up
size=5;//Start Size 5x font size
x=room_width/2;//Set In Center
start=false;//prevent changing size intil in room
Sets initial values.
Step Event
/// @description Move & Change Siz
if y
{
size-=0.024; //how quickly to scale down
}
y-=4;//how quickly to move up
if size<0.01 //when invisible
{
instance_destroy();
show_debug_message("Destroyed");
}
if y
{
size-=0.024; //how quickly to scale down
}
Checks if the instance is on screen, if it is reduces size each step.
y-=4;//how quickly to move up
Changes the value of y by -4 each step, making the instance move upwards.
if size<0.01 //when invisible
{
instance_destroy();
show_debug_message("Destroyed");
}
Destroys when size is below 0.01.
Draw Event
/// @description Draw The Text
draw_set_font(font_intro_text);
draw_set_colour(c_white);
draw_set_halign(fa_center);
draw_set_valign(fa_middle);
draw_text_transformed(x,y,text,size,size,0);
Sets the drawing style, and then draws the text with the given size.
obj_emitter
This object creates the sparkle objects for the star field effect.
There is no sprite for this object.
Create Event
/// @description Set Up
alarm[0]=5;
Sets alarm 0 to 5 (1/6 of a second)
Alarm 0 Event
/// @description Create Sparkle
alarm[0]=room_speed/2;
sparkle=instance_create_layer(room_width/2,room_height/2, "Instances",obj_sparkle);
sparkle.direction=irandom(360);
sparkle.speed=4+random(4);
alarm[0]=room_speed/2;
Resets alarm 0 to ½ a second.
sparkle=instance_create_layer(room_width/2,room_height/2, "Instances",obj_sparkle);
sparkle.direction=irandom(360);
sparkle.speed=4+random(4);
Creates an instance of obj_sparkle in the middle of the room, sets it to move outwards at a speed between 4 and 8, in a random direction.
This Section Is For The Game’s How To Play Room
The objects and code used here is a duplicate of that for the game credits, with text in the Create Event for obj_intro_control changes accordingly.
This Section Is For The Game Room
obj_player
This is the main player object that the user will control. It is set up to move on keypesses, within a given region, and move back to it’s starting position when no key is pressed. This object can also shoot projectiles (when they are active) with the mouse buttons. It also checks for collisions with enemies or enemies’ projectiles. It also draws a health bar at the bottom of the window.
This object uses the sprite spr_player, which is the same as that used for the menu room. It is shown again for consistency, in Figure 7_27:
Figure 7_27: Showing sprite used for the player.
Create Event
/// @description Set up
flying_level=room_height/2;
xx=x;
power_active=false;
shield=0;
alarm[10]=room_speed*6;
can_shoot_1=true;
can_shoot_2=true;
///show info
info=instance_create_layer(x,y,"Foreground",obj_info);
info.image_index=1;
//for damge
flash=false;
flying_level=room_height/2;
xx=x;
ang=0;
power_active=false;
shield=0;
This sets the initial values needed. flying_level and xx will be used later for setting the parallax effect and moving the player back to the starting point. power_active sets whether the power weapon upgrade is active or not. The value shield is used to determine if the player has a shield, which be set to 1 when active and then count down.
alarm[10]=room_speed*6;
alarm 10 is used to trigger the power weapon (see Alarm 10 Event).
can_shoot_1=true;
can_shoot_2=true;
These two flags are used for players 1 and 2 weapons, which the player can fire using mouse buttons when it is active.
info=instance_create_layer(x,y,"Foreground",obj_info);
info.image_index=1;
This creates an instance of obj_info and tells it to display subimage 1.
flash=false;
This flag is used to show the player flash or not, when damaged. This used in unison with Alarm 0, see below.
Step Event
/// @description Object ma
nagement
global.difference=(flying_level-y)/10;//set as a global value as it will be used for parallax background
image_angle=global.difference;
//keep in screen
y=clamp(y,80,room_height-80);
//move
//up down
nokey=keyboard_check(ord("W"))+keyboard_check(ord("S"));
if nokey==0
{
if y
if y>flying_level y-=3;
}
if keyboard_check(ord("W"))
{
y-=6;
}
if keyboard_check(ord("S"))
{
y+=6;
}
//forward back
if keyboard_check(ord("D"))
{
x+=6;
}
if keyboard_check(ord("A"))
{
x-=6;
}
x=clamp(x,xx-40,xx+180);
nokey=keyboard_check(ord("A"))+keyboard_check(ord("D"));
if nokey==0
{
if x
if x>xx x-=3;
}
//backgrounds
diff=(flying_level-y)/3;
layer_y("bg_1",550+diff);
layer_y("bg_2",(450+diff)/2);
layer_y("bg_3",(-100+diff)/3);
if global.level>8 power_active=true;
This code:
global.difference=(flying_level-y)/10;//
And this:
diff=(flying_level-y)/3;
layer_y("bg_1",550+diff);
layer_y("bg_2",(450+diff)/2);
layer_y("bg_3",(-100+diff)/3);
Adjusts the y (vertical) position of the background, based on the player’s y position. This helps create a nice and smooth parallax effect.
y=clamp(y,80,room_height-80);
This checks the y value of the player and checks it is range of between 80 and room_height less 80, if it outside this range the value will be changed. This is a great function, which would need several lines of code if coded in directly.
nokey=keyboard_check(ord("W"))+keyboard_check(ord("S"));
if nokey==0
{