INTRODUCTION TO GML:

INTRODUCTION TO GML:
DRAWING SHAPES WITH THE DRAW FUNCTION
F
unctions are the things that let GameMaker
perform certain tasks in your game. For every
action there is a corresponding function, and
there are close to a thousand functions that are
available that allow you to deal with everything from
motion, instances, graphics, sound and user input.
When we use a function, we say that we call the
function. A function call consists of the name of the
function, followed by the arguments, separated by
commas, in parentheses. Even when a function has no arguments, you must still use the parentheses.
Arguments can be values as well as expressions. Most functions have a fixed number of arguments, but
some can have an arbitrary number of arguments.
DRAW FUNCTIONS
GML provides a large number of functions for drawing objects. These functions should normally only be
used in the Draw event of objects. There are functions to draw shapes, sprites, backgrounds, text, etc.
DRAWING RECTANGLES
To draw a rectangle, there is a function called draw_rectangle() that takes five arguments: the x- and yposition of the top-left corner of the rectangle, the x- and y-position of the bottom-right corner of the
rectangle, and a true or false value indicating whether or not to draw the outline of a rectangle (true) or
draw a filled in rectangle (false).
The syntax looks something like this:
draw_rectangle(x1, y1, x2, y2, outline)
So let’s try drawing a rectangle in our program:
1. Create a new Object called obj_rectangle.
2. Create a Script and called it scr_rectangle.
3. Write the following line of code in your script to draw a rectangle:
draw_rectangle(100, 100, 200, 150, true)
4. Add a Draw event in your object (obj_rectangle), add the Execute Script action into the
Actions window, and choose your script from the menu:
Drawing Shapes with the Draw Function
Page 1 of 13
5. Add the object to your room.
When you run the program, you will see the outline of a rectangle drawn on the screen.
100, 100
200, 150
If you want a filled in rectangle, you would just need to change the last argument in the function from
true to false:
draw_rectangle(100, 100, 200, 150, false)
Drawing Shapes with the Draw Function
Page 2 of 13
SETTING COLOUR
To change the colour that is used to draw the shape, you need to use a function called
draw_set_color() and use one of the numerous colours included in GameMaker. In the following
example, we will use blue:
draw_set_color(c_blue)
Drawing Shapes with the Draw Function
Page 3 of 13
Of course, you can create your own custom colour by using the make_color_rgb() function and
passing three arguments representing how red, green and blue. Make note that the values that you pass
to the function cannot be less than 0 or greater than 255.
In the following example, I am going to use a specific shade of pink as my colour:
var col;
col = make_color_rgb(255, 100, 255)
draw_set_color(col);
draw_rectangle(100, 100, 200, 150, false)
Drawing Shapes with the Draw Function
Page 4 of 13
The first thing we needed to do was declare a variable to store the colour. I made it a local variable and
called it col. Next we created an assignment statement that made the variable col equal to a colour that
I created using the make_color_rgb() function where I passed the values 255, 100, 255. Once the
colour has been made, we need to pass the variable that is storing the colour into the
draw_set_color() function.
DRAWING CIRCLES
Drawing a circle can be done in GameMaker by using the draw_circle() function which takes four
arguments: the x-coordinate of the circle, y-coordinate of the centre of the circle, the circle’s radius (i.e.
the length from its center to its edge), and a true or false value indicating whether the circle will be filled
in (false) or not (true).
The syntax looks like this:
draw_circle(x, y, radius, outline)
EXAMPLE:
draw_set_color(c_green)
draw_circle(150, 140, 50, true)
Drawing Shapes with the Draw Function
Page 5 of 13
50
150, 140
DRAWING LINES
Drawing a line can be done using the function called draw_line() which takes four arguments: the xcoordinate of the start of the line, the y-coordinate of the start of the line, the x-coordinate of the end of
the line, and the y-coordinate of the end of the line.
The syntax looks like this:
draw_line(x1, y1, x2, y2)
EXAMPLE:
draw_line(100, 140, 200, 140)
Drawing Shapes with the Draw Function
Page 6 of 13
100, 140
200, 140
To set the width of the line, you can use the function draw_line_width() that takes one additional
argument indicating the width of the line in pixels. The syntax looks like this:
draw_line(x1, y1, x2, y2, width)
EXAMPLE:
draw_line(100, 140, 200, 140, 10)
10 pixels
wide
Drawing Shapes with the Draw Function
Page 7 of 13
DRAWING ROUNDED RECTANGLES
Drawing a rounded rectangle can be done using the function called draw_roundrect() which takes five
arguments: the x-coordinate of the top-left corner of the rectangle, the y-coordinate of the top-left corner
of the rectangle, the x-coordinate of the bottom-right corner of the rectangle, the y-coordinate of the
bottom-right corner of the rectangle, and whether the rectangle is drawn filled (false) or as an outline
(true).
The syntax looks like this:
draw_roundrect(x1, y1, x2, y2, outline)
EXAMPLE:
draw_set_color(c_red)
draw_roundrect(50, 100, 250, 180, false)
50, 100
250, 180
DRAWING TRIANGLES
Drawing a triangle can be done using the function called draw_triangle() which takes seven (7)
arguments: the x-coordinate of the triangle’s first corner, the y-coordinate of the triangle’s first corner,
the x-coordinate of the triangle’s second corner, the y-coordinate of the triangle’s second corner, the xcoordinate of the triangle’s third corner, the y-coordinate of the triangle’s third corner, and whether the
triangle is drawn filled (false) or as an outline (true).
The syntax looks like this:
draw_triangle(x1, y1, x2, y2, x3, y3, outline)
EXAMPLE:
Drawing Shapes with the Draw Function
Page 8 of 13
draw_set_color(c_olive)
draw_triangle(150, 100, 250, 200, 50, 200, false)
150, 100
50, 200
250, 200
DRAWING ELLIPSES
Drawing an ellipse can be done using the function called draw_ellipse() which takes five (5)
arguments: the x-coordinate of the left of the ellipse, the y-coordinate of the top of the ellipse, the xcoordinate of the right of the ellipse, the y-coordinate of the right of the ellipse, and whether the ellipse
is drawn filled (false) or as an outline (true).
The syntax looks like this:
draw_ellipse(x1, y1, x2, y2, outline)
EXAMPLE:
draw_set_color(c_teal)
draw_ellipse(50, 125, 250, 175, false)
Drawing Shapes with the Draw Function
Page 9 of 13
125
50
250
175
DRAWING ARROWS
Drawing an arrow can be done using the function called draw_arrow() which takes five (5) arguments:
the x-coordinate of the start of the line, the y-coordinate of the start of the line, the x-coordinate of the
end of the line, the y-coordinate of the end of the line, and the length of the arrow in pixels.
The syntax looks like this:
draw_arrow(x1, y1, x2, y2, size)
EXAMPLE:
draw_set_color(c_orange)
draw_arrow(150, 50, 150, 250, 50)
Drawing Shapes with the Draw Function
Page 10 of 13
150, 50
50
250, 50
DRAWING BUTTONS
Drawing a button can be done using the function called draw_button() which takes five (5) arguments:
the x-coordinate of the top-left corner of the button, the y-coordinate of the top-left corner of the button,
the x-coordinate of the bottom-right corner of the button, the y-coordinate of the bottom-right corner of
the button, and whether the button is up (true) or down (false).
The syntax looks like this:
draw_button(x1, y1, x2, y2, size)
EXAMPLE:
draw_set_color(c_ltgray)
draw_button(100, 120, 200, 180, true)
Drawing Shapes with the Draw Function
Page 11 of 13
To draw a button which will appear pressed if the left mouse button is held down, you can use the
mouse_check_button() function and pass the left mouse button as an argument:
draw_button(100, 120, 200, 180, !mouse_check_button(mb_left))
DRAWING TEXT
To draw text in a room, first you will need to create a Font and then add a Draw event. Within the
Draw event, you can use the draw_text() function which takes three arguments: the x-coordinate of
the drawn string, the y-coordinate of the drawn string, and the string itself that you want to draw.
The syntax looks like this:
draw_text(x, y, str)
EXAMPLE:
draw_set_font(font_arial)
draw_text(50, 100, “I love GameMaker!”)
Drawing Shapes with the Draw Function
Page 12 of 13
Drawing Shapes with the Draw Function
Page 13 of 13