by Ben Tyers
if (number mod 2==0)
{
// will draw if number is even
draw_text(50, 50, string(number)+ " is even");
}
if (age==20) {
// will draw “You Are Twenty” if age is equal to 20
draw_text(50,50, "You Are Twenty");
}
Appendix 2 Conditionals
Conditional statements are used to check and compare variables (and other values such as instance ids, if sounds are playing, keypresses, functions, mouse position, and more).
Therefore, conditional statements will be used often. Having a strong understanding of them is very important. Conditionals can combine with other functions. Conditionals, or combinations of them, can be used to make things happen (or not happen). For example:
Make a ball bounce when it hits a wall
Make an enemy fire a bullet if it can see the player
Play sound effects when an object loses some of its health
Unlock a level if a score is met
Make a player move if a mouse button or key is pressed
Detect the middle mouse button to change a weapon
See if a player has enough cash to buy an upgrade
Check if a player is jumping or not
Create an effect if a weapon is fired, etc.
Determining if a weapon is active or not
Explained in the most basic sense, conditionals evaluate expressions, and will execute and perform actions accordingly. For example, taking the following values:
a=3;
b=2;
c=5;
Would give the following results:
(a+b)==c returns true.
(a==b) returns false.
Note: Use == when using conditionals, rather than a single =.
Actual code will look like this:
if (a+b)==c
{
//do something if true
show_message("true");
}
else
{
// do something if false
show_message("false");
}
In the above example the true result will be processed.
You can add !, which means not. So ! is an expression that negates a logic sentence. So a true sentence turns into a false sentence, and a false sentence turns into a true sentence:
!(a==b) returns true if a is not equal to b.
You can test if a sound is playing or not:
if audio_is_playing(snd_background_music)
{
//do something
}
You can test the pressing of a mouse button:
if (mouse_check_button_pressed(mb_left))
{
//do something
}
You can also check for keyboard presses, for example:
if keyboard_check_pressed(ord(“Q”))
{
//Do something here
}
ord is a function that identifies a keypress of letters and numbers in a way that GameMaker Studio 2 can understand. This is known as virtual keycodes, and also includes a series of constants starting with vk_.
Variables can also be set to true or false:
answer=true;
alive=false;
so:
if (answer)
{
//Do Something
}
would perform any code between { and }.
if (alive)
{
//do something first part here if true
}
else
{
//do something second part here if false
}
would not execute the first part, but it would execute the second part.
You can also use operands and mathematical comparisons when checking a conditional:
a=3;
b=2;
c=5;
(a < b) returns false,
(c > b) returns true.
You can also use <= to check if a value is equal to or less than, and >= to check if a value is equal to or greater than.
You can use the following logic operators, && and and for and, || and or for or. For example, the following will execute code if A and the right arrow are pressed:
if (keyboard_check(ord(“A”))&& keyboard_check(vk_right))
{
//do something if A and right arrow is pressed
}
The following will check either, so it will execute any code if A is pressed or the right arrow is pressed or both are pressed:
if (keyboard_check(ord(“A”))|| keyboard_check(vk_right))
{
//do something if A or right arrow is pressed (or both)
}
Basic Projects
• A) Create a password system where the user has to enter a correct password to continue.
• B) Create a simple text input system using keypresses. Allow the user to enter their name. Then store as global.name when enter is pressed. Limit name to 10 characters
Project Note: Look up usage of keyboard_string
Advanced Projects
• C) Display an object at a random position on the screen for one second. Player must then click where the object appeared. Award points depending on how close the player clicked.
Appendix 3 Drawing
GameMaker Studio 2 has a number of built-in functions for drawing. These include setting drawing colours, setting text fonts, and drawing geometric shapes.
In the most basic terms, drawing items uses an X Y positional system. X relates to pixels across from the top left, Y the number of pixels down from the to. Drawing can be relative to the room position or a view. This and the next section assume drawing in a standard room using default room settings without the use of views. See Figure A_1_1 in Appendix 1 for an explanation of coordinates.
This section serves as an introduction to drawing basic shapes on the screen and familiarization with using X and Y coordinates.
Basic geometric shapes are useful for the following:
Drawing a room border
Creating pop-up boxes
Creating room transitions
Creating effects
Drawing shadows of objects
Note: Due to YYG being a British company, the spelling used is colour, though color can also be used.
Drawing code must be placed in a Draw Event. There are several options available, but for now we’ll just use the main Draw Event. Figure A_3_1 shows how to select this, and the options available.
Figure Figure A_3_1: Showing how to select draw event
Colour constants have built-in values:
Colour
Appearance
RGB Value
c_aqua
0,255,255
c_black
0,0,0
c_blue
0,0,255
c_dkgray
64,64,64
c_fuchsia
255,0,255
c_gray
128,128,128
c_green
0,128,0
c_lime
0,255,0
c_ltgray
192,192,192
c_maroon
128,0,0
c_navy
0,0,128
c_olive
128,128,0
c_orange
255,160,64
c_purple
128,0,128
c_red
255,0,0
c_silver
192,192,192
c_teal
0,128,128
c_white
255,255,255
c_yellow
255,255,0
The following code can be used to set a drawing colorr:
draw_set_colour(c_orange);
Colour can also be set using hexadecimal values prefixed with a '$' character, which in GameMaker Studio 2 is in the format BBGGRR:
draw_set_colour($FFA040);
Or you can set the colour by setting e
ach colour channel:
colour=make_colour_rgb(240, 90, 100);
You can also set the colour using RGB and saving this as a user-defined variable. Obviously, any value for make_colour_rgb should be in the range of 0 to 255. For example:
my_colour=make_colour_rgb(255, 160, 64);
draw_set_colour(my_colour);
draw_circle(50, 50, 25, false);
The above example would draw a red circle at position 50,50 with a radius of 25 and using false draws as a solid circle.
If you were to use true it would only draw the outline.
This code would draw a line from position 100,100 to 200,200 in blue:
draw_set_colour(c_blue); draw_line(100, 100, 200, 200);
The following will draw a solid gray rectangle from 5,5 to 110,110. The last false sets the rectangle to be filled in. Using true would draw the outline only.
draw_set_colour(c_gray); draw_rectangle(5, 5, 110, 110, false);
Other drawing functions that you can use include (again true or false draws filled or border only), for
example:
draw_ellipse(x1, y1, x2, y2, true); //draw an ellipse with outline
draw_point(x, y); // draws a single pixel
draw_roundrect(x1, y1, x2, y2, false); //draws a solid rounded rectangle
draw_line_width(x1, y1, x2, y2, width); //draws a line of given width
draw_triangle(x1, y1, x2, y2, x3, y3, false); //draws a solid triangle
If you're looking for something more advanced, you can look up primitives in the manual. You can open the manual by pressing F1 in GameMaker Studio 2.
Basic Projects
• A) Draw a grid of black and white squares, suitable for playing chess or checkers on. 3 Points
• B) Create a floor plan of the classroom; include furniture, windows, and doors (use different colour for each).
Advanced Project
• C) Draw a picture of the Mona Lisa or one of Piet Mondrian’s paintings using basic drawing shapes..
note on projects for Appendix 3
It’s also possible to draw a sequence of connected lines using primitives. For example:
draw_primitive_begin(pr_linestrip);
draw_vertex(50,50);
draw_vertex(150,50);
draw_vertex(50,150);
draw_vertex(250,50);
draw_vertex(50,250);
draw_primitive_end();
Figure A_3_1: Graph sheet for drawing on
Scale: 1 Square=___ Pixels
Appendix 4 Drawing Continued
There are a number of other functions for drawing images and variables. These can be used separately or combined to create a number of effects. In any game, you’re likely to have a number of sprites and information you want to display on the screen.
For example, images can be used for drawing:
The player
Missiles and bombs
Menu buttons
Walls and platforms Text can be used for:
Scores and health
Player names
Game information
Pop-up text
Game timer
Backgrounds and Foregrounds
Note: Only try to draw the value of a variable if it has already been declared in the Create Event or prior to drawing it; failing to do so may cause an error. Built-in variables health, lives and score are OK to draw without being declared.
Create a new project in GameMaker Studio 2, along with a new object obj_example.
To use drawing functions, they need to be placed within a Drawing Event. Create a Draw Event for the object you just created.
draw_text(100, 100, "Hello World! ");
This will draw the Hello World! sentence in the room at position 100,100 – where this position is the top-left corner of this drawn text.
You can also include strings and reals, by converting the real to a string:
age=20;
draw_text(100, 100, "I am "+string(age)+ " years old");
This will draw the text I am 20 years old sentence on the screen. You can format text too:
Use a different font
Use a colour
Have different horizontal and vertical alignment
Save the code you just wrote, and close the object. Create a new font by right clicking on at the top of the screen. Give the font a name, something like font_myfont, and select a better-looking typeface, for example, Calibri.
Resize the font to about 30 pixels, so the user can see it better. Now, save the font and return to your object’s Draw Event.
Formatting functions need to be applied before drawing text, and they can be applied in any event; however, the best practice is to set drawing directly before drawing any text. This can be in code or by calling a script you’ve set up. For example you can set font, colour, and alignment:
draw_set_font(font_myfont); //Use this font for drawing text
draw_set_colour(c_blue); //Make the text blue
draw_set_halign(fa_center); //Center the text to the x position draw_set_valign(fa_middle); // Center the text vertically to the y position
Note: When you apply formatting, it will remain in place for all objects; ideally you should set the formatting right before any drawing code.
Now, the text will be significantly bigger, since you created a bigger font. It will also appear blue, and its position will be changed because of the horizontal and vertical alignment settings.
Here are some more arguments that you can use with the alignment functions.
For horizontal alignment: fa_left, fa_center, fa_right
For vertical alignment: fa_top, fa_middle, fa_bottom
Note: You can insert a new line using /n
If you want to draw a value of a variable that is not a string, use the string() function with the real variable name, and this will convert it into a string. This will allow you to combine strings and real. If you are drawing just a real, you do not need to convert to a string.
When you apply drawing formatting, like font, colour, alpha, or alignment, it will apply to all drawing, including other objects, until you change to something else. For this reason it is a good idea to apply formatting right before you do any drawing, and to reset alpha back to 1 after you have changed it.
For example, you do the following code in the Create Event of an object, obj_example:
name="Ben";
age=28;
country="England";
food="Pizza";
Which would look like Figure A_4_1:
Figure A_4_1. Showing create event
You can then set a font, for example, as shown in Figure A_4_2:
Figure A_4_2: Setting a font
You can then apply the settings and draw the text on screen, by putting the following code in the Draw Event of obj_example:
draw_set_font(font_myfont);
draw_set_halign(fa_center);
draw_set_valign(fa_middle); draw_set_colour(c_red);
draw_text(300,200,"His name is "+name+". n He is "+string(age)+" years old. n He lives in "+country+". n His favourite food is "+food+".");
Create a room, room_example, and place one instance of obj_example in it. When run, you will see that shown in Figure A_4_3:
Figure A_4_3: Showing example output
Create a new project. Load the sprite from the resources folder, and give it a practical name, something short like spr_test.
Our goal is to draw this sprite in a few different ways, so create an object, obj_test do not assign a sprite, we will be drawing this using code in the Draw Event. Add a Draw Event with the following code to draw a normal sprite on the screen:
/// @description drawing
draw_sprite(spr_test, 0, 200, 200);
Place one instance of this object in room0 and test. This will draw the sprite spr_test, using sub image 0 and position 200, 200.
Sub image refers to which frame of the sprite to use. A sprite can have 0 (which can be useful tool in certain circumstances), 1
, or multiple sub images. They can be used for animations, or to show a different image when facing different directions or performing an action like shooting or climbing a ladder.
If you run the game now, you will see your sprite at the 200,200 position, but what if we want to make the sprite look different? For extra formatting options, use the draw_sprite_ext function:
draw_sprite_ext(sprite, sub image, x, y, xscale, yscale, rotation, colour, 1);
The above is used when you want more flexibility in drawing the sprite. It may also be used to draw the default sprite. It will draw the sub image frame, at the given x and y location, while xscale and yscale set its size, 1 is 100% size, 0.5 would be half size, 2 would be double size. Rotation changes the angle of the image counterclockwise. Colour blends the image colour. An example using draw_sprite_ext(); would be the following, which would draw the sprite spr_enemy, sub image 0, at position 180,120, 50% larger, rotated 25’ counterclockwise with a reddened colour:
draw_sprite_ext(spr_enemy, 0, 180, 120, 1.5, 1.5, 25, c_red, 1);
The colour blending can be used to great effect to give a visual reference of something happening. For example, blending with red can visualize that the enemy has been hit by a bullet.
If your sprite has just one sub image, and no other drawing actions, you don't need to add anything in the Draw Event as the sprite will be automatically drawn, when it is assigned to an object. If you are drawing text or want to draw multiple sprites from a single object and your object has a sprite, you can add this:
draw_self();
If using draw_self(); you may want to manually set which sub image (if you have multiple sub images). You can do this using: