PhotoShaman

Зашквар

Mar 5th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.09 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <clocale>
  4. #include <windows.h>
  5. #include <vector>
  6. #include <VersionHelpers.h>
  7. #include "Source.h"
  8.  
  9. using namespace std;
  10.  
  11. int MapMatrix[40][40];
  12.  
  13. void SetCursorPosition(int x, int y)
  14. {
  15.     COORD coord;
  16.     coord.X = x;
  17.     coord.Y = y;
  18.     SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
  19. }
  20. enum ConsoleColor
  21. {
  22.     Black = 0,
  23.     Blue = 1,
  24.     Green = 2,
  25.     Cyan = 3,
  26.     Red = 4,
  27.     Magenta = 5,
  28.     Brown = 6,
  29.     LightGray = 7,
  30.     DarkGray = 8,
  31.     LightBlue = 9,
  32.     LightGreen = 10,
  33.     LightCyan = 11,
  34.     LightRed = 12,
  35.     LightMagenta = 13,
  36.     Yellow = 14,
  37.     White = 15
  38. };
  39. static void SetColor(int text, int background) // устанавливает цвет текста и фона в консоли
  40. {
  41.     HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  42.     SetConsoleTextAttribute(hStdOut, (WORD)((background << 4) | text));
  43. }
  44.  
  45. class Warrior
  46. {
  47. private:
  48.     int index;
  49.     int maxHealth;
  50.     int currentHealth;
  51.     int damage;
  52.     int XPos;
  53.     int YPos;
  54.  
  55.     void DrawWarrior(int x, int y, bool atFirst)
  56.     {
  57.         if (!atFirst)
  58.         {
  59.             SetCursorPosition(XPos, YPos);
  60.             SetColor(0, 0);
  61.             cout << " ";
  62.             MapMatrix[XPos][YPos] = 0;
  63.         }
  64.         SetCursorPosition(x, y);
  65.         SetColor(index + 1, 0);
  66.         cout << index;
  67.         XPos = x;
  68.         YPos = y;
  69.         MapMatrix[XPos][YPos] = index;
  70.     }
  71.     void Move()
  72.     {
  73.         int x = XPos, y = YPos;
  74.         int action = 1 + rand() % 4;
  75.         switch (action)
  76.         {
  77.         case 1: if (x > 1)
  78.             x -= 1; break;  //Влево
  79.         case 2: if (x < 40)
  80.             x += 1; break;  //Вправо
  81.         case 3: if (y > 1)
  82.             y -= 1; break;  //Вверх
  83.         case 4: if (y < 40)
  84.             y += 1; break;  //Вниз
  85.         }
  86.         DrawWarrior(x, y, false);
  87.     }
  88.     void Death(/*vector <Warrior> warriorsList*/)
  89.     {
  90.         SetCursorPosition(XPos, YPos);
  91.         SetColor(4, 0);
  92.         cout << "X";
  93.         MapMatrix[XPos][YPos] = -1;
  94.     }
  95. public:
  96.     Warrior(int inputIndex, int health, int inputDamage, int xPos, int yPos)
  97.     {
  98.         index = inputIndex;
  99.         maxHealth = health;
  100.         currentHealth = health;
  101.         damage = inputDamage;
  102.  
  103.         XPos = xPos;
  104.         YPos = yPos;
  105.         DrawWarrior(xPos, yPos, true);
  106.     }
  107.     void GetDamage(int inputDamage)
  108.     {
  109.         currentHealth -= inputDamage;
  110.     }
  111.     void LookAround(vector <Warrior> warriorsList)
  112.     {
  113.         if (currentHealth <= 0)
  114.             Death(/*warriorsList*/);
  115.         else
  116.         {
  117.             /*if (XPos > 0 && YPos > 0 && MapMatrix[XPos - 1][YPos - 1] != -1)
  118.                 warriorsList[MapMatrix[XPos - 1][YPos - 1]].GetDamage(damage);
  119.             else if (YPos > 0 && MapMatrix[XPos][YPos - 1] != -1)                  
  120.                 warriorsList[MapMatrix[XPos][YPos - 1]].GetDamage(damage);
  121.             else if (XPos < 38 && YPos > 0 && MapMatrix[XPos + 1][YPos - 1] != -1)
  122.                 warriorsList[MapMatrix[XPos + 1][YPos - 1]].GetDamage(damage);
  123.             else if (XPos > 0 && MapMatrix[XPos - 1][YPos] != -1)
  124.                 warriorsList[MapMatrix[XPos - 1][YPos]].GetDamage(damage);
  125.             else if (XPos < 38 && MapMatrix[XPos + 1][YPos] != -1)
  126.                 warriorsList[MapMatrix[XPos + 1][YPos]].GetDamage(damage);
  127.             else if (XPos > 0 && YPos < 38 && MapMatrix[XPos - 1][YPos + 1] != -1)
  128.                 warriorsList[MapMatrix[XPos - 1][YPos + 1]].GetDamage(damage);
  129.             else if (YPos < 38 && YPos < 38 && MapMatrix[XPos][YPos + 1] != -1)
  130.                 warriorsList[MapMatrix[XPos][YPos + 1]].GetDamage(damage);
  131.             else if (XPos < 38 && YPos < 38 && MapMatrix[XPos + 1][YPos + 1] != -1)
  132.                 warriorsList[MapMatrix[XPos + 1][YPos + 1]].GetDamage(damage);
  133.             else */Move();
  134.         }
  135.     }
  136. };
  137.  
  138. vector <Warrior> WarriorsList;
  139.  
  140. // Для курсора
  141. HWND GetConsoleHwnd() {
  142. #define MY_BUFSIZE 1024
  143.     HWND hwndFound;
  144.     char pszNewWindowTitle[MY_BUFSIZE];
  145.     char pszOldWindowTitle[MY_BUFSIZE];
  146.     GetConsoleTitle(pszOldWindowTitle, MY_BUFSIZE);
  147.     wsprintf(pszNewWindowTitle, "%d/%d",
  148.         GetTickCount(),
  149.         GetCurrentProcessId());
  150.     SetConsoleTitle(pszNewWindowTitle);
  151.     Sleep(1);
  152.     hwndFound = FindWindow(NULL, pszNewWindowTitle);
  153.     SetConsoleTitle(pszOldWindowTitle);
  154.     return(hwndFound);
  155. }
  156.  
  157. void main()
  158. {
  159.     setlocale(LC_ALL, "Russian");
  160.     system("mode con cols=80 lines=40");
  161.     // Заполняем матрицу карты нулями
  162.     for (int i = 0; i < 38; i++)
  163.         for (int j = 0; j < 38; j++)
  164.             MapMatrix[i][j] = -1;
  165.  
  166.     SetColor(0, 15);
  167.  
  168.     for (int i = 0; i < 40; i++)
  169.     {
  170.         SetCursorPosition(41, i);
  171.         cout << " ";
  172.     }
  173.  
  174.     int xPosCursor, yPosCursor;
  175.     RECT rect;
  176.     POINT point;
  177.     HWND consoleHwnd = GetConsoleHwnd();
  178.  
  179.     while (!GetAsyncKeyState(VK_ESCAPE))
  180.     {
  181.         GetWindowRect(consoleHwnd, &rect);
  182.         GetCursorPos(&point);
  183.         if (IsWindows8OrGreater()) yPosCursor = (point.y - rect.top - 30) / 16; //Координаты Y для вин8-10
  184.         else yPosCursor = (point.y - rect.top - 30) / 12; //Координаты Y для вин7
  185.         xPosCursor = (point.x - rect.left - 9) / 8; //Координаты X для вин7 и вин10
  186.         SetCursorPosition(xPosCursor, yPosCursor);
  187.         if (GetAsyncKeyState(VK_LBUTTON))
  188.         {
  189.             if (xPosCursor >= 0 && xPosCursor < 39)
  190.             {
  191.                 if ((int)WarriorsList.size() < 9)
  192.                 {
  193.                     WarriorsList.push_back(Warrior((int)WarriorsList.size(), 60, 15, xPosCursor, yPosCursor));
  194.                 }
  195.             }
  196.         }
  197.         for (int i = 0; i < (int)WarriorsList.size(); i++)
  198.         {
  199.             WarriorsList[i].LookAround(WarriorsList);
  200.         }
  201.         Sleep(500);
  202.     }
  203.     return;
  204. }
Advertisement
Add Comment
Please, Sign In to add comment