Code:
/********************************************************************
*Zachs first CPP game. Kind of a RPG. Text based.
*Started Dec. 1; Put in movement
*Modified Dec. 2; put in some traps, items, more commands.
*Modified Dec. 3; Defined enemies. Fixed health/end_game loop.
*Defined Weaponry, bombs, and sword.
*Still can't get item manipultion to work.
*Modified Dec. 3; Got Items to work! Wolf is placed.
*Modified Dec. 4; Ghost is placed, but not yet working.
*Inserted 3 cheats.
*Modified Dec. 9-10; Colored text, improved layout.
*******************************************************************/
#include <iostream>
#include <cstdlib>
#include <conio.h> //allows getch();
#include <iomanip> //for setw
#include <windows.h> //for playsound()
#include <mmsystem.h> //for playsound()
using namespace std; //allows cout instead of std::cout
/*Edit the following for your own custom game*/
int player_location_Y = 0; /* Player*/
int player_location_X = 0; /* Start*/
int health = 100; //player health
int health_potion = 10; //gives ten health
int end_game = 100; // timer: if zero, game ends
int points = 0; //score
int bread = 0; //food
int trap1 = 0; //trap
int trap2 = 0; //trap
int trap3 = 0; //another trap
int trap4 = 0; //yet another trap
int trap5 = 0; //the last trap
int wolf = 1; //wolf enemy
int ghost_Y = 10; //ghost enemie
int ghost_X = 10;
int holy = 0;
int dragon = 1; //dragon enemie
int ledge = 0; //ledge
int bomb = 10; //ticking time bomb
int sword = 0; //weapon
int gold = 2;
int max_h = 100;
int Y;
int X;
int need = 30 - gold;
char input;
void DrawColorString(string szText, int X, int Y, WORD color)
{
HANDLE OutputH;
COORD position = {X, Y};
OutputH = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(OutputH, color);
SetConsoleCursorPosition(OutputH, position);
cout << szText;
}
int main()
{
char name[20];
cout << "Welcome to CAVE.\n ";
DrawColorString("",10, 1, FOREGROUND_RED | FOREGROUND_BLUE);
cout << "\nPlease enter your name: ";
cin >> name;
cout << "\nHello " << name << ". Welcome to my quest!\n";
cout << "You must venture through this maze avoiding traps\n";
getch();
{
while (end_game > 0 || health > 0)
{
if (player_location_Y == 25)
{
system("cls");
DrawColorString("BLOCKED!\n",10, 1, FOREGROUND_RED | FOREGROUND_BLUE);
cout << "That way is blocked!\n\n";
player_location_Y--;
getch();
}
else if (player_location_Y == -25)
{
system("cls");
DrawColorString("BLOCKED!\n",10, 1, FOREGROUND_RED | FOREGROUND_BLUE);
cout << "That way is blocked!\n\n";
player_location_Y++;
getch();
}
else if (player_location_X == 25)
{
system("cls");
DrawColorString("BLOCKED!\n",10, 1, FOREGROUND_RED | FOREGROUND_BLUE);
cout << "That way is blocked!\n";
player_location_Y--;
getch();
}
else if (player_location_X == -25)
{
system("cls");
DrawColorString("BLOCKED!\n",10, 1, FOREGROUND_RED | FOREGROUND_BLUE);
cout << "That way is blocked!\n";
player_location_Y++;
getch();
}
system("cls");
end_game--;
{
if (end_game <= 0)
{
system("cls");
DrawColorString("GAME OVER\n",10, 1, FOREGROUND_RED | FOREGROUND_BLUE);
cout << "Time Out!\n";
system("pause");
return 0;
}
}
{
if (health <= 0)
{
system("cls");
DrawColorString("GAME OVER\n",10, 1, FOREGROUND_RED | FOREGROUND_BLUE);
cout << "Out of health!\n";
cout << "GAME OVER\n";
system("pause");
return 0;
}
}
DrawColorString("\n", 20, 2, FOREGROUND_BLUE | FOREGROUND_GREEN);
cout << setw(20) << "*****************************************\n";
cout << setw(20) << "**|||||****||***|***|***|||||************\n";
cout << setw(20) << "**||******|**|**|***|***||***************\n";
cout << setw(20) << "**||*****||||||**|*|****||--*************\n";
cout << setw(20) << "**||****||****||*|*|****||***************\n";
cout << setw(20) << "**|||||*||****||* | ****|||||************\n";
cout << setw(20) << "*****************************************\n\n";
cout << "__________________<CAVE V. 1.4>__________________\n\n";
cout <<"North (N)\nSouth (S)\nEast (E)\nWest (W)\nHelp (H)\n\n";
cin >> input;
cout <<"\n";
if (input == 'Q' || input == 'q')
{
system("cls");
return 0;
}
if (input == 'H' || input == 'h')
{
system("cls");
DrawColorString("HELP MENU",10, 1, FOREGROUND_RED | FOREGROUND_BLUE);
cout <<"\nNorth (n)\nSouth (S)\nEast(E)\nWest (W)\nQuit (Q)\n";
cout <<"Life (L)\nPoints (P)\nHelp (H)\nTime (T)\nStory (Y)\n";
cout <<"Full status (F)\n\n";
cout <<"Gold: "<< gold<<"\n\n";
cin >> input;
}
if (input == 'L' || input == 'l')
{
system("cls");
DrawColorString("HEALTH",10, 1, FOREGROUND_RED | FOREGROUND_BLUE);
cout << "\nHealth is "<<health<<"\n\n";
getch();
}
if (input == 'v' || input == 'V')
{
system("cls");
DrawColorString("CHEAT MENU",10, 1, FOREGROUND_RED | FOREGROUND_BLUE);
cout << "\nCheats:\n\n";
cout << "More time (x)\n5+ gold (a)\nPosition jump (z)\nWin game (o)\n";
cout << "Must have 30 or more gold to use win game.\n";
cin >> input;
}
if (input == 'P' || input == 'p')
{
system("cls");
DrawColorString("POINTS",10, 1, FOREGROUND_RED | FOREGROUND_BLUE);
cout << "\nPoints are "<<points<<"\n\n";
getch();
}
if (input == 'T' || input == 't')
{
system("cls");
DrawColorString("TIME REMAIN",10, 1, FOREGROUND_RED | FOREGROUND_BLUE);
cout << "\nTime left is "<<end_game<<" moves left.\n\n";
getch();
}
if (input == 'M' || input == 'm')
{
system("cls");
DrawColorString("MAP",10, 1, FOREGROUND_RED | FOREGROUND_BLUE);
cout << "\nPosition is\nY: " << player_location_Y << "\nX: ";
cout << player_location_X <<"\n\n";
getch();
}
if (input == 'y' || input == 'Y')
{
system("cls");
DrawColorString("STORY",10, 1, FOREGROUND_RED | FOREGROUND_BLUE);
cout << "\n\n'Marry my daughter? You? Why, I wouldn't let\n";
cout << "you marry my neighbors dog! You don't have any\n";
cout << "money at all! If you can get 30 gold, you can\n";
cout << "marry her.'\n";
cout << "Those words rang through your head as you searched\n";
cout << "for 30 gold coins. You pawned your gold watch and\n";
cout << "got 6 gold. Then the sheriff came and collected taxes,\n";
cout << "leaving you with 2 coins. No one will hire you.\nn";
cout << "The citizens aren't donating either. There is one\n";
cout << "hope left... the cave. Many people go in, all rich.\n";
cout << "None came out. You were sure to find 30 coins in there.\n\n";
cout << "You remember your mother, 'Don't go in there, you'll\n";
cout << "never come out!\n\n";
cout << "You light a torch and enter the cave....\n\n";
getch();
}
if (input == 'n' || input == 'N')
{
system("cls");
DrawColorString("MOVING",10, 1, FOREGROUND_RED | FOREGROUND_BLUE);
cout << "\nYou venture north\n";
player_location_Y = player_location_Y++;
getch();
}
if (input == 's' || input == 'S')
{
system("cls");
DrawColorString("MOVING",10, 1, FOREGROUND_RED | FOREGROUND_BLUE);
cout << "\nYou venture south\n";
player_location_Y = player_location_Y--;
getch();
}
if (input == 'e' || input == 'E')
{
system("cls");
DrawColorString("MOVING",10, 1, FOREGROUND_RED | FOREGROUND_BLUE);
cout << "\nYou venture east\n";
player_location_X = player_location_X++;
getch();
}
if (input == 'w' || input == 'W')
{
system("cls");
DrawColorString("MOVING",10, 1, FOREGROUND_RED | FOREGROUND_BLUE);
cout << "\nYou venture west\n";
player_location_X = player_location_X--;
getch();
}
if (input == 'z')
{
system("cls");
DrawColorString("POSITION JUMP",10, 1, FOREGROUND_RED | FOREGROUND_BLUE);
cout << "\nEnter location Y: ";
cin >> Y;
cout << "Enter location X: ";
cin >> X;
player_location_Y = Y;
player_location_X = X;
cout << "\nPosition:\nY: " << player_location_Y << "\nX: ";
cout << player_location_X << "\n";
getch();
}
if (input == 'x')
{
system("cls");
DrawColorString("TIME CHEAT, WIMP",10, 1, FOREGROUND_RED | FOREGROUND_BLUE);
cout << "\n10 turns added.";
end_game = end_game + 10;
cout << "\nturns left: " << end_game;
getch();
}
if (input == 'a')
{
system("cls");
DrawColorString("MONEY CHEAT",10, 1, FOREGROUND_RED | FOREGROUND_BLUE);
cout << "\n5 gold added.";
gold = gold + 5;
cout << "\nGold: " << gold;
getch();
}
if (input == 'F' || input == 'f')
{
system("cls");
DrawColorString("FULL STATS",10, 1, FOREGROUND_RED | FOREGROUND_BLUE);
cout << "\nHealth is "<<health<<"\n";
cout << "Gold is: "<<gold<<"\n";
cout << "Points are: "<<points<<"\n";
cout << "Time left: "<<end_game<<" moves.\n";
getch();
}
if (input == 'o' || input == 'O')
{
if (gold < 30)
{
system("cls");
DrawColorString("CHEATER",10, 1, FOREGROUND_RED | FOREGROUND_BLUE);
cout << "\nNot enough gold to win!\n"<<need<<" needed";
getch();
}
else if (gold <= 30)
{
system("cls");
DrawColorString("CHEATER",10, 1, FOREGROUND_RED | FOREGROUND_BLUE);
cout << "\nYou win! Points: " << points << "\n";
system("pause");
return 0;
}
}
if (player_location_Y == 0 && player_location_X == 1) //wall
{
system("cls");
DrawColorString("WALL!",10, 1, FOREGROUND_RED | FOREGROUND_BLUE);
cout << "\nThats a wall, stupid!\n";
player_location_X--;
getch();
}
if (player_location_Y == 1 && player_location_X == 1) //wall
{
system("cls");
DrawColorString("WALL!",10, 1, FOREGROUND_RED | FOREGROUND_BLUE);
cout << "\nThats a wall, stupid!\n";
player_location_X--;
getch();
}
if (player_location_Y == 2 && player_location_X == 1) //wall
{
system("cls");
DrawColorString("WALL!",10, 1, FOREGROUND_RED | FOREGROUND_BLUE);
cout << "\nThats a wall, stupid!\n";
player_location_X--;
getch();
}
if (player_location_Y == 3 && player_location_X == 1) //wall
{
system("cls");
DrawColorString("WALL!",10, 1, FOREGROUND_RED | FOREGROUND_BLUE);
cout << "\nThats a wall, stupid!\n";
player_location_X--;
getch();
}
if (player_location_Y == 4 && player_location_X == 1) //wall
{
system("cls");
DrawColorString("WALL!",10, 1, FOREGROUND_RED | FOREGROUND_BLUE);
cout << "\nThats a wall, stupid!\n";
player_location_X--;
getch();
}
if (player_location_Y == 5 && player_location_X == 0) //fountain
{
if (health < max_h)
{
system("cls");
DrawColorString("Fountain!",10, 1, FOREGROUND_RED | FOREGROUND_BLUE);
cout << "\nFountain of cool water, + 5 health!\n";
getch();
}
else if (health == max_h)
{
system("cls");
DrawColorString("Fountain!",10, 1, FOREGROUND_RED | FOREGROUND_BLUE);
cout << "\nYou can't drink a drop of water.\n";
getch();
}
}
if (player_location_Y == 7 && player_location_X == 0) //bread
{
if (bread == 0)
{
system("cls");
DrawColorString("FOOD!",10, 1, FOREGROUND_RED | FOREGROUND_BLUE);
cout << "\nLoaf of bread! + 20 time!\n";
points = points + 1;
bread = bread + 1;
getch();
}
else if (bread == 1)
{
system("cls");
DrawColorString("CRUMBS!",10, 1, FOREGROUND_RED | FOREGROUND_BLUE);
cout << "\nBread crumbs\n";
getch();
}
}
if (player_location_Y == 3 && player_location_X == 5) //trap1
{
if (trap1 == 0)
{
system("cls");
DrawColorString("TRAP",10, 1, FOREGROUND_RED | FOREGROUND_BLUE);
cout << "\nFire pit! - 10 health!\n";
health = health - 10;
trap1 = trap1 + 1;
getch();
}
else if (trap1 == 1)
{
system("cls");
DrawColorString("TRAP",10, 1, FOREGROUND_RED | FOREGROUND_BLUE);
cout << "\nYou carefully cross the fire pit\n";
getch();
}
}
if (player_location_Y == -5 && player_location_X == 9) //trap2
{
if (trap2 == 0)
{
system("cls");
DrawColorString("TRAP",10, 1, FOREGROUND_RED | FOREGROUND_BLUE);
cout << "\nYIKES! SPIKES! - 25 health!\n";
health = health - 10;
trap2 = trap2 + 1;
getch();
}
else if (trap2 == 1)
{
system("cls");
DrawColorString("TRAP",10, 1, FOREGROUND_RED | FOREGROUND_BLUE);
cout << "\nYou carefully cross the spikes\n";
getch();
}
}
if (player_location_Y == 7 && player_location_X == 5) //sword
{
if (sword == 0)
{
system("cls");
DrawColorString("SWORD",10, 1, FOREGROUND_RED | FOREGROUND_BLUE);
cout << "\nThere is a sword. Heavy, but usable.\n";
sword = sword + 1;
points = points + 10;
getch();
}
else if (sword == 1)
{
system("cls");
DrawColorString("SWORD",10, 1, FOREGROUND_RED | FOREGROUND_BLUE);
cout << "\nA sword used to be here.\n";
getch();
}
}
if (player_location_Y == -5 && player_location_X == 1) //wolf
{
if (sword == 0)
{
system("cls");
DrawColorString("WOLF!",10, 1, FOREGROUND_RED | FOREGROUND_BLUE);
cout << "\nA wolf jumps out of a hole and kills you!\n";
health = 0;
getch();
}
else if (sword == 1 && wolf == 1)
{
system("cls");
DrawColorString("WOLF!",10, 1, FOREGROUND_RED | FOREGROUND_BLUE);
cout << "\nA wolf jumps out of a hole, but you kill it.\n";
wolf = 0;
getch();
}
else if (wolf == 0)
{
system("cls");
DrawColorString("WOLF!",10, 1, FOREGROUND_RED | FOREGROUND_BLUE);
cout << "\nA wolf's corpse remains.\n";
getch();
}
if (player_location_Y == -19 && player_location_X == 7) //light
{
if (gold < 30)
{
system("cls");
DrawColorString("ALMOST!",10, 1, FOREGROUND_RED | FOREGROUND_BLUE);
cout << "\nOne more step to the south, and you'll win..\n";
cout << "but wait...\n";
cout << "Not enough gold!\n";
cout << "Gold needed: " << need << "\n";
getch();
}
else if (gold >= 30)
{
system("cls");
DrawColorString("ALMOST!",10, 1, FOREGROUND_RED | FOREGROUND_BLUE);
cout << "\nOne more step south, and you win!\n";
getch();
}
else if (gold == 50)
{
system("cls");
DrawColorString("ALMOST!",10, 1, FOREGROUND_RED | FOREGROUND_BLUE);
cout << "\n50 gold! WOW! STEP SOUTH TO WIN, MY LORD!.\n";
getch();
}
}
if (player_location_Y == ghost_Y && player_location_X == ghost_X) //ghost
{
if (holy == 0)
{
system("cls");
DrawColorString("G-GHOST!",10, 1, FOREGROUND_RED | FOREGROUND_BLUE);
cout << "\nYou see a ghost. He tries to talk to you\n";
cout << "but you don't have any holy power to hear him\n!";
getch();
}
}
else if (holy == 1)
{
system("cls");
DrawColorString("G-GHOST!",10, 1, FOREGROUND_RED | FOREGROUND_BLUE);
cout << "\nA ghost appears and says:\n";
cout << "At position y (-20) and x (17)\n";
cout << "is your destiny.";
getch();
}
}
}
}
}