C++: A Simple Role-Playing Game
A Simple Role-Playing Game
In this video, we make use of several classes to present a simple example of object-oriented programming via a role-playing game. The game board is a 10 x 10 maze with 10 monsters and the player character, which we refer to as the hero.
To make the programming easier, we keep the monsters fixed in place and only allow the hero to move. The player moves the hero by using the keys 'a', 'w', 's', and 'z' to move left, up, right, and down, respectively. To attack a monster, the hero just moves into it.
For this lesson, we have split our code into three classes: CCreature, CDungeon, and CRolePlayingGame. The class CCreature is used to instantiate both the hero and the monsters. It also handles the combat. The class CDungeon is a simple class which holds the maze. There is only one member function in this class and it simply returns a char to indicate whether the location is filled by a wall or not. The class CRolePlayingGame handles the rest, including movement, display, and end of game conditions.
The last part of our program consists of our main function with the game loop. This loop gets a move, attempts to move the hero, checks for end of game conditions, and removes any dead monsters. Invalid moves are ignored. The end of game conditions consist of either the hero dying, or killing all of the monsters.