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)



Arriving on the back of a meteorite, an alien pathogen has spread rapidly around the world, infecting all living humans and animals, and killing off all insect life. Only a handful are immune, and these survivors cling desperately to life, searching for food, fresh water, and a means of escape, find rescue, and discover a way to rebuild.

Click here to learn more and read an extract!

« Back to tutorial listing

— Creating a simple roguelike —
Part 5: Monsters attacking

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


Up until now, the monsters have been static, not moving or attacking. We're going to change this, and have the monsters move towards us and also attack. This is going to be a short part, to demonstrate how the enemies can attack us.

Extract the archive, run cmake CMakeLists.txt, followed by make, and then use ./rogue05 to run the code. You will see a window open like the one above, showing our main character in a dungeon environment. Use the same controls as before to move around. Battle the Micro Mice when you find them. There are three in total. They will hit you back and can harm you, but cannot kill you. Once you're finished, close the window to exit.

Inspecting the code

We're going to start by updating monsters.c. doMonsters is up first:

void doMonsters(void)
	int processing;

	processing = 1;



		processing = dungeon.animationTimer == 0 && dungeon.currentEntity != dungeon.player;
	while (processing);

Before calling nextMonster, we're making a call to a new function named moveToPlayer. We'll see this fully in a moment. For now, we'll cover off a tweak to nextMonster:

void nextMonster(void)
	int found;
	Entity *e;

	e = dungeon.currentEntity;

		e = e->next;

		if (e == NULL)
			e = dungeon.entityHead.next;

		found = !e->dead && (e->type == ET_MONSTER || e->type == ET_PLAYER);
	while (!found);

	dungeon.currentEntity = e;

Where before our `found` variable was being set to 1 (true) when finding an entity type of ET_MONSTER or ET_PLAYER, we're now also requiring that the entity's `dead` flag is set to 0 (false). This is to handle a race condition where, following the player's turn, the enemies go next (due to the nextMonster call). It is likely that the player might have just killed a monster, but owing to our processing the dead monster is able to act and strike us. By checking if the monster is not dead first, we eliminate this bug.

Now we come to moveToPlayer:

static void moveToPlayer(void)
	int dx, dy;
	Entity *e;

	dx = dungeon.player->x - dungeon.currentEntity->x;
	dy = dungeon.player->y - dungeon.currentEntity->y;

	dx = MIN(MAX(dx, -1), 1);
	dy = MIN(MAX(dy, -1), 1);

	e = getEntityAt(dungeon.currentEntity->x + dx, dungeon.currentEntity->y + dy);

	if (e == NULL || e == dungeon.player)
		moveEntity(dungeon.currentEntity, dx, dy);

The idea behind this function is that the enemy will simply attempt to move towards the player, one square at a time. We start by subtracting dungeon's currentEntity's `x` and `y` (the Monster) from the player's `x` and `y`, assigning the results to `dx` and `dy`. This will give us the direction the monster needs to move to reach the player. Because our monsters can only one square at a time, we need to limit the values of `dx` and `dy` to between -1 and 1. We do this by calling our MIN and MAX macros for `dx` and `dy`, passing in -1 to MAX and -1 to MIN.

Finally, we check to see if the square the monster wants to move into is empty or is occupied by the player. We do this by calling getEntityAt, passing over the currentEntity's `x` and `y`, plus the calculated `dx` and `dy` values, assigning the result to an Entity variable named `e`. We then test the value of `e` to see that it's either NULL or is the player. If so, we'll call moveEntity, passing over currentEntity and our calculated `dx` and `dy`. Remember that according to moveEntity this will result in the monster attacking the player, should the player be in the square the monster is attempting to move into.

Next, we've made a tweak to addMonsters:

void addMonsters(void)
	int i;

	for (i = 0 ; i < 3 ; i++)
		addEntityToDungeon(initEntity("Micro Mouse"));

We've just added a for-loop here, to create 3 Micro Mouse monsters, instead of just 1. They'll be added into the dungeon at random places.

We've also tweaked the Micro Mouse itself:

void initMicroMouse(Entity *e)
	Monster *m;

	m = createMonster(e);
	m->hp = m->maxHP = 1 + rand() % 4;
	m->defence = 1;
	m->minAttack = 1;
	m->maxAttack = 3;

	STRCPY(e->name, "Micro Mouse");
	e->texture = getAtlasImage("gfx/entities/microMouse.png", 1);

We've increased its maxAttack from 2 to 3. This is because it would otherwise always fail to cause damage to the player, being too weak to harm them. These sorts of tweaks will likely continue to happen during our development and game testing, especially when weapons and armour are added.

Finally, we've made an update to player.c, in doPlayer:

void doPlayer(void)
	int dx, dy;

	moveDelay = MAX(moveDelay - app.deltaTime, 0);

	if (moveDelay == 0)
		dx = dy = 0;

		if (app.mouse.buttons[SDL_BUTTON_LEFT])
			dx = dungeon.selectedTile.x - dungeon.player->x;
			dy = dungeon.selectedTile.y - dungeon.player->y;

			dx = MIN(MAX(dx, -1), 1);
			dy = MIN(MAX(dy, -1), 1);

		// snipped

Since we now have mouse support, we've added in the ability for the player to move the character around by highlighting a square and holding the left mouse button. It's not the best method of navigation, but does sometimes help with fighting monsters, especially when attacking along the diagonals.

We first test to see if App's mouse's left button is pressed, then calculate the distance between dungeon's selectedTile and the player, assigning the results to `dx` and `dy`. As with the monsters, we're limiting the values of `dx` and `dy` to between -1 and 1. The rest of the function continues as normal.

That's all we needed to do to add in basic monster movement and attacking. While this works, it's very crude and there are many flaws. To begin with, the monsters are always moving towards the player, regardless of how far away they are and whether they are in the line of sight. The other issue is that the movement is so basic that the monsters can get stuck behind walls, etc. The way to solve this is to use a better navigation method, such as A*.

In our next part, we'll introduce A* pathfinding to replace the movement we've demo'd here.


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