Intro
There have been many different ways of showing the player's surroundings in games gone by, such as the first-person-view common in many shoot-em-up games, the side view that is common in platformers and the top-down view that was used in many of the original graphical RPGs. However, all of these utilise graphics, something that we do not have, and so we must use a different way of description, besides pictures. As you have seen already, that is obviously going to be text.
Now I presume that you have read a story book. The more descriptive the book the better, as that is where our method of 'movement' will take its roots from. Words can have great power, and luckily for us, they also make it considerably easier to develop a game, for several reasons:
Enough of that though, let's get programming.
The Code
Alright, now you know why we are making it text based, let's get on to actually creating a system. If you have ever read a 'Text-adventure book' (The ones that say "if you chose a, turn to page 15 etc.") then you will be very familiar with the system we shall be using. In fact, you will already know it inside out, as that is almost exactly how ours will work.
This is how it will go:
So first, we will need to create a variable to store the information about where our player is. This can then be used to output the correct information about the surroundings.
Declare this global variable:
int playerLocation = 0; // Stores a numerical reference to the location of the playerGoing back to newGameFunc() we can start to make the game playable.
First let's tell the program to put the player at location 1 whenever a new game starts.
Inside newGameFunc(), after the last piece of information about the user is processed, add this line:
playerLocation = 1; // Place the player in the start areaFrom now on each numerical location stored in playerLocation will be called an area.
Now we've moved the player to the correct location we must describe the area to the player. This will be done using simple if statements to check where the player is. It may but be the best way, but it works well and is simple.
To implement this, add this piece of code inside our while loop in main().
if(playerLocation == 1) { */ Code for when the player is in area 1 */}We will flesh this out in a minute.
For every area we will have another one of these if statements, and in each one will be the respective code for it, including statements, actions and consequences of those actions, such as moving to a new area.
My first area will be in the middle of a forest, but you should obviously adapt it to fit your setting.
if(playerLocation == 1) { cout << "You are standing in the middle of a forest. A path veers off to the East and to the West\n"; cout << " 1: Go East\n 2: Go West\n"; cin >> userInput;}Here I have also made the program request the player's reply, and we shall process it soon.
A Note Before We Proceed
This is where the code can get confusing if you process too much in one area. This does not mean that you should only have limited things happening, but that you should try to rigidly adhere to only keeping one area to one area. While this may sound stupid, it is actually very easy to mix things up and combine two areas into one. This becomes very awkward and confusing later on down the line.
With that out of the way, let's continue.
The Code - Continued
Now let's process that all-important user input.
When the player enters '1', we want the player to move east, when they enter '2', we want them to move west. Let's incorporate that, by simply checking the input then changing playerLocation accordingly.
if(playerLocation == 1) { cout << "You are standing in the middle of a forest. A path veers off to the East and to the West\n"; cout << " 1: Go East\n 2: Go West\n"; cin >> userInput; if(userInput == 1) playerLocation = 2; // 'East' pressed else if(userInput == 2) playerLocation = 3; // 'West' pressed}It's that simple!
Using this template you can easily create multiple, joined areas. Why don't you have a go before you move on to the next section. I'm sure you want more than one location in your game!
|
