Advertisement
Guest User

Main.cpp

a guest
Dec 14th, 2014
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.50 KB | None | 0 0
  1. /*
  2. ============================================================================
  3. ==========================Adventure Game Skeleton!==========================
  4. ============================================================================
  5.  
  6. - This has all of the basic features of a text based adventure game.
  7.  
  8. - 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
  9.     player.
  10.  
  11. - Nothing can go through walls. It will stop you if you try.
  12.  
  13. - The map can be adjusted and changed, ' ' will always be walkable ground, '#' will always be walls
  14.  
  15. - 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)
  16.  
  17. - You could add several maps by making "doors" on specific coordinates that when you walk through them, it causes the new map to load.
  18.     This could possibly lead to a very large map system.
  19.  
  20. - The sword was added and now you can attack enemies. It goes in four directions, and only goes one unit in distance.
  21.  
  22. - 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,
  23.     add to the player's loot. This could possibly lead to a buying system where you can increase the range of your weapon,
  24.     amongst other things such as lives, armor, etc., etc..
  25.  
  26. - 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
  27.     and iterate through that vector, as well as access the items.
  28.  
  29. - I added classes (Hooray!). Now you can call the member functions of the spawned enemies without having to write the code several times.
  30.  
  31. - You can now enter a number into NUM_OF_ENEMIES and it will spawn that many enemies.
  32. */
  33. #include "Functions.h"
  34. #include "Entities.h"
  35.  
  36. using namespace std;
  37.  
  38. const int NUM_OF_ENEMIES = 30;  //You can set this to any number and it will
  39.                                 //spawn that many enemies
  40.  
  41. //These are for holding the enemies that are made and iterating through them
  42. vector<Enemy> EnemyVector;
  43. vector<Enemy>::iterator iter;
  44.  
  45. Player player('@', 1, 1);
  46.  
  47. int main()
  48. {
  49.     srand(static_cast<unsigned int>(time(0)));
  50.  
  51.     int Rounds = 0;
  52.     int HealCounter = 0;
  53.     bool GameBool = true;
  54.     bool Choosing = true;
  55.  
  56.     Enemy * Pointer[NUM_OF_ENEMIES];//Pointer array to create new enemies. Refer to the
  57.                                     //for loop below.
  58.     for (int i = 0; i < NUM_OF_ENEMIES; i++)
  59.     {
  60.         Pointer[i] = new Enemy(EnemyVector);
  61.     }
  62.  
  63.     for (iter = EnemyVector.begin(); iter != EnemyVector.end(); iter++)
  64.     {
  65.         iter->AddToGame();//Adding the enemies to the screen
  66.     }
  67.  
  68.     player.AddToGame();//Adding the player to the screen
  69.  
  70.     ShowBoard();//Show the board
  71.  
  72.     while (GameBool)
  73.     {
  74.         if (HealCounter == 5)
  75.         {
  76.             player.Health += 1;
  77.  
  78.             if (player.Health > 100)
  79.                 player.Health = 100;
  80.  
  81.             HealCounter = 0;
  82.         }
  83.  
  84.         ClearScreen();//Clear the screen
  85.  
  86.         ShowBoard();//Show the board after the enemies have moved
  87.  
  88.         cout << "\nCurrent health: " << player.Health << endl;
  89.         cout << "Current gold: " << player.Gold << endl;
  90.         cout << "\nWhat would you like to do?"
  91.                 "\nB - Go To The Store"
  92.                 "\nW - Move Up"
  93.                 "\nS - Move Down"
  94.                 "\nA - Move Left"
  95.                 "\nD - Move Right"
  96.                 "\nU - Attack Up"
  97.                 "\nJ - Attack Down"
  98.                 "\nH - Attack Left"
  99.                 "\nK - Attack Right\n";
  100.  
  101.         Choosing = true;
  102.  
  103.         Sleep(50);
  104.  
  105.         while (Choosing)
  106.         {
  107.             //Go To The Shop
  108.            
  109.             if (GetAsyncKeyState(B))
  110.             {
  111.                 player.Choice = 'b';
  112.                 Choosing = false;
  113.             }
  114.  
  115.             //Move Up
  116.            
  117.             if (GetAsyncKeyState(W))
  118.             {
  119.                 player.Choice = 'w';
  120.                 Choosing = false;
  121.             }
  122.        
  123.             //Move Left
  124.            
  125.             if (GetAsyncKeyState(A))
  126.             {
  127.                 player.Choice = 'a';
  128.                 Choosing = false;
  129.             }
  130.  
  131.             //Move Down
  132.            
  133.             if (GetAsyncKeyState(S))
  134.             {
  135.                 player.Choice = 's';
  136.                 Choosing = false;
  137.             }
  138.  
  139.             //Move Right
  140.            
  141.             if (GetAsyncKeyState(D))
  142.             {
  143.                 player.Choice = 'd';
  144.                 Choosing = false;
  145.             }
  146.  
  147.  
  148.             //Attack Up
  149.            
  150.             if (GetAsyncKeyState(U))
  151.             {
  152.                 player.Choice = 'u';
  153.                 Choosing = false;
  154.             }
  155.  
  156.             //Attack Left
  157.            
  158.             if (GetAsyncKeyState(H))
  159.             {
  160.                 player.Choice = 'h';
  161.                 Choosing = false;
  162.             }
  163.  
  164.             //Attack Down
  165.            
  166.             if (GetAsyncKeyState(J))
  167.             {
  168.                 player.Choice = 'j';
  169.                 Choosing = false;
  170.             }
  171.  
  172.             //Attack Right
  173.            
  174.             if (GetAsyncKeyState(K))
  175.             {
  176.                 player.Choice = 'k';
  177.                 Choosing = false;
  178.             }
  179.         }
  180.  
  181.         CheckChoice(player.Choice, player.Damage, player.Gold, player.Health, player.Range);
  182.  
  183.         for (iter = EnemyVector.begin(); iter != EnemyVector.end(); iter++)
  184.         {
  185.             iter->Move(player.PlayerX, player.PlayerY, player.Health);//Move all of the enemies
  186.         }
  187.  
  188.         //Check if all enemies are dead
  189.         for (iter = EnemyVector.begin(); iter != EnemyVector.end(); iter++)
  190.         {
  191.             if (iter->IsDead == false)
  192.                 break;//If not, exit the loop
  193.  
  194.             else if (iter == (EnemyVector.end() -1) && iter->IsDead == true)
  195.             {
  196.                 GameBool = false;   //If so, set the GameBool to false and exit the
  197.             }                       //main while loop
  198.         }
  199.  
  200.         if (player.Health <= 0)
  201.             GameBool = false;
  202.  
  203.         if (Win == true)
  204.             GameBool = false;
  205.  
  206.         ++Rounds;
  207.         ++HealCounter;
  208.     }
  209.  
  210.     for (int i = 0; i < NUM_OF_ENEMIES; i++)
  211.     {
  212.         delete Pointer[i];//delete the enemies that were made
  213.         Pointer[i] = NULL;
  214.     }
  215.  
  216.     if (player.Health <= 0)
  217.         cout << "\nYou died!\n";                        //You Lost!!
  218.  
  219.     else if (Win == true)
  220.     {
  221.         cout << "\nYou reached the golden question mark! You win!\n";
  222.         cout << "\nYour score is " << ((((player.Gold / Rounds) * player.Health) * player.Damage) * player.Range) + 500;
  223.     }
  224.  
  225.     else
  226.     {
  227.         cout << "\nYou Defeated All Of The Enemies!\n";//You won!!
  228.         cout << "Your score is " << (((player.Gold / Rounds) * player.Health) * player.Damage) * player.Range;
  229.     }
  230.  
  231.     _getch();//Because system("PAUSE") is a bad thing to use
  232.  
  233.     return 0;
  234. }
  235.  
  236. void CheckChoice(char& choice, int& Damage, int& Gold, int& Health, int& Range)
  237. {
  238.     switch (choice) //You can pick any valid move or attack without
  239.         {                       //having to deal with a cout asking if you want
  240.         case 'b':
  241.             BuyScreen(Damage, Gold, Health, Range);
  242.             break;
  243.        
  244.         case 'w':               //to attack or move.
  245.             player.Move();
  246.             break;
  247.  
  248.         case 's':
  249.             player.Move();
  250.             break;
  251.  
  252.         case 'a':
  253.             player.Move();
  254.             break;
  255.  
  256.         case 'd':
  257.             player.Move();
  258.             break;
  259.  
  260.         case 'u':
  261.             player.Attack(EnemyVector, iter);
  262.             break;
  263.  
  264.         case 'j':
  265.             player.Attack(EnemyVector, iter);
  266.             break;
  267.  
  268.         case 'h':
  269.             player.Attack(EnemyVector, iter);
  270.             break;
  271.  
  272.         case 'k':
  273.             player.Attack(EnemyVector, iter);
  274.             break;
  275.  
  276.         }
  277. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement