« Back to tutorial listing

— 2D Shoot 'Em Up Tutorial —
Part 7: Shooting enemies

Introduction

Note: this tutorial builds upon the ones that came before it. If you aren't familiar with the previous tutorials in this series you should read those first.

We now have collision detection and can shoot the enemies to destroy them! Extract the archive, run cmake CMakeLists.txt, followed by make to build. Once compiling is finished type ./shooter07 to run the code.

A 1280 x 720 window will open, with a near-black background. A spaceship sprite will also be shown, as in the screenshot above. The ship can now be moved using the arrow keys. Up, down, left, and right will move the ship in the respective directions. You can also fire by holding down the left control key. Enemies (basically red versions of the player's ship) will spawn from the right and move to the left. Shoot enemies to destroy them. Close the window by clicking on the window's close button.

Inspecting the code

To start with we're going to update defs.h to add a side declaration. This will come into effect when firing bullets.


#define SIDE_PLAYER 0
#define SIDE_ALIEN  1

In order to test whether or not a bullet has hit an enemy, we need to do some collision detection. We've added a new compilation unit called util.c, which contains a single function - collision:


int collision(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2)
{
	return (MAX(x1, x2) < MIN(x1 + w1, x2 + w2)) && (MAX(y1, y2) < MIN(y1 + h1, y2 + h2));
}

This function takes 8 arguments, essentially two rectangles. It tests to see if the rectangles overlap and if so, returns 1. This is a very common test, so we won't go into details as to how it works (there are many, many such collision detection routines to be found around the web).

Next, we'll take a look at some other minor changes, starting with structs.h. In the Entity struct, we've added a new variable called side. This will use one of the SIDE_ declarations from defs.h.


int side;

We see this side variable in action in stage.c, where we've added it to the initPlayer, spawnEnemies, and fireBullet functions (some context of each omitted):


static void initPlayer(void)
{
	...
	player->side = SIDE_PLAYER;


static void spawnEnemies(void)
{
	...
	enemy->side = SIDE_ALIEN;


static void fireBullet(void)
{
	...
	bullet->side = SIDE_PLAYER;

Next, we want to update doFighters, to tell the function to delete a fighter if its health is 0. This is a simple change:


prev = &stage.fighterHead;

if (e != player && (e->x < -e->w || e->health == 0))
{
	if (e == stage.fighterTail)
	{
		stage.fighterTail = prev;
	}

	prev->next = e->next;
	free(e);
	e = prev;
}

We then add a new test to doBullets. As well as deleting a bullet if it leaves the right-hand side of the screen, we'll call a new function called bulletHitFighter, passing over the bullet. This function will be used to, as the name suggests, test to see if the bullet has hit made contact with a fighter.


if (bulletHitFighter(b) || b->x > SCREEN_WIDTH)
{
	if (b == stage.bulletTail)
	{
		stage.bulletTail = prev;
	}

	prev->next = b->next;
	free(b);
	b = prev;
}

Let's take a look at the bulletHitFighter function:


static int bulletHitFighter(Entity *b)
{
	Entity *e;

	for (e = stage.fighterHead.next ; e != NULL ; e = e->next)
	{
		if (e->side != b->side && collision(b->x, b->y, b->w, b->h, e->x, e->y, e->w, e->h))
		{
			b->health = 0;
			e->health = 0;

			return 1;
		}
	}

	return 0;
}

What this code does is step through all the fighters in our stage linked list and calls our collision function. We pass over the bullet's x, y, w, and h, as well as those of the fighter's, to the function. These represent the two objects' rectangles and will be used to check for an overlap. If the two rectangles do overlap, we set both the bullet's and fighter's health to 0, thus eliminating them from the game. One very, very important additional check is to see who the bullet belongs to. If we don't do this then the bullet will kill the issuing fighter as soon as it is fired. In effect, the player will press fire to die! This is why we added the side variable to Entity, to prevent such a thing happening. To sum up, if we see that the bullet is on the same side as the fighter that issued it, we'll ignore it (there will be further consequences for this in the next tutorial).

Finally, we need to ensure that the enemy fighters have health. If we don't do this they'll be removed the instant they are spawned.


static void spawnEnemies(void)
{
	...
	enemy->health = 1;

We can now shoot the enemies and defeat them! Great. In the next tutorial we'll add some challenge to the game have the enemies shoot back at (and destroy!) the player.

Exercises

  • Allow the enemies to be shot more than once before they are destroyed.
  • Destroy the enemies if they come into contact with the player.

Purchase

The source code for all parts of this tutorial (including assets) is available for purchase:

From itch.io

It is also available as part of the SDL2 tutorial bundle:

Desktop site