PC Games

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

Android Games

Number Blocks
Match 3 Warriors


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
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 »


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)


A North-South Divide

For over a hundred years, messenger Duncan has wandered the world, searching for the missing pieces of an amulet that will rid him of his curse; a curse that has burdened him with an extreme intolerance of the cold, an unnaturally long life, and the despair of watching all he knew and loved become lost to the ravages of time. But now, Duncan is close to the end of his long quest.

Click here to learn more and read an extract!

« Back to tutorial listing

— Creating a Run and Gun game —
Part 6: Gameplay tweaks

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


In this part, we're going to focus on fixing some of the issues that we identified recently: bullets passing through map tiles, and enemies being able to see the player anywhere they are.

Extract the archive, run cmake CMakeLists.txt, followed by make, and then use ./gunner06 to run the code. You will see a window open like the one above. The main player character starts off in the bottom left-hand corner. The same controls from past tutorials apply. The player can now move around the map, jump on the various map blocks, and battle the enemies. You will notice that you can no longer shoot through the map and that enemies won't fire on you without a clear line of sight. Once you're finished, close the window to exit.

Inspecting the code

This isn't a long part, since we're mainly dealing with a couple of issues. To start with, let's look at fixing the issue with the bullets passing through the game world (the map). We've made some tweaks to checkCollisions in bullets.c:

static void checkCollisions(Bullet *b)

	if (b->life != 0)

checkCollisions used to only check to see if our bullet had hit an entity. Now, we're checking first if it has made contact with the world. We've extracted the previous entity check code into a new function called checkEntityCollisions, and introduced another function called checkWorldCollisions. We're first testing if the bullet has hit the world. If it hasn't (its `life` is greater than 0), we're then testing the collisions with entities as before.

checkWorldCollisions is a very simple function:

static void checkWorldCollisions(Bullet *b)
	int mx, my;

	mx = b->hitbox.x + (b->hitbox.w / 2);

	my = b->hitbox.y + (b->hitbox.h / 2);

	if (!isInsideMap(mx, my) || stage.map[mx][my] != 0)
		b->life = 0;

The idea is that we're going to check to see if the bullet has entered a solid map tile and, if so, kill it. We're first taking the bullet's hitbox `x` value and adding half of its `hitbox` `w` (width), and then assigning it to a variable called `mx`. We're then dividing the result by MAP_TILE_SIZE, to find out the tile index on the horizontal axis. We're doing the same using the `hitbox`'s `y` and `h` values, to find the vertical position, and assigning it to a variable called `my`. With the position within the map found, we're calling isInsideMap to find out if the bullet is inside the map bounds, and also checking if the tile within the map at `mx` and `my` is a non-zero value. If the bullet isn't inside the map or has hit a non-zero tile (non-solid), we're setting the bullet's `life` to 0.

With our bullets no longer able to pass through solid map tiles, we can now look at blocking the enemies' line of sight to the player. We've introduced a new file to handle this, so it can be done centrally. ai.c will contain any functions that relate to common ememy behaviour. There are just two function here - canSeePlayer and traceMap. canSeePlayer is just one line right now:

int canSeePlayer(int x, int y)
	return traceMap(x, y);

The function takes two parameters (`x` and `y`) and simply returns the result of traceMap (passing through the `x` and `y` values it received). There is actually a little more to traceMap:

static int traceMap(int x, int y)
	int x1, y1, x2, y2, dx, dy, sx, sy, err, e2;

	x1 = x / MAP_TILE_SIZE;
	y1 = y / MAP_TILE_SIZE;
	x2 = (stage.player->hitbox.x + (stage.player->hitbox.w / 2)) / MAP_TILE_SIZE;
	y2 = (stage.player->hitbox.y + (stage.player->hitbox.h / 2)) / MAP_TILE_SIZE;

	dx = abs(x2 - x1);
	dy = abs(y2 - y1);

	sx = (x1 < x2) ? 1 : -1;
	sy = (y1 < y2) ? 1 : -1;
	err = dx - dy;

	while (1)
		e2 = 2 * err;

		if (e2 > -dy)
			err -= dy;
			x1 += sx;

		if (e2 < dx)
			err += dx;
			y1 += sy;

		if (x1 == x2 && y1 == y2)
			return 1;

		if (!isInsideMap(x1, y1) || stage.map[x1][y1] != 0)
			return 0;

	return 0;

traceMap employs a line of sight (LOS) check, using Bresenham's line algorithm. We won't go into how this algorithm works, and only how we're using it for our LOS checks. First, we're setting up `x1` and `y2` (the line start point) as `x` and `y` divided by MAP_TILE_SIZE. The line end point is the middle of the player's `hitbox`, divided by MAP_TILE_SIZE. This LOS test will simply check the map tiles to see if the view is clear. In other words, it's a very coarse check. At the end of each line step, we're checking if we've left the map or if the map tile at `x1` and `y1` is a non-zero value (via isInsideMap and testing Stage's map data). If we're no longer inside the map or have hit a solid tile, we'll reuturn 0, to indicate that the trace failed. Should we get to the map tile in which the player resides (or, at least, the middle of their hitbox), we'll return 1.

Turning to greenSoldier.c, we've updated the lookForPlayer function to make it a little more sensible:

static void lookForPlayer(Entity *self)
	int distX, distY;
	EnemySoldier *s;

	distX = abs(self->x - stage.player->x);
	distY = abs(self->y - stage.player->y);

	if (distX <= SCREEN_WIDTH / 2 && distY <= self->texture->rect.h)
		if ((self->facing == FACING_LEFT && stage.player->x < self->x) || (self->facing == FACING_RIGHT && stage.player->x > self->x))
			if (canSeePlayer(self->x + (self->texture->rect.w / 2), self->y))
				s = (EnemySoldier*) self->data;

				s->shotsToFire = 2 + rand() % 3;

				s->thinkTime = FPS * 2;

Now, instead of allowing the enemy to see the player from anywhere (depending on the direction of facing), the enemy can only see the player if they are half a screen away on the horizontal, and their own texture height on the vertical (in other words, a small rectangular region). The test for the direction of facing comes next. If that passes, we then call the canSeePlayer function that we defined in ai.c. The enemy's line of sight starts from their `x` position, plus half their texture width, as well as their `y` position. In other words, from the top of their head. If that check passes, the enemy is allowed to fire.

Those are our fixes done, which means we have now improved our gameplay a lot. What we need now is a larger map to explore. In the next part, we'll increase the size of our map, and even add in a new enemy type.


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