PhotoShaman

ConsoleWarsSimulator

Mar 7th, 2017
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.48 KB | None | 0 0
  1. //-------------------------Source.cpp-------------------------
  2.  
  3. #include "Header.h"
  4.  
  5. int main()
  6. {
  7.     setlocale(LC_ALL, "Russian");
  8.     system("mode con cols=80 lines=40");
  9.  
  10.     bool isWin7 = !IsWindows8OrGreater();
  11.  
  12.     vector <Warrior> WarriorsList;
  13.     int MapMatrix[Heigth][Width];
  14.  
  15.     for (int i = 0; i < Heigth; i++)
  16.     {
  17.         for (int j = 0; j < Width; j++)
  18.         {
  19.             MapMatrix[i][j] = -1;
  20.         }
  21.     }
  22.  
  23.     //Для курсора
  24.     int xPosCursor, yPosCursor;
  25.     RECT rect;
  26.     POINT point;
  27.     HWND consoleHwnd = GetConsoleHwnd();
  28.  
  29.  
  30.     //while (true) //проверка на нажатую клавишу
  31.     //  for (int i = 1; i < 257; i++)
  32.     //      if (GetAsyncKeyState(i))    
  33.     //          cout << i << endl;
  34.  
  35.  
  36.     while (!GetAsyncKeyState(VK_ESCAPE))
  37.     {
  38.         GetWindowRect(consoleHwnd, &rect);
  39.         GetCursorPos(&point);
  40.         if (isWin7)
  41.             yPosCursor = (point.y - rect.top - 30) / 12; //Координаты Y для вин7
  42.         else
  43.             yPosCursor = (point.y - rect.top - 30) / 16; //Координаты Y для вин8-10
  44.         xPosCursor = (point.x - rect.left - 9) / 8; //Координаты X
  45.         SetCursorPosition(xPosCursor, yPosCursor);
  46.  
  47.         if (xPosCursor > 1 && xPosCursor < Width - 2 && yPosCursor > 1 && yPosCursor < Heigth - 2
  48.             && MapMatrix[xPosCursor][yPosCursor] == -1 && WarriorsList.size() <= MaxWarriors - 1)
  49.         {
  50.             if (GetAsyncKeyState(VK_F1))
  51.             {
  52.                 WarriorsList.push_back(Warrior((int)WarriorsList.size(), xPosCursor, yPosCursor, 60, 15));
  53.                 MapMatrix[xPosCursor][yPosCursor] = (int)WarriorsList.size() - 1;
  54.             }
  55.             else if (GetAsyncKeyState(VK_F2))
  56.             {
  57.                 WarriorsList.push_back(Warrior((int)WarriorsList.size(), xPosCursor, yPosCursor, 200, 10));
  58.                 MapMatrix[xPosCursor][yPosCursor] = (int)WarriorsList.size() - 1;
  59.             }
  60.             else if (GetAsyncKeyState(VK_F3))
  61.             {
  62.                 WarriorsList.push_back(Warrior((int)WarriorsList.size(), xPosCursor, yPosCursor, 120, 8));
  63.                 MapMatrix[xPosCursor][yPosCursor] = (int)WarriorsList.size() - 1;
  64.             }
  65.             else if (GetAsyncKeyState(VK_F4))
  66.             {
  67.                 WarriorsList.push_back(Warrior((int)WarriorsList.size(), xPosCursor, yPosCursor, 500, 100));
  68.                 MapMatrix[xPosCursor][yPosCursor] = (int)WarriorsList.size() - 1;
  69.             }
  70.         }
  71.         for (int i = 0; i < (int)WarriorsList.size(); i++)
  72.         {
  73.             int x = WarriorsList[i].GetXPos();
  74.             int y = WarriorsList[i].GetYPos();
  75.             if (WarriorsList[i].GetcurrentHealth() > 0 && x > 0 && y > 0 && x < Width && y < Heigth)
  76.             {
  77.                 int damage = WarriorsList[i].GetDamage();
  78.  
  79.                 if (MapMatrix[x - 1][y - 1] > -1)
  80.                     WarriorsList[MapMatrix[x - 1][y - 1]].TakeDamage(damage);
  81.                 else if (MapMatrix[x - 1][y] > -1)
  82.                     WarriorsList[MapMatrix[x - 1][y]].TakeDamage(damage);
  83.                 else if (MapMatrix[x - 1][y + 1] > -1)
  84.                     WarriorsList[MapMatrix[x - 1][y + 1]].TakeDamage(damage);
  85.                 else if (MapMatrix[x][y - 1] > -1)
  86.                     WarriorsList[MapMatrix[x][y - 1]].TakeDamage(damage);
  87.                 else if (MapMatrix[x][y + 1] > -1)
  88.                     WarriorsList[MapMatrix[x][y + 1]].TakeDamage(damage);
  89.                 else if (MapMatrix[x + 1][y - 1] > -1)
  90.                     WarriorsList[MapMatrix[x + 1][y - 1]].TakeDamage(damage);
  91.                 else if (MapMatrix[x + 1][y] > -1)
  92.                     WarriorsList[MapMatrix[x + 1][y]].TakeDamage(damage);
  93.                 else if (MapMatrix[x + 1][y + 1] > -1)
  94.                     WarriorsList[MapMatrix[x + 1][y + 1]].TakeDamage(damage);
  95.                 else
  96.                 {
  97.                     MapMatrix[x][y] = -1;
  98.                     int action = WarriorsList[i].Move(x, y);
  99.                     switch (action)
  100.                     {
  101.                     case 0: MapMatrix[x + 1][y] = i; break;
  102.                     case 1: MapMatrix[x - 1][y] = i; break;
  103.                     case 2: MapMatrix[x][y + 1] = i; break;
  104.                     case 3: MapMatrix[x][y - 1] = i; break;
  105.                     }
  106.                 }
  107.             }
  108.             else
  109.             {
  110.                 MapMatrix[x][y] = -1;
  111.             }
  112.         }
  113.         for (int i = 0; i < (int)WarriorsList.size(); i++)
  114.         {
  115.             WarriorsList[i].DrawWarrior();
  116.             WarriorsList[i].TypeInfo(i);
  117.         }
  118.         Sleep(100);
  119.         SetColor(0, 0);
  120.         system("cls");
  121.         DrawCenterLine();
  122.     }
  123.     return 0;
  124. }
  125.  
  126. //-------------------------Warrior.cpp-------------------------
  127.  
  128. #include "Header.h"
  129.  
  130. int Warrior::Move(int x, int y)
  131. {
  132.     if (x <= 1)
  133.     {
  134.         xPos += 1;
  135.         return 0;
  136.     }
  137.     else if (x >= Width - 2)
  138.     {
  139.         xPos -= 1;
  140.         return 1;
  141.     }
  142.     else if (y <= 1)
  143.     {
  144.         yPos += 1;
  145.         return 2;
  146.     }
  147.     else if (y >= Heigth - 2)
  148.     {
  149.         yPos -= 1;
  150.         return 3;
  151.     }
  152.     else
  153.     {
  154.         int action = 1 + rand() % 4;
  155.         switch (action)
  156.         {
  157.         case 1: xPos += 1; return 0;  //Влево
  158.         case 2: xPos -= 1; return 1;  //Вправо
  159.         case 3: yPos += 1; return 2;  //Вверх
  160.         case 4: yPos -= 1; return 3;  //Вниз
  161.         }
  162.     }
  163.     xPos += 1;
  164.     return 0;
  165. }
  166.  
  167. void Warrior::DrawWarrior()
  168. {
  169.     SetCursorPosition(xPos, yPos);
  170.     if (currentHealth <= 0)
  171.     {
  172.         SetColor(4, 0);
  173.         cout << 'X';
  174.     }
  175.     else
  176.     {
  177.         SetColor(index + 1, 0);
  178.         cout << 0;
  179.     }
  180. }
  181.  
  182. void Warrior::TypeInfo(int y)
  183. {
  184.     SetColor(15, 0);
  185.     SetCursorPosition(Width + 3, y);
  186.     if (currentHealth > 0)
  187.     {
  188.         cout << index + 1 << ". [" << currentHealth << '/' << maxHealth << "] DMG: " << damage;
  189.     }
  190.     else
  191.     {
  192.         cout << index + 1 << ".Death! [" << currentHealth << '/' << maxHealth << "] DMG: " << damage;
  193.     }
  194. }
  195.  
  196. //-------------------------Functions.cpp-------------------------
  197.  
  198. #include "Header.h"
  199.  
  200. void SetCursorPosition(int x, int y)
  201. {
  202.     COORD coord;
  203.     coord.X = x;
  204.     coord.Y = y;
  205.     SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
  206. }
  207.     //Black =           0,
  208.     //Blue =            1,
  209.     //Green =           2,
  210.     //Cyan =            3,
  211.     //Red =             4,
  212.     //Magenta =         5,
  213.     //Brown =           6,
  214.     //LightGray =       7,
  215.     //DarkGray =        8,
  216.     //LightBlue =       9,
  217.     //LightGreen =      10,
  218.     //LightCyan =       11,
  219.     //LightRed =        12,
  220.     //LightMagenta =    13,
  221.     //Yellow =          14,
  222.     //White =           15
  223. void SetColor(int text, int background) // устанавливает цвет текста и фона в консоли
  224. {
  225.     HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  226.     SetConsoleTextAttribute(hStdOut, (WORD)((background << 4) | text));
  227. }
  228. //Для курсора
  229. HWND GetConsoleHwnd()
  230. {
  231. #define MY_BUFSIZE 1024
  232.     HWND hwndFound;
  233.     char pszNewWindowTitle[MY_BUFSIZE];
  234.     char pszOldWindowTitle[MY_BUFSIZE];
  235.     GetConsoleTitle(pszOldWindowTitle, MY_BUFSIZE);
  236.     wsprintf(pszNewWindowTitle, "%d/%d",
  237.         GetTickCount(),
  238.         GetCurrentProcessId());
  239.     SetConsoleTitle(pszNewWindowTitle);
  240.     Sleep(1);
  241.     hwndFound = FindWindow(NULL, pszNewWindowTitle);
  242.     SetConsoleTitle(pszOldWindowTitle);
  243.     return(hwndFound);
  244. }
  245.  
  246. void DrawCenterLine()
  247. {
  248.     SetColor(0, 15);
  249.     for (int i = 0; i < Heigth; i++)
  250.     {
  251.         SetCursorPosition(Width + 1, i);
  252.         cout << " ";
  253.     }
  254. }
  255.  
  256. //-------------------------Header.h-------------------------
  257.  
  258. #ifndef HEADER_H
  259. #define HEADER_H
  260. //Libs
  261. #include <iostream>
  262. #include <iostream>
  263. #include <string>
  264. #include <clocale>
  265. #include <windows.h>
  266. #include <vector>
  267. #include <VersionHelpers.h>
  268. using namespace std;
  269.  
  270. #define Heigth 40
  271. #define Width 40
  272. #define MaxWarriors 40
  273. //Functions.cpp
  274.  
  275. void SetCursorPosition(int x, int y);
  276. void SetColor(int text, int background);
  277. HWND GetConsoleHwnd();
  278. void DrawCenterLine();
  279.  
  280.  
  281. //Warrior.cpp
  282.  
  283. class Warrior
  284. {
  285. private:
  286.     int index;
  287.     int xPos;
  288.     int yPos;
  289.     int maxHealth;
  290.     int currentHealth;
  291.     int damage;
  292.  
  293. public:
  294.     Warrior(int indexIN, int xPosIN, int yPosIN, int HealthIN, int damageIN)
  295.         :index(indexIN), xPos(xPosIN), yPos(yPosIN), maxHealth(HealthIN), currentHealth(HealthIN), damage(damageIN)
  296.     {}
  297.     int Move(int x, int y);
  298.     void DrawWarrior();
  299.     void TypeInfo(int y);
  300.     void TakeDamage(int inputDamage) { currentHealth -= inputDamage; }
  301.     int GetDamage() { return damage; }
  302.     int GetXPos() { return xPos; }
  303.     int GetYPos() { return yPos; }
  304.     int GetcurrentHealth() { return currentHealth; }
  305. };
  306.  
  307.  
  308. //Source.cpp
  309.  
  310. #endif
Advertisement
Add Comment
Please, Sign In to add comment