As the last tutorial before we install a graphics library I thought a simple example that involves a game loop but no graphics would be worth while. Here’s a simple C++ tutorial for creating an adventure game where a prisoner escapes from a dungeon:
Setting up the game
To begin, create a new C++ project in your preferred development environment. Once you have created the project, create a new source file for your game. You can name it “main.cpp” or anything you like.
Defining game variables
Next, define the variables that will be used in the game. In this case, you will need variables to keep track of the player’s current location and the game’s state.
1 2 |
int location = 0; bool gameRunning = true; |
Creating the game loop
As we have learned before the game loop is the heart of your game. It will continuously run until the game is over. Inside the game loop, you will have to write code to handle the player’s input and update the game state accordingly.
1 2 3 4 |
while(gameRunning){ // get player input // update game state } |
Implementing the game logic
The game logic defines how the player can interact with the game world. In this case, the player will need to navigate through different rooms in the dungeon to find a way to escape. You will need to define a list of possible actions the player can take in each room and what happens when they take that action.
Here’s an example code snippet to get you started:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
while(gameRunning){ // display room description // get player input if (location == 0){ // player is in the cell if (input == "look"){ std::cout << "You are in a small, dank cell with a rusted iron door.\n"; } else if (input == "open door"){ std::cout << "The door is locked.\n"; } else if (input == "use key"){ std::cout << "You don't have a key.\n"; } else if (input == "escape"){ std::cout << "You can't escape from here without finding a way to open the door.\n"; } else{ std::cout << "I don't understand that command.\n"; } } // add more room descriptions and actions here } |
Adding game objects
To make the game more interesting, you can add objects that the player can interact with. For example, the player might find a key that can be used to unlock a door, or a weapon that can be used to fight enemies. You will need to define the objects and their properties, such as their name, description, and location.
1 2 3 4 5 6 7 |
struct Object{ std::string name; std::string description; int location; }; Object key = {"key", "A small iron key.", 2}; |
You can then modify the game logic to handle the player interacting with objects:
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 |
int location = 0; void movePlayer(std::string direction){ if (direction == "north" && location == 0){ location = 1; std::cout << "You have moved to the north.\n"; } else if (direction == "south" && location == 1){ location = 0; std::cout << "You have moved to the south.\n"; } else{ std::cout << "You cannot move in that direction.\n"; } } while(gameRunning){ std::string input; std::cout << "You are in a " << rooms[location].description << ".\n"; std::cout << "What would you like to do?\n"; std::cin >> input; if (input == "move"){ std::string direction; std::cout << "Which direction would you like to move?\n"; std::cin >> direction; movePlayer(direction); } else if (input == "quit"){ gameRunning = false; std::cout << "Goodbye!\n"; } else{ std::cout << "Invalid input.\n"; } } |
This code defines a movePlayer function that takes a direction parameter and updates the location variable based on the player’s input. The while loop displays the room description and prompts the player for input. If the player inputs “move”, the code prompts the player for a direction and calls the movePlayer function. If the player inputs “quit”, the game ends.
To make the game more challenging, you can add multiple levels with increasing difficulty. Each level can have different puzzles and obstacles that the player must overcome to progress to the next level. For example, in the first level, the player might need to find a way to escape the dungeon, while in the second level, the player might have to navigate through a maze to reach the exit.
You can define each level as a separate function, and call them from the game loop based on the player’s progress, like this, perhaps:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
void level1(){ // game logic for level 1 } void level2(){ // game logic for level 2 } while(gameRunning){ if (location == 0){ // player is in the cell level1(); // call level 1 function } else if (location == 1){ // player is in level 2 level2(); // call level 2 function } // add more levels here } |
Adding game over conditions
Finally, to make the game more engaging, you can add game over conditions, such as running out of health, or failing to complete a puzzle within a certain time limit. You can use these conditions to end the game and display a game over screen to the player.
Time for a real game now. To get started we will need to setup a Visual Studio project that uses SFML.
Hi John,
Enjoying the code very much, are you going to continue with this Coding a simple text adventure in C++ example. Been trying to get the rooms function workingno luck as yet. tried to design a struct rooms{ int location; string description; }; no luck. i’ll keep trying.
thanks
Michael