Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <clocale>
- #include <windows.h>
- #include <vector>
- #include <VersionHelpers.h>
- #include "Source.h"
- using namespace std;
- int MapMatrix[40][40];
- void SetCursorPosition(int x, int y)
- {
- COORD coord;
- coord.X = x;
- coord.Y = y;
- SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
- }
- enum ConsoleColor
- {
- Black = 0,
- Blue = 1,
- Green = 2,
- Cyan = 3,
- Red = 4,
- Magenta = 5,
- Brown = 6,
- LightGray = 7,
- DarkGray = 8,
- LightBlue = 9,
- LightGreen = 10,
- LightCyan = 11,
- LightRed = 12,
- LightMagenta = 13,
- Yellow = 14,
- White = 15
- };
- static void SetColor(int text, int background) // устанавливает цвет текста и фона в консоли
- {
- HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
- SetConsoleTextAttribute(hStdOut, (WORD)((background << 4) | text));
- }
- class Warrior
- {
- private:
- int index;
- int maxHealth;
- int currentHealth;
- int damage;
- int XPos;
- int YPos;
- void DrawWarrior(int x, int y, bool atFirst)
- {
- if (!atFirst)
- {
- SetCursorPosition(XPos, YPos);
- SetColor(0, 0);
- cout << " ";
- MapMatrix[XPos][YPos] = 0;
- }
- SetCursorPosition(x, y);
- SetColor(index + 1, 0);
- cout << index;
- XPos = x;
- YPos = y;
- MapMatrix[XPos][YPos] = index;
- }
- void Move()
- {
- int x = XPos, y = YPos;
- int action = 1 + rand() % 4;
- switch (action)
- {
- case 1: if (x > 1)
- x -= 1; break; //Влево
- case 2: if (x < 40)
- x += 1; break; //Вправо
- case 3: if (y > 1)
- y -= 1; break; //Вверх
- case 4: if (y < 40)
- y += 1; break; //Вниз
- }
- DrawWarrior(x, y, false);
- }
- void Death(/*vector <Warrior> warriorsList*/)
- {
- SetCursorPosition(XPos, YPos);
- SetColor(4, 0);
- cout << "X";
- MapMatrix[XPos][YPos] = -1;
- }
- public:
- Warrior(int inputIndex, int health, int inputDamage, int xPos, int yPos)
- {
- index = inputIndex;
- maxHealth = health;
- currentHealth = health;
- damage = inputDamage;
- XPos = xPos;
- YPos = yPos;
- DrawWarrior(xPos, yPos, true);
- }
- void GetDamage(int inputDamage)
- {
- currentHealth -= inputDamage;
- }
- void LookAround(vector <Warrior> warriorsList)
- {
- if (currentHealth <= 0)
- Death(/*warriorsList*/);
- else
- {
- /*if (XPos > 0 && YPos > 0 && MapMatrix[XPos - 1][YPos - 1] != -1)
- warriorsList[MapMatrix[XPos - 1][YPos - 1]].GetDamage(damage);
- else if (YPos > 0 && MapMatrix[XPos][YPos - 1] != -1)
- warriorsList[MapMatrix[XPos][YPos - 1]].GetDamage(damage);
- else if (XPos < 38 && YPos > 0 && MapMatrix[XPos + 1][YPos - 1] != -1)
- warriorsList[MapMatrix[XPos + 1][YPos - 1]].GetDamage(damage);
- else if (XPos > 0 && MapMatrix[XPos - 1][YPos] != -1)
- warriorsList[MapMatrix[XPos - 1][YPos]].GetDamage(damage);
- else if (XPos < 38 && MapMatrix[XPos + 1][YPos] != -1)
- warriorsList[MapMatrix[XPos + 1][YPos]].GetDamage(damage);
- else if (XPos > 0 && YPos < 38 && MapMatrix[XPos - 1][YPos + 1] != -1)
- warriorsList[MapMatrix[XPos - 1][YPos + 1]].GetDamage(damage);
- else if (YPos < 38 && YPos < 38 && MapMatrix[XPos][YPos + 1] != -1)
- warriorsList[MapMatrix[XPos][YPos + 1]].GetDamage(damage);
- else if (XPos < 38 && YPos < 38 && MapMatrix[XPos + 1][YPos + 1] != -1)
- warriorsList[MapMatrix[XPos + 1][YPos + 1]].GetDamage(damage);
- else */Move();
- }
- }
- };
- vector <Warrior> WarriorsList;
- // Для курсора
- HWND GetConsoleHwnd() {
- #define MY_BUFSIZE 1024
- HWND hwndFound;
- char pszNewWindowTitle[MY_BUFSIZE];
- char pszOldWindowTitle[MY_BUFSIZE];
- GetConsoleTitle(pszOldWindowTitle, MY_BUFSIZE);
- wsprintf(pszNewWindowTitle, "%d/%d",
- GetTickCount(),
- GetCurrentProcessId());
- SetConsoleTitle(pszNewWindowTitle);
- Sleep(1);
- hwndFound = FindWindow(NULL, pszNewWindowTitle);
- SetConsoleTitle(pszOldWindowTitle);
- return(hwndFound);
- }
- void main()
- {
- setlocale(LC_ALL, "Russian");
- system("mode con cols=80 lines=40");
- // Заполняем матрицу карты нулями
- for (int i = 0; i < 38; i++)
- for (int j = 0; j < 38; j++)
- MapMatrix[i][j] = -1;
- SetColor(0, 15);
- for (int i = 0; i < 40; i++)
- {
- SetCursorPosition(41, i);
- cout << " ";
- }
- int xPosCursor, yPosCursor;
- RECT rect;
- POINT point;
- HWND consoleHwnd = GetConsoleHwnd();
- while (!GetAsyncKeyState(VK_ESCAPE))
- {
- GetWindowRect(consoleHwnd, &rect);
- GetCursorPos(&point);
- if (IsWindows8OrGreater()) yPosCursor = (point.y - rect.top - 30) / 16; //Координаты Y для вин8-10
- else yPosCursor = (point.y - rect.top - 30) / 12; //Координаты Y для вин7
- xPosCursor = (point.x - rect.left - 9) / 8; //Координаты X для вин7 и вин10
- SetCursorPosition(xPosCursor, yPosCursor);
- if (GetAsyncKeyState(VK_LBUTTON))
- {
- if (xPosCursor >= 0 && xPosCursor < 39)
- {
- if ((int)WarriorsList.size() < 9)
- {
- WarriorsList.push_back(Warrior((int)WarriorsList.size(), 60, 15, xPosCursor, yPosCursor));
- }
- }
- }
- for (int i = 0; i < (int)WarriorsList.size(); i++)
- {
- WarriorsList[i].LookAround(WarriorsList);
- }
- Sleep(500);
- }
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment