Text Based Adventure Game Using C++

Coding

Create c++ project using visual studio.

  • To create a C++ project in Visual Studio
  • From the main menu, choose File > New > Project to open the Create a New Project dialog box.
  • At the top of the dialogue, set Language to C++, set Platform to Windows, and set Project type to Console.

  • From the filtered list of project types, choose Console App then choose Next. In the next page, enter a name for the project(“TextBasedAdventure”), and specify the project location if desired.
  • Choose the Create button to create the project.

Add a new source file

If Solution Explorer isn’t displayed, on the View menu, click Solution Explorer.

Add a new source file to the project, as follows

  • In Solution Explorer, right-click the Source Files folder, point to Add, and then click New Item.
  • In the Code node, click C++ File (.cpp), type a name for the file(like Area.cpp, DeadlyArea.cpp..,), and then click Add.
  • The .cpp file appears in the Source Files folder in Solution Explorer, and the file is opened in the Visual Studio editor.
  • In the file in the editor, type a valid C++ program that uses the C++ Standard Library, code to build.
  • Save the file.
  • On the Build menu, click Build Solution .
  • The Output window displays information about the compilation progress, for example, the location of the build log and a message that indicates the build status.

Header Files

  • Create *.h file under Header Files Folder.
  • Add preprocessor directives in *.h File which needed in respective *.cpp file.
  • Need to declare members and functions.

Source Files

  • Create *.cpp (like Area.cpp) file under Source Files Folder.
  • Add preprocessor directive (Area.h)
  • Need to write definition for declared functions

Instructions

  • To run click shortcutKey F5. Then output command prompt will display like this:

In begining Your health value is 100. Area is printed and need to choose any path given

  • Press 1 and click enter. For every move 5 points will be reduced and next level will be printed.

Now choose any option(Path) and click enter.

Choose the path which u think as correct path for given Area untill reach the destination

you have option to go back. when you get severe damage , your health will be reduced by 40.

  • After reaching destination, there is option to play again.
  • click “Y” to continue and “N” to exit the application.

TextBasedAdventure.cpp

This class has Main() function. Creating GameEngine class object as GameEngine* gameEngine = new GameEngine(); .Class object is created using new Keyword. Methods should be called in main class.

 Code:

int main(int argc, char* argv[])

