My programming ramblings

Roguelike game in C++ - Adding a map to the game

Posted on July 16, 2012 by Paul

The last post of this series has laid the ground for a small roguelike game - the main character @ was added on the screen and the user was able to change his position using the arrow keys. Now it is time to add more interactivity to our game by creating a test map on which the character is allowed to move freely.

We’ll start by refactoring a bit the code for the screen initialization part implemented last time, we could put all this code in a Screen class, we show here only the definition of the Screen class:

A game character is currently defined as having an ASCII symbol and a position, in the future we could add more properties for a character like color, speed etc … Having all this in a class, that can be extended in the future, sounds like a good idea:

A game map is usually bigger than the player’s screen and at a particular moment only a part of this map should be visible to the player for a bit of frill. We could use an ncurses WINDOW for storing the game’s map and a subwindow of this for what is actually shown on the screen, in other words we’ll need a window and a viewport.

We could create a Frame class to define our game map (a window) and the viewport (a subwindow). A Frame object could be initialize by specifying his width, height and his position, for a viewport we’ll also need to specify his parent window:

1 classFrame{ 2 // Frame dimensions 3 int_height,_width; 4 // Frame position 5 int_row,_col; 6 // Boolean - FALSE for a window and TRUE for a subwindow (viewport) 7 bool_has_super; 8 // Pointer to an ncurses WINDOW 9 WINDOW*_w;10 // Pointer to an ncurses WINDOW, this will be NULL for a window and will point to the parent window11 // for a subwindow12 WINDOW*_super;13 public:14 ...15 // Initialize a main window (no parent)16 Frame(intnr_rows,intnr_cols,introw_0,intcol_0);17 // Initialize a subwindow (viewport) with a parent window18 Frame(Frame&sw,intnr_rows,intnr_cols,introw_0,intcol_0);19 ~Frame();20 ...21 // Fill a window with numbers - the window is split in four equal regions,22 // each region is filled with a single number, so 4 regions and 4 numbers.23 // This is a suggestion of how this will look:24 // 0 | 125 // -----26 // 2 | 327 // This function is used only for debugging purposes.28 voidfill_window();29 ...30 // Add a character to the window31 voidadd(Character&x);32 // Center the viewport around a character33 voidcenter(Character&x);34 };

The fill_window method from above is useful in the development phase of the game, we’ll need a dummy game map on which we could move our main character. The viewport will follow the main character movements with the aid of the center method from Frame.

Using the above elements we can define the main function or our game in a much cleaner way than before:

1 intmain(){ 2 3 // Initialize ncurses 4 Screenscr; 5 6 // Print a welcome message on screen 7 scr.add("Welcome to the RR game.\nPress any key to start.\nIf you want to quit press \"q\" or \"Q\""); 8 9 // Wait until the user press a key10 intch=getch();11 12 // Create an ncurses window to store the game map. This will be twice the size13 // of the screen and it will be positioned at (0,0) in screen coordinates14 Framegame_map(2*scr.height(),2*scr.width(),0,0);15 16 // Create an ncurses subwindow of the game map. This will have the size17 // of the user screen and it will be initially postioned at (0, 0)18 Frameviewport(game_map,scr.height(),scr.width(),0,0);19 20 // Initialize the main character. We are going to put this in the middle of21 // the game map (for now)22 Charactermain_char('@',game_map.height()/2,game_map.width()/2);23 24 // Fill the game map with numbers25 game_map.fill_window();26 27 // Start the game loop28 game_loop(game_map,viewport,main_char,ch);29 30 return0;31 }

Currently the game loop contains code for moving the character on the screen and centering the viewport:

Disclaimer:All data and information provided on this site is for informational purposes only. solarianprogrammer.com makes no representations as to accuracy, completeness, currentness, suitability, or validity of any information on this site and will not be liable for any errors, omissions, or delays in this information or any losses, injuries, or damages arising from its display or use. All information is provided on an as-is basis. solarianprogrammer.com does not collect any personal information about its visitors except that which they provide voluntarily when leaving comments. This information will never be disclosed to any third party for any purpose. Some of the links contained within this site have my referral id, which provides me with a small commission for each sale. Thank you for understanding.