There are also lots more drawing functions for formatting text, and for drawing sprites. This chapter will introduce you to them.
Scores and health
Player names
Game information
Pop-up text
Dialogue between characters
Menu options
Displaying the main player
Drawing lives as images
Displaying what weapons are currently available
Various in-game buttons
Instances the player can walk or jump on
Enemy instances
Game info such as health or timers
Ensure you only try to draw variables that have already been defined – failure to do so will cause an error and crash your game.
Create a new project in GameMaker, along with a new object obj_example.
To use drawing functions, they need to be placed within one of the Drawing Events.
Ideally, you should use drawing events for actual drawing code. Other code unrelated to drawing should be placed in an alternative event, for example, the Step Event.
Create a Draw Event for the object you just created.
This will draw the This is some example text at the given position.
This will draw the text I am 43 years old.
With different colors
Different fonts
To have vertical and horizontal alignment
Save the code you just wrote and close the object.
Formatting will remain in place for all future drawing. It is good practice to set it each time just before drawing.
The text on the previous page will be drawn centered on the horizontal and vertical, in purple and using the set font.
You can force a new line using
Another important element of GameMaker is sprite control. You’re likely to use sprites a lot in your game. The aim of this section is to draw a sprite in a few different ways. The default way of drawing the sprite with default settings would be to assign it to an object and have nothing in the Draw Event. You can of course use a Draw Event and draw the sprite manually.
This will draw the sprite spr_test at position 200,200. The 0 tells the program which frame of the image to draw.
Color blending, for example, can be used to visually show damage to a player or enemy. If you want to draw a sprite with the default settings, this will be done automatically.
Sprites can also consist of multiple subimages (frames) that can be played in sequence to create an animation.
GameMaker starts the subimage frame at position 0, so your first frame (if you have subimages) will be 0.
Assign this sprite to an object, obj_example.
Delete any Draw Event if you have one present (right click and select delete).
If you are using a drawing event, you will be required to tell the program to draw the sprite. You can use draw_self() or draw_sprite_ext.( )
Make this change now.
Another variable that you’ll find useful is image_xscale and image_yscale. This can be used to great effect in making a sprite flip when the direction changes.
If you now test your game, you can move the character left and right and the sprite will face the correct direction.
which would set the 3rd subimage and turn off animation.
Basic Projects
- A)
Draw a sprite that rotates.
- B)
Make a program that draws players lives as heart images. Allow the player to press arrow keys to change lives between 1 and 6.
Advance Projects
- C)
Draw clouds that move across the screen at different speeds and with different alpha values.
- D)
Create an instance that follows a path and points in the direction it is moving.
Useful Functions
Summary
You can now do some more advanced text handling and know the basics of sprite control. You understand that animations are made of subimages that are played in sequence. You should now know how to use scaling to change which direction a sprite points in. You now are aware of how to set a subimage and change the animation speed.