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


The Third Side (Battle for the Solar System, #2)

The White Knights have had their wings clipped. Shot down and stranded on a planet in independent space, the five pilots find themselves sitting directly in the path of the Pandoran war machine as it prepares to advance The Mission. But if they can somehow survive and find a way home, they might just discover something far more worrisome than that which destroyed an empire.

Click here to learn more and read an extract!

« Back to tutorial listing

— Mission-based 2D shoot 'em up —
Part 7: Secondary weapon: Rockets

Note: this tutorial assumes knowledge of C, as well as prior tutorials.

Introduction

Before we move onto implementing missions and objectives, we should take the opportunity to work on our secondary weapons. We will have 4 weapons and a shield system in the end, all which will have unique properites. We're going to start with a simple one: rockets.

Extract the archive, run cmake CMakeLists.txt, followed by make, and then use ./shooter3-07 to run the code. You will see a window open like the one above. Use the WASD control scheme to move the fighter around. Hold J to fire your main guns. Press I to fire your secondary weapon (rockets). Play the game as normal. Firing a rocket will consume ammo. Collect more ammo power-ups from defeated enemies to replenish your stock. Once you're finished, close the window to exit.

Inspecting the code

Adding in our rockets is very, very easy. Rockets are basically bullets, with high damage, and a different `tick` function to that which we already have.

We'll start with the update to defs.h:


enum
{
	SW_NONE,
	SW_ROCKET
};

We've created a new enum to represent our secondary weapon (SW being short for Secondary Weapon). SW_NONE will mean we don't have a secondary weapon, while SW_ROCKET will declare we have rockets.

Now on to structs.h:


typedef struct
{
	int catnip;
	struct
	{
		int health, maxHealth;
		int ammo;
		int damage;
		int reload;
		int output;
		int secondaryWeapon;
	} kite;
} Game;

We've updated Game's `kite`'s struct, adding in secondaryWeapon. This will be used to determine the type of secondary weapon we're using. Again, note that we're putting this into our Game struct, rather than assigning it to the player's fighter directly. This is so that the player can choose which weapon to use between missions.

Finally, we've updated the Stage struct:


typedef struct
{
	// snipped
	double      engineEffectTimer, rocketEffectTimer;
	int         numActiveEnemies;
	Entity     *player;
	PointF      camera;
} Stage;

We've added in rocketEffectTimer. Like engineEffectTimer, this will be used to control how often our rockets stream flame trails.

With that done, we can move over to bullets.c, where we'll implement our new rocket weapon. First of all, we're updating initBullets:


void initBullets(void)
{
	memset(&stage.bulletHead, 0, sizeof(Bullet));

	stage.bulletTail = &stage.bulletHead;

	standardBulletTextures[0] = getAtlasImage("gfx/bullets/greenPlasma.png", 1);
	standardBulletTextures[1] = getAtlasImage("gfx/bullets/redPlasma.png", 1);

	rocketTexture = getAtlasImage("gfx/bullets/rocket.png", 1);
}

The only new thing we're doing here is grabbing a texture for our rocket sprite, and assigning it to rocketTexture.

Next up, we've added a new function called fireRocket:


void fireRocket(Entity *owner, int facing)
{
	Bullet *b;

	b = spawnBullet(owner);
	b->y = owner->y + (owner->texture->rect.h / 2);
	b->texture = rocketTexture;
	b->facing = facing;
	b->damage = 50;
	b->health = FPS * 2;

	if (facing == FACING_LEFT)
	{
		b->x = owner->x;
		b->dx = -ROCKET_SPEED;
	}
	else
	{
		b->x = owner->x + owner->texture->rect.w;
		b->dx = ROCKET_SPEED;
	}

	b->tick = rocketTick;
	b->draw = standardDraw;
}

A very simple function. We're basically creating a bullet, and giving it the rocketTexture texture. We're setting its `damage` to 50 and making it live for 2 seconds (via its `health`). We're also testing to see which direction the owner of the rocket is facing (in this case, the player), and setting the rocket's starting `x` position and `dx` (it's speed) accordingly; we want to make sure the rocket goes in the right direction and originates from the front of the firing craft. Finally, we're setting the bullet's `tick` to a new function called rocketTick, and its `draw` function to standardDraw.

So far so good. Now to look at the rocketTick function:


static void rocketTick(Bullet *b)
{
	if (stage.rocketEffectTimer <= 0)
	{
		addRocketEngineEffect(b->x + (b->facing == FACING_LEFT ? b->texture->rect.w : 0), b->y + b->texture->rect.h - 10);
	}

	checkCollisions(b);
}

You might remember that the player's bullets are killed off if they leave the screen. Well, we're not doing that test in this function; our rockets can fly until they expire naturally. So, this function merely tests if Stage's rocketEffectTimer is 0 or less, and then creates an engine effect at the rear of the rocket (by calling addRocketEngineEffect, found in effects.c). We're also calling checkCollisions, to see if the rocket has struck an enemy.

That's it for bullets.c! Very simple. Now we can look at the other compilation units that we've updated.

If we look at game.c, we can see we've updated initGame:


void initGame(void)
{
	memset(&game, 0, sizeof(Game));

	game.kite.health = game.kite.maxHealth = 10;
	game.kite.reload = MIN_KITE_RELOAD;
	game.kite.output = 1;
	game.kite.damage = 1;
	game.kite.ammo = MAX_KITE_AMMO;
	game.kite.secondaryWeapon = SW_ROCKET;
}

We're setting Game's kit's secondaryWeapon to SW_ROCKET, to tell our game we have rockets as our secondary weapon. This is just for testing purposes. In our full game, the player won't have a starting secondary weapon, and will need to buy one.

Now onto player.c, where we've made one update and one addition, starting with doPlayerControls:


static void doPlayerControls(Entity *self, Fighter *f)
{
	// snipped

	if (isControl(CONTROL_SECONDARY))
	{
		clearControl(CONTROL_SECONDARY);

		fireSecondary(self);
	}

	f->dx = MIN(MAX(f->dx, -MAX_FIGHTER_SPEED), MAX_FIGHTER_SPEED);
	f->dy = MIN(MAX(f->dy, -MAX_FIGHTER_SPEED), MAX_FIGHTER_SPEED);
}

We're testing if we've pressed the control for firing our secondary weapon (isControl is found in controls.c). If so, we're going clear the control, and then call a new function named fireSecondary. Unlike when we fire our main guns, we want the player to press the button, rather than hold it down. Our secondary weapons don't have a reload or firing detaly, so this will ensure we don't use up all our ammo in one go, which would be annoying!

The fireSecondary function is defined so:


static void fireSecondary(Entity *self)
{
	if (game.kite.ammo > 0 && game.kite.secondaryWeapon != SW_NONE)
	{
		switch (game.kite.secondaryWeapon)
		{
			case SW_ROCKET:
				fireRocket(self, self->facing);
				break;

			default:
				break;
		}

		game.kite.ammo--;
	}
}

Another simple function. We're first testing if we have any `ammo` (and also a valid secondary weapon), and then testing which kind of weapon we have. We only have SW_ROCKET right now, so we're calling the fireRocket function. With that done, we're decrementing our `ammo`, since we've just used some.

Finally, let's look at stage.c, where we've updated doStage:


static void doStage(void)
{
	stage.numActiveEnemies = 0;

	stage.engineEffectTimer -= app.deltaTime;
	stage.rocketEffectTimer -= app.deltaTime;

	// snipped

	if (stage.engineEffectTimer <= 0)
	{
		stage.engineEffectTimer = 1;
	}

	if (stage.rocketEffectTimer <= 0)
	{
		stage.rocketEffectTimer = 0.15;
	}
}

Just like our engineEffectTimer, we're decreasing rocketEffectTimer, and setting it to 0.15 if it falls to 0 or less.

And that's it! We can now fire some powerful rockets as a secondary weapon, so long as we have the ammo to do so. If not, we can just shoot down some enemies, and hope they release the powerups we need (or, later, buy ammo from the Mouse Bros. shop - but that's for another time).

Rockets are a good weapon, and do a lot of damage. What would be better is if we could launch one that will hunt down our enemies, letting us "fire and forget". So, in the next part we'll look into adding in homing missiles!

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