{

string input;

char choice;

GameEngine* gameEngine = new GameEngine();

gameEngine->Initialize();

do

{

gameEngine->Reset()

gameEngine->PrintTitle()

gameEngine->PlayGame();

choice = ‘x’;

do {

cout << “Do you want to play again ? (Y / N)” << endl;

getline(cin, input);

if (input.length() == 1)

{

choice = input[0];

if(choice == ‘y’ || choice == ‘Y’ || choice == ‘n’ || choice == ‘N’)

break;

cout << “!! Invalid Input !!” << endl;

} while (true);

} while (choice != ‘n’ && choice != ‘N’);

}

GameEngine.cpp

Initialize() method initialize Area value as NormalArea and DeadlyArea class. Adding path to area. Health variable is initialized as 100.

Code:

void GameEngine::Initialize()

{

homeArea = new NormalArea(“The sound of the rustling leaves is calling you to \

explore the densest of all the jungles. Don\’t get lost! \

The faint rays of sunlight filtering through the leaves \

pronounce vividly that, yes, exploring the unexplorable tracks \

of Amazon is possible. The stories from your forest adventure books \

are going to come alive. Yes, you are going to explore the place where Anacondas stay.”);

 

Area* area2 = new NormalArea(“You cannot think of exploring Amazon other than navigating \

through its river, its lifeline. The densest corners of the jungle can only be reached by boat.”);

 

Area* area3 = new NormalArea(“Entered into a dark cave with absolutely no light inside.”);

 

Area* area4 = new NormalArea(“The path is very slippery. Please choose a proper shoe to continue climbing.”);

 

Area* area5 = new NormalArea(“Somehow you managed to make a fire. It is very scary to see \

the skeletons of humans inside the cave. It is better to go back to the river.”);

 

Area* area6 = new NormalArea(“Your selection of shoe is very good for such terrain. “);

 

Area* area7 = new DeadlyArea(“You have slipped from the mid of the path and you got so many wounds.\

Your selection of shoe is not so helpful.”, 25);

 

Area* area8 = new NormalArea(“You have reached the top of the hill. The sunset scenery makes you feel peace. End of journey.”);

 

Area* area9 = new DeadlyArea(“You got so many wounds and fractures. You couldn`t make the journey and you have to call SOS! and wait until someone comes and rescue you.”, 80);

 

Area* area10 = new DeadlyArea(“You got stuck into the deep cave and couldn`t come out. Call SOS! and wait until someone comes to rescue you.”, 40);

 

homeArea->AddPath(area2, “Take a boat : Canoe on the longest river of the World. You need a boat to start your journey”);

homeArea->SetBackArea(nullptr);

area2->AddPath(area3, “Enter into cave : There is a hidden cave near the waterfall. Climb the rock to reach the caves”);

area2->AddPath(area4, “Hike : The waterfall origins from the hill top. Hikes through the dense ridge to climb to the top of the hill”);

area2->SetBackArea(homeArea);

area3->AddPath(area5, “Make a fire : Try to light fire with some stones.”);

area3->SetBackArea(area2);

 

area4->AddPath(area6, “Trial : Trial shoe.”);

area4->AddPath(area7, “Hike : Hike shoe.”);

area4->SetBackArea(area2);

 

area5->AddPath(area10, “Continue : Continue exploring into the deep cave.”);

area5->SetBackArea(area3);

area6->AddPath(area8, “Continue : Continues to hike the hill.”);

area6->SetBackArea(area4);

 

area7->AddPath(area8, “Heal : Treat your wounds with the medikit that you have carried with you and continue to hike.”);

area7->AddPath(area9, “Continue : Continue hiking without any rest”);

area7->SetBackArea(area4);

 

area8->SetBackArea(area6);

area9->SetBackArea(area7);

area10->SetBackArea(area5);

 

health = 100;

currentArea = homeArea;

}

 

Reset() method will reset to Health and currentArea as 100 and homeArea respectively.

 

Code:

void GameEngine::Reset()

{

health = 100;

currentArea = homeArea;

}

PrintTitle() will Print title and instruction.

Code:

void GameEngine::PrintTitle()

{

cout << “======================================================================” << endl;

cout << “\t\t\tText-based Adventure Game” << endl;

cout << “======================================================================” << endl << endl;

 

cout << “Instruction : Text-based adventure games is a game where all the interaction takes place \

through on-screen words. You have to choose one of the path option listed. Every move will reduce 5 health.\

Some area may cause more damage to your health.” << endl;

 

cout << endl << endl;

}

 

PlayGame() method will first check whether health is greater than 0, only condition is true game will proceed otherwise Game willl be over.

Printing the path and destination moving. After that will check whether the choosen path is deadly area or safe. Health value reduced 5 for each move whereas when it is deadly move value reduced depends on damage(25, 40,80).

Code:

void GameEngine::PlayGame()

{

while (health > 0)

{

cout << endl << “Your health is ” << health << endl;

this->currentArea->Print();

this->currentArea->PrintPaths();

this->currentArea = (Area*)this->currentArea->MoveDestination();

if ((dynamic_cast<DeadlyArea*>(this->currentArea)) != nullptr)

{

DeadlyArea* deadlyArea = ((DeadlyArea*)(this->currentArea));

cout << endl << “!!!!  You got severe damage and your health will be deducted by ” << deadlyArea->GetDamage() << endl;

health -= deadlyArea->GetDamage();

}

else

health -= 5

if (health <= 0)

{

cout << endl << “!!!!  You are dead !!!!” << endl;

break;

}

else if (this->currentArea->IsDestination())

{

cout << endl << “!!!!  You have reached the final destination successfully !!!!” << endl;

break;

}

}

cout << endl << “!!!!  Game Over !!!!” << endl;

}

Area.cpp

AddPath()  will add description and area to vector variable.

 Code:

void Area::AddPath(AbstractArea* area, string description)

{

paths.push_back(new Path(description, area));

}

PrintPaths() will print one by one path from vector variable using for loop.

 Code:

void Area::PrintPaths()

{

int i = 0;

for (; i < paths.size(); i++)

{

cout << endl << “\tPath # ” << (i + 1) << ” : ” << paths[i]->GetDescription() << endl;

}

 

if (backArea != nullptr)

cout << endl << “\tPath # ” << (i + 1) << ” : ” << ” : Go Back” << endl;

}

MoveDestination() will check whether choosen path, entered input is valid or not.

Code:

AbstractArea* Area::MoveDestination()

{

string input;

int path;

while (true)

{

cout << endl << “Choose a Path :”;

getline(cin, input);

 

try {

path = stoi(input);

}

catch (std::exception e) {

cout << “!! Invalid Input !!” << endl;

continue;

}

path–;

if (path < 0 || path >= (paths.size() + 1))

{

cout << “!! Invalid Path !!” << endl;

continue;

}

else if (path == paths.size())

{

if (backArea != nullptr)

return backArea;

}

else

return paths[path]->GetDestination();

cout << “!! Invalid Path !!” << endl;

path = 0;

}

return nullptr;

}

Previous Post
7 Reasons You Should Use Python Programming Language
Next Post
Project Concepts on Deep Learning for Beginners

Related Posts

No results found.

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.