Roguelike game in C++ - Bootstrap
Posted on July 12, 2012 by Paul
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:
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:
You can compile the above code with:
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:
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:
Currently the the game loop function contains code for moving @ on the screen:
The erase function will simply put a symbol at position (row, col):
Let’s play a bit with this game.
Initial screen:
Game starts and the main character, @, is shown on the screen:
Game state after some movements:
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:
- Roguelike game in C++ - Introduction
- Roguelike game in C++ - Bootstrap
- Roguelike game in C++ - Adding a map to the game
- Roguelike game in C++ - Map generation with Perlin noise
- Roguelike game in C++ - Adding a rudimentary monster to the game
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: