Roguelike game in C++ - Bootstrap
Posted on July 12, 2012 by Sol

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

I suppose you’ve skimmed through the Introduction to this series of blog posts about implementing a toy roguelike game in C++. That being said, let’s get started!

If you want to follow along with the development, you will need a C++ compiler and the ncurses library. I’m going to use the GNU C++ compiler (specifically g++-4.7.1) for testing and compiling my code. In principle any C++ compiler available on your system should work, just be sure that you can work with the last version of ncurses. For Mac users, installing Xcode and the Command line tools will also install ncurses. On Linux you could use your package manager for installing g++ and ncurses, the procedure is different from distribution to distribution, if you use a Debian derived distro, like Ubuntu, all you have to do is to write in a Terminal:

1
2
sudo apt-get install g++
sudo apt-get install libncurses5-dev

For Windows users, the situation is a bit more complicated. First, you will need to install the last stable version of the GNU C++ compiler from www.equation.com. After that you will need to find a binary version of ncurses. For your convenience I’ve included a 64 bits version of ncurses on the Gihub repository of this blog series. You will need to copy the content of the include folder in the include path of your compiler, same goes for the lib folder. If you already know what Cygwin or Mingw is, you probably don’t need my help to install g++ and ncurses.

Let’s check if we can compile a simple code with ncurses:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#include <ncurses.h>

int main() {
	// Initialize ncurses
	initscr();
	clear();

	// Print a string on screen
	printw("Seems that you can use ncurses ...\nPress any key to exit!");

	// Wait until the user press a key
	getch();

	// Clear ncurses data structures
	endwin();
}

You can compile the above code with:

1
g++ test.cpp -lncurses -o test

If you have no error, congratulations, you can follow along with this post.

@ is usually the symbol used for representing the main character in a roguelike game. We could start our game with an welcome message and present the user with the option to start or quit the game. Once the game starts we will print the main character on the screen, the user will be able to quit at any point by pressing q or Q:

 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
...	
	// Initialize ncurses
	initscr();
	clear();
	noecho();
	cbreak();
	keypad(stdscr, TRUE);
	curs_set(0);	

	// Define the main character initial position and symbol:
	int row = 10, col = 10;
	char main_char = '@';

	// Print a welcome message on screen
	printw("Welcome to the RR game.\nPress any key to start.\nIf you want to quit press \"q\" or \"Q\"");

	// Wait until the user press a key
	int ch = getch();

	// Clear the screen
	clear();
	
	// Start the game loop
	while(1) {
		if(ch == 'q' || ch == 'Q') {
			break;
		}
		// If the user choses to stay, show the main character at position (row,col)
		else {
			mvaddch(row, col, main_char);
			ch = getch();
		}
	}
...

A game with no movement is pretty lame if you ask me, so in the next step we are going to let the user to move the character on screen using the arrow keys. Initially I was thinking to use for movement the H,J,K,L keys à la Vim, but I think the arrow keys are more intuitive. If I will ever change my mind I could add this as an option in the game settings.

The screen is an array of rows and columns for ncurses, so if we want to move the @ we will need to change the (row, col) position. For e.g. if you want to move left you will subtract 1 from col. If you want to go right you will add 1 to col and so on … An important observation here, when we move @ from one position to another we will need to clean the old location. We could use an empty C++ character for this if we want to have a clean screen or another symbol if we want to see a trace for the movements of @. For now, I’m going to erase the old position of @ with the # symbol.

We can clean up a bit the main function by moving the ncurses initialization in a separate function, same goes for the game loop:

 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
int main() {
	// Define main character symbol and initial position
	int row = 10, col = 10;
	char main_char = '@';

	// Start ncurses
	init();

	// Print a welcome message on screen
	printw("Welcome to the RR game.\nPress any key to start.\nIf you want to quit press \"q\" or \"Q\"");

	// Wait until the user press a key
	int ch = getch();

	// Clear the screen
	clear();	

	// Start the game loop
	game_loop(main_char, row, col, ch);

	// Clear ncurses data structures
	endwin();

	return 0;
}

Currently the the game loop function contains code for moving @ on the screen:

 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
40
void game_loop(char main_char, int row, int col, int ch) {
	// Check if the user wishes to play the game
	if(ch == 'q' || ch =='Q') return;

	// Show the main character on the screen
	mvaddch(row, col, main_char);
	refresh();

	for(;;) {
		ch = getch();

		if(ch == KEY_LEFT) {
			erase(row, col);
			col = col - 1;
			mvaddch(row, col, main_char);
			refresh();
		}
		else if(ch == KEY_RIGHT) {
			erase(row, col);
			col = col + 1;
			mvaddch(row, col, main_char);
			refresh();
		}
		else if(ch == KEY_UP) {
			erase(row, col);
			row = row - 1;
			mvaddch(row, col, main_char);
			refresh();
		}
		else if(ch == KEY_DOWN) {
			erase(row, col);
			row = row + 1;
			mvaddch(row, col, main_char);
			refresh();
		}
		else if(ch == 'q' || ch == 'Q') {
			break;
		}
	}
}

The erase function will simply put a symbol at position (row, col):

1
2
3
void erase (int y, int x) {
	mvaddch(y, x, '#');
}

Let’s play a bit with this game.

Initial screen:

Figure_1

Game starts and the main character, @, is shown on the screen:

Figure_2

Game state after some movements:

Figure_3

Not bad for a few lines of code. Next time we are going to create a map for the game and put the @ on this map. We’ll also add functionality for seeing different parts of the map, since in general the map is larger than the screen.

If you have any improvements in mind feel free to drop me a comment.

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