Roguelike game in C++ - Adding a rudimentary monster to the game
Posted on August 1, 2012 by Sol

The code for this post is on GitHub: https://github.com/sol-prog/roguelike.

Last time we’ve added a real map to the game, generated with a Perlin noise function. But where is the fun in a roguelike game without a monster ? In this post, we are going to add a rudimentary monster to our game, that will chase the main character. The player task will be to dodge the monster and stay alive …

Our monster will be dumb, all he knows is to chase the main character on the map. Because I’m more interested in testing an idea for a chasing algorithm, we are not going to create a separate class for the monster, we’ll refactor the code and add some AI to our monster in a future tutorial. For now we could use the Character class to initialize our monster:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
int main() {

	...

	// Initialize the main character. We are going to put this in the middle of 
	// the game map (for now)
	Character main_char('@', game_map.height()/2, game_map.width()/2);

	// Add a dummy monster to the game (we start with a hard coded monster position defined 
	// relatively to our main character position)
	Character monster('M', main_char.row() + 7, main_char.col() + 22);

	...

	// Start the game loop
	game_loop(game_map, viewport, main_char, ch, monster);

	return 0;
}

Now, that we have a monster in our game, we could add it to the map in the game_loop function:

1
2
3
4
5
6
7
8
9
void game_loop(Frame &game_map, Frame &viewport, Character &main_char, int ch, Character &monster) {
	// Check if the user wishes to play the game
	if(ch == 'q' || ch =='Q') return;

	// Add the monsters to the game_map
	game_map.add(monster);

	....
}

What about the chasing algorithm ? We could use a simple idea based on the Euclidean distance for moving the monster:

  • Check the up, down, left and right neighbor positions for our monster.
  • If a cell (neighbor) is free, calculate the distance between this cell and the main character position.
  • Move in the cell that minimize the distance between the monster and the main character.

We’ll start by creating a new member function in Frame, that will check if the target position is free to move in or not:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
// Check if the target position is free
bool Frame::target_position(int row_0, int col_0) {
	// Get the element at the target position:
	char target = mvwinch(_w, row_0, col_0);
	// If the target position is watter, wall or snow stop the character to go through it
	if (target == '~' || target == '#' || target == 'S') {
		return FALSE;
	}
	// If the target position is a monster don't let the character to go through it (for now)
	if (target == 'M') {
		return FALSE;
	}
	return TRUE;
}

For now, we could add the tracking (chasing) algorithm in the game_loop function just after the main character movements, this code will be moved to a Monster class in our next article from this series:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
	...
	// Other characters movements
	// Try to move left
	int pos = -1, score = game_map.height()*game_map.height() + game_map.width()*game_map.width(), dist = -1;
	if( game_map.target_position(monster.row(), monster.col() - 1)) {
		dist = std::pow(main_char.row() - monster.row(),2) + std::pow(main_char.col() - (monster.col() - 1),2);
		if(score > dist) {
			score = dist;
			pos = 0;
		}
	}
	// Try to move right
	if( game_map.target_position(monster.row(), monster.col() + 1)) {
		dist = std::pow(main_char.row() - monster.row(),2) + std::pow(main_char.col() - (monster.col() + 1),2);
		if(score > dist) {
			score = dist;
			pos = 1;
		}
	}
	// Try to move up
	...
	// Try to move down
	...

	switch(pos) {
		case 0:
			game_map.add(monster, monster.row(), monster.col() - 1);
			break;
		case 1:
			game_map.add(monster, monster.row(), monster.col() + 1);
			break;
		case 2:
			game_map.add(monster, monster.row() - 1, monster.col());
			break;
		case 3:
			game_map.add(monster, monster.row() + 1, monster.col());
			break;
	}
	...

Let’s see the new code in action. When the game starts, we have the main character, @, and the monster, M, in their initial positions:

Rogulike characters initial positions

A screenshot of the game after a few movements, note how the monster follows the player character:

Rogulike characters tracking the player

I’ve also made a small movie for showing how you can play the game:

In the next article, we are going to refactor the code for the monster, in order to be able to add more than one monster to the screen. We could also define areas on which a monster can see the main character. When you have ten monsters on your game, it won’t be much fun if every monster from the game can see the position of the player character and start chasing it. We could create a few kind of monsters with different characteristics. Also the main character should have a sword or some other weapon to be able to kill or stop a monster …

All posts from this series:

If you are interested in learning more about the new C++11 syntax I would recommend reading The C++ Programming Language by Bjarne Stroustrup.

or, Professional C++ by M. Gregoire, N. A. Solter, S. J. Kleper 2nd edition:

blog comments powered by Disqus