© The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature 2023
B. TyersGameMaker Fundamentalshttps://doi.org/10.1007/978-1-4842-8713-2_3

3. Drawing Shapes

Ben Tyers1  
(1)
Worthing, West Sussex, UK
 

GameMaker is very flexible when it comes to drawing. There are several built-in functions for drawing basic and advanced geometric shapes. Drawing is done by positioning sprites or vertices within the room. In GameMaker, x relates to how many pixels across the screen and y for how many down. The position at the top left is 0,0. This chapter will introduce you to the basics of drawing.

Basic geometric shapes can be used for such things as
  • Drawing the border of a room.

  • Making popup text boxes.

  • Creating effects when changing rooms.

  • Making various graphical effects.

  • Drawing grids, for example for inventory.

Note YoYo games is a British company, spelling is in British English, though American spelling can be used, for example colour or color. For example: draw_set_colour(c_red) or draw_set_color(c_blue), you can set your preferred spelling in the Preferences. Drawing code must always be placed within a Draw Event. There are several options available for drawing, but for now we’ll just focus on the Draw Event. Figure 3-1 shows how to select this event.

A screenshot of an object example to select the draw events. Object example has a name, example, sprites, collision mask, and select events. Event has add event option tabs that lead to select draw from the dropdown option.

Figure 3-1

Showing how to select draw event

There are several built-in color constants that can be used, see Figure 1-9 back in Chapter 1.

The following code can be used to set a drawing color:
draw_set_colour(c_blue);
Colors can also be set using their hexadecimal values prefixed with a ‘$’ character, which in the format used in GameMaker is in BBGGRR, for example:
draw_set_colour($00ff00);
You can also set the color using the more known RGB color system. The function for this is make_colour_rgb, an example being:
var my_colour=make_colour_rgb(15,90,255);
draw_set_colour(my_colour);
draw_circle(100,100,80,true);

which would draw the outline of a circle in the defined color. Setting the value true to false would draw a solid circle.

Note

You can set up global values at game start, so your defined colors are accessible by all objects in your game. Very useful if you will be using a lot of custom color in your game. For example: global.colour_mix_1=make_colour_rgb(80,90,60);

You can draw a single line in green between two points:
draw_set_colour(c_green);
draw_line(10,10,90,90);
The following will draw a solid yellow rectangle. Using true would draw the outline only:
draw_set_colour(c_yellow);
draw_rectangle(25,25,90,90,false);
There are many other shapes that can be drawn, again false for filled or true for outline only:
draw_ellipse(x1,y1,x2,y2,false); //draws a solid ellipse
draw_roundrect(x1,y1,x2,y2,true); //draw an outline of a rectangle with rounded corners
draw_triangle(x1,y1,x2,y2,x3,y3,true); //draws an outline of a triangle

There are many other drawing shapes and functions. Look up the manual for functions starting with draw_ and have a play-around using them.

Note

When in the code window, you can click on a function with the middle mouse button to quickly look up its entry in the manual.

Basic Projects

  1. A)

    Draw a chessboard in black and white, using appropriate drawing functions.

     
  2. B)

    Use draw functions to draw a floor plan of the room you are in. Use different colors for chairs, tables, lockers, etc.

     

Advance Project

  1. C)

    Using drawing functions to draw a representation of the Mona Lisa, limit yourself to 30 minutes.

     
It is also possible to draw a sequence of connected lines using primitives. For example:
draw_primitive_begin(pr_linestrip);
draw_vertex(10,10);
draw_vertex(10,90);
draw_vertex(90,120);
draw_vertex(120,180);
draw_vertex(200,200);
draw_primitive_end();

will draw a shape on screen.

A primitive is a collection of points that are connected to each other by lines. This has the advantage over the basic draw_line function as you only need to reference the next point to draw to (as opposed to stating the start and end point).

Useful Functions

Sometimes you may wish to set the alpha of something you are drawing. Setting an alpha changes its transparency (from 0 which is fully transparent to 1 which if fully opaque). This can apply to text, shapes, primitives and sprites, etc.

For example:
draw_set_alpha(0.5);
draw_set_colour(c_red);
draw_roundrect(20,20,880,200,false);
draw_set_alpha(1);//set back so other objects are not affected

Which would draw a red rectangle with rounded corners at 50% transparancy.

You also draw a rectangle with a gradient of two colors for example:
draw_roundrect_colour_ext(50,50,300,200, 5, 5, c_red, c_green, false);
You can also draw text with a choice of four colors, and alpha, for example:
draw_text_colour(200,200,"Some Example Text",c_red,c_green,c_yellow,c_blue,1);

Summary

You should now be comfortable in setting colors, drawing various geometric shapes, and have better understanding of room co-ordinates.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.111.107