Programming basic enemies
Enemies are more or less an extension of hazards; the basic idea is that if they touch (or shoot a projectile that touches) the player, the player is injured or killed. Only in games, though, not in real life. There is one difference here: enemies move. You can create enemies that will march to their deaths (think Goombas in the Mario Bros games), patrol a given area (think soldiers in the Metal Gear games), or even actively hunt the player (think ghosts in Pac-Man). For now, we're going to take a look at a simple enemy that will patrol a straight line.
Getting ready
As with everything we've done so far in this chapter, you'll need sprites to represent your enemy. You can make your own or you can use the one included in the downloaded files. It's acceptable to use the same sprite for different types of enemies but, from a game design perspective, it's a good idea to differentiate. This will make it easier for your player to identify the enemy types and react accordingly.
Create a sprite and name it spr_enemy_patrol
. Load the associated images from the files provided, or create your own, and be sure to set up the collision mask.
How to do it...
- Create an object called
obj_enemy_patrol
and assign the sprite you just made. - Add a Create event and drag and drop Set Variable that sets
image_speed
to0.6
. - Drag and drop Set Variable, which will set
dir
as-1
, and Transform Sprite, which will change thexscale
to-1
. Now, that's the simple part. The rest of the actions for (for the moment) will be handled by a Step event. - Add a Step event. This is where you'll need to run several tests in order to dictate your enemy's movement.
- Drag and drop Test Variable and have it check whether or not
dir
is equal to-1
. - Beneath this, drag a Start and End Block, within which you'll add a Test Variable that checks whether or not
x
is greater than16
. - Below this, you'll need a Start and End Block, between which you should place Moved Fixed, and set it to move
left
at a speed of1
. - Under all of the preceding boxes, drag and drop another Start and End Block.
- Within this block, place a Test Variable that checks whether
x
is less than or equal to16
, followed by another Start and End Block. - Within this block you will need a Set Variable that sets
dir
to1
, a Transform Sprite that changesxscale
to1
, and a Move Fixed that sets the move direction to theright
at a speed of1
.
You now have exactly half of the actions required for a patrolling enemy. At this point you can do one of two things: You could copy this entire set of actions, paste the copies directly below the existing actions, and go about changing the variables to their exact opposites, or you could drag and drop new blocks in the same order, but set the variables to their opposites. The former is much faster, but the latter will help you learn more about programming logic. In any respect, your Actions box should look like this:
How it works...
While this Step event contains a lot of actions, it is really doing something quite simple. GameMaker is checking, every game step, for the patrolling enemy's state and position. If headed left and his position on the room's x
coordinate is more than 16, he'll continue on in that direction. Once he gets past this point, GameMaker switches his direction, flips the sprite and sends him off to the other side of the room until his x
coordinate reaches the room width minus 16 pixels, rinse and repeat.
There's more...
This method of creating a patrolling enemy is a simple enough way to move your enemy in a set space. I chose to have him walk right across the entire room, but you could also have him walk within a smaller space. Another way to accomplish this is to create left and right boxes, remove their visibility by unchecking the Visible
box in Object Properties, and placing them at either end of the area you want patrolled. In the enemy's properties, instead of having everything in the Step event, you can control when they change direction by when they collide with the invisible boxes. The same actions would apply; they would just be executed in a different way. This just goes to show you that there are many possible solutions to all of the problems you may encounter when making games; you just have to find them.
See also
Paths will be discussed in Chapter 4, Let's Get Physical – Using GameMaker's Physics System.