Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ============================================================================
- ==========================Adventure Game Skeleton!==========================
- ============================================================================
- - This has all of the basic features of a text based adventure game.
- - The AI isn't very good(all randomized), but it's a start. If you want a really smart enemy, have them go directly for the
- player.
- - Nothing can go through walls. It will stop you if you try.
- - The map can be adjusted and changed, ' ' will always be walkable ground, '#' will always be walls
- - No matter what, you can have the walls anywhere and it will work (As long as there is spawn space, and spawns are in the right place)
- - You could add several maps by making "doors" on specific coordinates that when you walk through them, it causes the new map to load.
- This could possibly lead to a very large map system.
- - The sword was added and now you can attack enemies. It goes in four directions, and only goes one unit in distance.
- - A loot system is now possible. Do a check if the enemy is dead, then if you step on them and they are in fact dead,
- add to the player's loot. This could possibly lead to a buying system where you can increase the range of your weapon,
- amongst other things such as lives, armor, etc., etc..
- - You can make an inventory system with a vector by adding loot, or bought items to it. Then you could add them to a vector
- and iterate through that vector, as well as access the items.
- - I added classes (Hooray!). Now you can call the member functions of the spawned enemies without having to write the code several times.
- - You can now enter a number into NUM_OF_ENEMIES and it will spawn that many enemies.
- */
- #include "Includes.h"
- void ShowBoard();
- void ClearBoard();
- const char Wall = '#';
- const char Ground = ' ';
- const int Length = 20;
- const int Height = 20;
- char board[Height][Length] = { {'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'}, //0
- {'#', ' ', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', '#'}, //1
- {'#', ' ', '#', ' ', '#', '#', '#', '#', ' ', '#', '#', '#', '#', ' ', '#', ' ', '#', '#', ' ', '#'}, //2
- {'#', ' ', '#', ' ', '#', ' ', ' ', ' ', ' ', '#', ' ', ' ', '#', ' ', '#', ' ', ' ', '#', ' ', '#'}, //3
- {'#', ' ', '#', ' ', '#', ' ', '#', '#', '#', '#', ' ', ' ', '#', ' ', '#', '#', '#', '#', ' ', '#'}, //4
- {'#', ' ', '#', '#', '#', ' ', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'}, //5
- {'#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#', '#', '#', '#', '#', ' ', '#', '#', '#', '#', '#', '#'}, //6
- {'#', ' ', '#', '#', '#', '#', '#', ' ', '#', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', ' ', '#'}, //7
- {'#', ' ', '#', ' ', ' ', ' ', ' ', ' ', '#', ' ', '#', ' ', '#', ' ', '#', ' ', '#', ' ', ' ', '#'}, //8
- {'#', ' ', '#', '#', '#', '#', '#', '#', '#', ' ', '#', ' ', '#', ' ', '#', ' ', '#', ' ', ' ', '#'}, //9
- {'#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#', ' ', '#', ' ', '#', ' ', ' ', ' ', ' ', '#'}, //10
- {'#', '#', '#', ' ', '#', '#', '#', '#', '#', ' ', '#', ' ', '#', ' ', '#', ' ', '#', '#', ' ', '#'}, //11
- {'#', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', '#', ' ', '#', ' ', '#', ' ', ' ', '#', ' ', '#'}, //12
- {'#', ' ', '#', '#', '#', ' ', '#', '#', '#', ' ', '#', ' ', '#', ' ', '#', '#', ' ', '#', ' ', '#'}, //13
- {'#', ' ', '#', ' ', ' ', ' ', '#', ' ', '#', ' ', '#', ' ', '#', ' ', ' ', '#', ' ', '#', ' ', '#'}, //14
- {'#', ' ', '#', ' ', '#', ' ', '#', ' ', '#', ' ', '#', ' ', '#', '#', ' ', '#', ' ', '#', ' ', '#'}, //15
- {'#', ' ', ' ', ' ', '#', ' ', '#', ' ', '#', ' ', '#', ' ', ' ', '#', ' ', '#', ' ', '#', ' ', '#'}, //16
- {'#', ' ', '#', '#', '#', ' ', '#', ' ', '#', ' ', '#', '#', ' ', '#', '#', '#', ' ', '#', ' ', '#'}, //17
- {'#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'}, //18
- {'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'}};//19
- // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
- void ShowBoard()
- {
- for (int i = 0; i < Height; i++)
- {
- for (int j = 0; j < Length; j++)
- {
- cout << board[i][j];
- }
- cout << endl;
- }
- }
- class Player
- {
- public:
- void Move();
- void AddToGame();
- void Attack();
- void GetLoot();
- void TakeDamage();
- char Choice;
- char Sign;
- int Gold;
- int Health;
- int PlayerX, PlayerY;
- Player (char, int, int);
- };
- Player::Player (char a, int b, int c)
- {
- Sign = a;
- PlayerX = b;
- PlayerY = c;
- Gold = 0;
- Health = 10;
- }
- void Player::TakeDamage()
- {
- this->Health -= 1;
- }
- Player player('@', 1, 1);
- class Enemy
- {
- bool Random;
- int Choice;
- public:
- char Sign;
- int EnemyX, EnemyY;
- bool IsDead;
- void Move();
- void AddToGame();
- void RandomSpawn();
- void Attack();
- Enemy (char, int, int); //There are several different ways to spawn
- Enemy (int, int); //enemies now
- Enemy (char);
- Enemy ();
- };
- //These are for holding the enemies that are made and iterating through them
- vector<Enemy> EnemyVector;
- vector<Enemy>::iterator iter;
- void Enemy::RandomSpawn() //For the enemy constructors that don't have
- { //an input for the X and Y coordinates
- bool ValidPosition = false;
- while (ValidPosition == false)
- {
- int randX, randY;
- randX = (rand() % 20);
- randY = (rand() % 20);
- if (randX <= 2 || randY <= 2 || board[randY][randX] == Wall)
- {
- continue;
- }
- else
- {
- EnemyX = randX;
- EnemyY = randY;
- ValidPosition = true;
- }
- }
- }
- Enemy::Enemy (char enemySign, int enemyX, int enemyY)
- {
- Sign = enemySign;
- EnemyX = enemyX;
- EnemyY = enemyY;
- IsDead = false;
- EnemyVector.push_back(*this);
- }
- Enemy::Enemy (int enemyX, int enemyY)
- {
- char Signs[] = {'!', '$', '%', '^', '&', '*', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
- char randomSign = (rand() % 32);
- Sign = Signs[randomSign];
- EnemyX = enemyX;
- EnemyY = enemyY;
- IsDead = false;
- EnemyVector.push_back(*this);
- }
- Enemy::Enemy (char enemySign)
- {
- Sign = enemySign;
- RandomSpawn();
- IsDead = false;
- EnemyVector.push_back(*this);
- }
- Enemy::Enemy()
- {
- char Signs[] = {'!', '$', '%', '^', '&', '*', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
- char randomSign = (rand() % 32);
- Sign = Signs[randomSign];
- IsDead = false;
- RandomSpawn();
- EnemyVector.push_back(*this);
- }
- void Enemy::AddToGame() //More so to show them on the first round
- {
- board[EnemyY][EnemyX] = Sign;
- }
- void Enemy::Move()
- {
- char Finished = 'P';
- //Can't have dead enemies moving, can we?
- //"this" refers to the enemy currently using this member function
- while (Finished != 'R' && this->IsDead != true)
- {
- Choice = (rand() % 4);
- //up
- if (Choice == 0)
- {
- //A series of checks to see if the move is valid.
- //Same for all moves that the enemy takes
- --EnemyY;
- if (board[EnemyY][EnemyX] == Wall)
- {
- ++EnemyY;
- continue;
- }
- ++EnemyY;
- --EnemyY;
- if (EnemyY == player.PlayerY && EnemyX == player.PlayerX)
- {
- ++EnemyY;
- player.TakeDamage();
- break;
- }
- ++EnemyY;
- board[EnemyY][EnemyX] = Ground;
- --EnemyY;
- board[EnemyY][EnemyX] = Sign;
- Finished = 'R';
- }
- //down
- else if (Choice == 1)
- {
- ++EnemyY;
- if (board[EnemyY][EnemyX] == Wall)
- {
- --EnemyY;
- continue;
- }
- --EnemyY;
- ++EnemyY;
- if (EnemyY == player.PlayerY && EnemyX == player.PlayerX)
- {
- --EnemyY;
- player.TakeDamage();
- break;
- }
- --EnemyY;
- board[EnemyY][EnemyX] = Ground;
- ++EnemyY;
- board[EnemyY][EnemyX] = Sign;
- Finished = 'R';
- }
- //Right
- else if (Choice == 2)
- {
- ++EnemyX;
- if (board[EnemyY][EnemyX] == Wall)
- {
- --EnemyX;
- continue;
- }
- --EnemyX;
- --EnemyX;
- if (EnemyY == player.PlayerY && EnemyX == player.PlayerX)
- {
- ++EnemyX;
- player.TakeDamage();
- break;
- }
- ++EnemyX;
- board[EnemyY][EnemyX] = Ground;
- ++EnemyX;
- board[EnemyY][EnemyX] = Sign;
- Finished = 'R';
- }
- //Left
- else if (Choice == 3)
- {
- --EnemyX;
- if (board[EnemyY][EnemyX] == Wall)
- {
- ++EnemyX;
- continue;
- }
- ++EnemyX;
- ++EnemyX;
- if (EnemyY == player.PlayerY && EnemyX == player.PlayerX)
- {
- --EnemyX;
- player.TakeDamage();
- break;
- }
- --EnemyX;
- board[EnemyY][EnemyX] = Ground;
- --EnemyX;
- board[EnemyY][EnemyX] = Sign;
- Finished = 'R';
- }
- }
- }
- void Player::GetLoot()
- {
- Gold += 500;
- }
- void Player::Attack()
- {
- bool invalid = true;
- while (invalid)
- {
- //Up
- if (Choice == 'u')
- {
- //Pretty much the same as the move function, except it loops through
- //the enemy vector and sees if they are in the space in the attack
- //direction. If they are, set them to dead.
- --PlayerY;
- if (board[PlayerY][PlayerX] != Wall && board[PlayerY][PlayerX] != Ground)
- {
- for (iter = EnemyVector.begin(); iter != EnemyVector.end(); iter++)
- {
- if (PlayerY == iter->EnemyY && PlayerX == iter->EnemyX)
- {
- iter->IsDead = true;
- GetLoot();
- }
- }
- ++PlayerY;
- break;
- }
- ++PlayerY;
- break;
- }
- //Down
- else if (Choice == 'j')
- {
- ++PlayerY;
- if (board[PlayerY][PlayerX] != Wall && board[PlayerY][PlayerX] != Ground)
- {
- for (iter = EnemyVector.begin(); iter != EnemyVector.end(); iter++)
- {
- if (PlayerY == iter->EnemyY && PlayerX == iter->EnemyX)
- {
- iter->IsDead = true;
- GetLoot();
- }
- }
- --PlayerY;
- break;
- }
- --PlayerY;
- break;
- }
- //Left
- else if (Choice == 'h')
- {
- --PlayerX;
- if (board[PlayerY][PlayerX] != Wall && board[PlayerY][PlayerX] != Ground)
- {
- for (iter = EnemyVector.begin(); iter != EnemyVector.end(); iter++)
- {
- if (PlayerY == iter->EnemyY && PlayerX == iter->EnemyX)
- {
- iter->IsDead = true;
- GetLoot();
- }
- }
- ++PlayerX;
- break;
- }
- ++PlayerX;
- break;
- }
- //Right
- else if (Choice == 'k')
- {
- ++PlayerX;
- if (board[PlayerY][PlayerX] != Wall && board[PlayerY][PlayerX] != Ground)
- {
- for (iter = EnemyVector.begin(); iter != EnemyVector.end(); iter++)
- {
- if (PlayerY == iter->EnemyY && PlayerX == iter->EnemyX)
- {
- iter->IsDead = true;
- GetLoot();
- }
- else
- continue;
- }
- --PlayerX;
- break;
- }
- --PlayerX;
- break;
- }
- }
- }
- void Player::AddToGame()//More so that your character is visible on the first round
- {
- board[PlayerY][PlayerX] = Sign;
- }
- void Player::Move()
- {
- bool invalid = true;
- while (invalid)
- {
- //up
- if (Choice == 'w')
- {
- //A series of checks to see if the move is valid.
- --PlayerY;
- if (board[PlayerY][PlayerX] == Wall)
- {
- ++PlayerY;
- break;
- }
- ++PlayerY;
- board[PlayerY][PlayerX] = Ground;
- --PlayerY;
- board[PlayerY][PlayerX] = Sign;
- invalid = false;
- }
- //down
- else if (Choice == 's')
- {
- ++PlayerY;
- if (board[PlayerY][PlayerX] == Wall)
- {
- --PlayerY;
- break;
- }
- --PlayerY;
- board[PlayerY][PlayerX] = Ground;
- ++PlayerY;
- board[PlayerY][PlayerX] = Sign;
- invalid = false;
- }
- //Right
- else if (Choice == 'd')
- {
- ++PlayerX;
- if (board[PlayerY][PlayerX] == Wall)
- {
- --PlayerX;
- break;
- }
- --PlayerX;
- board[PlayerY][PlayerX] = Ground;
- ++PlayerX;
- board[PlayerY][PlayerX] = Sign;
- invalid = false;
- }
- //Left
- else if (Choice == 'a')
- {
- --PlayerX;
- if (board[PlayerY][PlayerX] == Wall)
- {
- ++PlayerX;
- break;
- }
- ++PlayerX;
- board[PlayerY][PlayerX] = Ground;
- --PlayerX;
- board[PlayerY][PlayerX] = Sign;
- invalid = false;
- }
- }
- }
- int main()
- {
- srand(static_cast<unsigned int>(time(0)));
- int Rounds = 0;
- const int NUM_OF_ENEMIES = 10; //You can set this to any number and it will
- bool GameBool = true; //spawn that many enemies
- Enemy * Pointer[NUM_OF_ENEMIES];//Pointer array to create new enemies. Refer to the
- //for loop below.
- for (int i = 0; i < NUM_OF_ENEMIES; i++)
- {
- Pointer[i] = new Enemy;
- }
- for (iter = EnemyVector.begin(); iter != EnemyVector.end(); iter++)
- {
- iter->AddToGame();//Adding the enemies to the screen
- }
- player.AddToGame();//Adding the player to the screen
- ShowBoard();//Show the board
- while (GameBool)
- {
- ClearScreen();//Clear the screen
- ShowBoard();//Show the board after the enemies have moved
- cout << "\nCurrent health: " << player.Health << endl;
- cout << "Current gold: " << player.Gold << endl;
- cout << "\nWhat would you like to do?\nW - Move Up\nS - Move Down\nA - Move Left\nD - Move Right";
- cout << "\nU - Attack Up\nJ - Attack Down\nH - Attack Left\nK - Attack Right\n";
- cin >> player.Choice;
- switch (player.Choice) //You can pick any valid move or attack without
- { //having to deal with a cout asking if you want
- case 'w': //to attack or move.
- player.Move();
- break;
- case 's':
- player.Move();
- break;
- case 'a':
- player.Move();
- break;
- case 'd':
- player.Move();
- break;
- case 'u':
- player.Attack();
- break;
- case 'j':
- player.Attack();
- break;
- case 'h':
- player.Attack();
- break;
- case 'k':
- player.Attack();
- break;
- }
- for (iter = EnemyVector.begin(); iter != EnemyVector.end(); iter++)
- {
- iter->Move();//Move all of the enemies
- }
- //Check if all enemies are dead
- for (iter = EnemyVector.begin(); iter != EnemyVector.end(); iter++)
- {
- if (iter->IsDead == false)
- break;//If not, exit the loop
- else if (iter == (EnemyVector.end() -1) && iter->IsDead == true)
- {
- GameBool = false; //If so, set the GameBool to false and exit the
- } //main while loop
- }
- if (player.Health <= 0)
- GameBool = false;
- ++Rounds;
- }
- for (int i = 0; i < NUM_OF_ENEMIES; i++)
- {
- delete Pointer[i];//delete the enemies that were made
- Pointer[i] = NULL;
- }
- if (player.Health == 0)
- cout << "\nYou died!\n"; //You Lost!!
- else
- {
- cout << "\nYou Defeated All Of The Enemies!\n";//You won!!
- cout << "Your score is " << (player.Gold / Rounds) * player.Health;
- }
- _getch();//Because system("PAUSE") is a bad thing to use
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement