PC Games

Orb
Lasagne Monsters
Three Guys Apocalypse
Water Closet
Blob Wars : Attrition
The Legend of Edgar
TBFTSS: The Pandoran War
Three Guys
Blob Wars : Blob and Conquer
Blob Wars : Metal Blob Solid
Project: Starfighter
TANX Squadron

Tutorials

2D shoot 'em up
2D top-down shooter
2D platform game
Sprite atlas tutorial
Working with TTF fonts
2D adventure game
Widget tutorial
2D shoot 'em up sequel
2D run and gun
Roguelike
Medals (Achievements)
2D turn-based strategy game
2D isometric game
2D map editor
2D mission-based shoot 'em up
2D Santa game
2D split screen game
SDL 1 tutorials (outdated)

Latest Updates

SDL2 Versus game tutorial
Wed, 20th March 2024

Download keys for SDL2 tutorials on itch.io
Sat, 16th March 2024

The Legend of Edgar 1.37
Mon, 1st January 2024

SDL2 Santa game tutorial 🎅
Thu, 23rd November 2023

SDL2 Shooter 3 tutorial
Wed, 15th February 2023

All Updates »

Tags

android (3)
battle-for-the-solar-system (10)
blob-wars (10)
brexit (1)
code (6)
edgar (9)
games (43)
lasagne-monsters (1)
making-of (5)
match3 (1)
numberblocksonline (1)
orb (2)
site (1)
tanx (4)
three-guys (3)
three-guys-apocalypse (3)
tutorials (17)
water-closet (4)

Books


Project Starfighter

In his fight back against the ruthless Wade-Ellen Asset Protection Corporation, pilot Chris Bainfield finds himself teaming up with the most unlikely of allies - a sentient starfighter known as Athena.

Click here to learn more and read an extract!

« 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:

Mobile site