Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //-------------------------Source.cpp-------------------------
- #include "Header.h"
- int main()
- {
- setlocale(LC_ALL, "Russian");
- system("mode con cols=80 lines=40");
- bool isWin7 = !IsWindows8OrGreater();
- vector <Warrior> WarriorsList;
- int MapMatrix[Heigth][Width];
- for (int i = 0; i < Heigth; i++)
- {
- for (int j = 0; j < Width; j++)
- {
- MapMatrix[i][j] = -1;
- }
- }
- //Для курсора
- int xPosCursor, yPosCursor;
- RECT rect;
- POINT point;
- HWND consoleHwnd = GetConsoleHwnd();
- //while (true) //проверка на нажатую клавишу
- // for (int i = 1; i < 257; i++)
- // if (GetAsyncKeyState(i))
- // cout << i << endl;
- while (!GetAsyncKeyState(VK_ESCAPE))
- {
- GetWindowRect(consoleHwnd, &rect);
- GetCursorPos(&point);
- if (isWin7)
- yPosCursor = (point.y - rect.top - 30) / 12; //Координаты Y для вин7
- else
- yPosCursor = (point.y - rect.top - 30) / 16; //Координаты Y для вин8-10
- xPosCursor = (point.x - rect.left - 9) / 8; //Координаты X
- SetCursorPosition(xPosCursor, yPosCursor);
- if (xPosCursor > 1 && xPosCursor < Width - 2 && yPosCursor > 1 && yPosCursor < Heigth - 2
- && MapMatrix[xPosCursor][yPosCursor] == -1 && WarriorsList.size() <= MaxWarriors - 1)
- {
- if (GetAsyncKeyState(VK_F1))
- {
- WarriorsList.push_back(Warrior((int)WarriorsList.size(), xPosCursor, yPosCursor, 60, 15));
- MapMatrix[xPosCursor][yPosCursor] = (int)WarriorsList.size() - 1;
- }
- else if (GetAsyncKeyState(VK_F2))
- {
- WarriorsList.push_back(Warrior((int)WarriorsList.size(), xPosCursor, yPosCursor, 200, 10));
- MapMatrix[xPosCursor][yPosCursor] = (int)WarriorsList.size() - 1;
- }
- else if (GetAsyncKeyState(VK_F3))
- {
- WarriorsList.push_back(Warrior((int)WarriorsList.size(), xPosCursor, yPosCursor, 120, 8));
- MapMatrix[xPosCursor][yPosCursor] = (int)WarriorsList.size() - 1;
- }
- else if (GetAsyncKeyState(VK_F4))
- {
- WarriorsList.push_back(Warrior((int)WarriorsList.size(), xPosCursor, yPosCursor, 500, 100));
- MapMatrix[xPosCursor][yPosCursor] = (int)WarriorsList.size() - 1;
- }
- }
- for (int i = 0; i < (int)WarriorsList.size(); i++)
- {
- int x = WarriorsList[i].GetXPos();
- int y = WarriorsList[i].GetYPos();
- if (WarriorsList[i].GetcurrentHealth() > 0 && x > 0 && y > 0 && x < Width && y < Heigth)
- {
- int damage = WarriorsList[i].GetDamage();
- if (MapMatrix[x - 1][y - 1] > -1)
- WarriorsList[MapMatrix[x - 1][y - 1]].TakeDamage(damage);
- else if (MapMatrix[x - 1][y] > -1)
- WarriorsList[MapMatrix[x - 1][y]].TakeDamage(damage);
- else if (MapMatrix[x - 1][y + 1] > -1)
- WarriorsList[MapMatrix[x - 1][y + 1]].TakeDamage(damage);
- else if (MapMatrix[x][y - 1] > -1)
- WarriorsList[MapMatrix[x][y - 1]].TakeDamage(damage);
- else if (MapMatrix[x][y + 1] > -1)
- WarriorsList[MapMatrix[x][y + 1]].TakeDamage(damage);
- else if (MapMatrix[x + 1][y - 1] > -1)
- WarriorsList[MapMatrix[x + 1][y - 1]].TakeDamage(damage);
- else if (MapMatrix[x + 1][y] > -1)
- WarriorsList[MapMatrix[x + 1][y]].TakeDamage(damage);
- else if (MapMatrix[x + 1][y + 1] > -1)
- WarriorsList[MapMatrix[x + 1][y + 1]].TakeDamage(damage);
- else
- {
- MapMatrix[x][y] = -1;
- int action = WarriorsList[i].Move(x, y);
- switch (action)
- {
- case 0: MapMatrix[x + 1][y] = i; break;
- case 1: MapMatrix[x - 1][y] = i; break;
- case 2: MapMatrix[x][y + 1] = i; break;
- case 3: MapMatrix[x][y - 1] = i; break;
- }
- }
- }
- else
- {
- MapMatrix[x][y] = -1;
- }
- }
- for (int i = 0; i < (int)WarriorsList.size(); i++)
- {
- WarriorsList[i].DrawWarrior();
- WarriorsList[i].TypeInfo(i);
- }
- Sleep(100);
- SetColor(0, 0);
- system("cls");
- DrawCenterLine();
- }
- return 0;
- }
- //-------------------------Warrior.cpp-------------------------
- #include "Header.h"
- int Warrior::Move(int x, int y)
- {
- if (x <= 1)
- {
- xPos += 1;
- return 0;
- }
- else if (x >= Width - 2)
- {
- xPos -= 1;
- return 1;
- }
- else if (y <= 1)
- {
- yPos += 1;
- return 2;
- }
- else if (y >= Heigth - 2)
- {
- yPos -= 1;
- return 3;
- }
- else
- {
- int action = 1 + rand() % 4;
- switch (action)
- {
- case 1: xPos += 1; return 0; //Влево
- case 2: xPos -= 1; return 1; //Вправо
- case 3: yPos += 1; return 2; //Вверх
- case 4: yPos -= 1; return 3; //Вниз
- }
- }
- xPos += 1;
- return 0;
- }
- void Warrior::DrawWarrior()
- {
- SetCursorPosition(xPos, yPos);
- if (currentHealth <= 0)
- {
- SetColor(4, 0);
- cout << 'X';
- }
- else
- {
- SetColor(index + 1, 0);
- cout << 0;
- }
- }
- void Warrior::TypeInfo(int y)
- {
- SetColor(15, 0);
- SetCursorPosition(Width + 3, y);
- if (currentHealth > 0)
- {
- cout << index + 1 << ". [" << currentHealth << '/' << maxHealth << "] DMG: " << damage;
- }
- else
- {
- cout << index + 1 << ".Death! [" << currentHealth << '/' << maxHealth << "] DMG: " << damage;
- }
- }
- //-------------------------Functions.cpp-------------------------
- #include "Header.h"
- void SetCursorPosition(int x, int y)
- {
- COORD coord;
- coord.X = x;
- coord.Y = y;
- SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
- }
- //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
- void SetColor(int text, int background) // устанавливает цвет текста и фона в консоли
- {
- HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
- SetConsoleTextAttribute(hStdOut, (WORD)((background << 4) | text));
- }
- //Для курсора
- 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 DrawCenterLine()
- {
- SetColor(0, 15);
- for (int i = 0; i < Heigth; i++)
- {
- SetCursorPosition(Width + 1, i);
- cout << " ";
- }
- }
- //-------------------------Header.h-------------------------
- #ifndef HEADER_H
- #define HEADER_H
- //Libs
- #include <iostream>
- #include <iostream>
- #include <string>
- #include <clocale>
- #include <windows.h>
- #include <vector>
- #include <VersionHelpers.h>
- using namespace std;
- #define Heigth 40
- #define Width 40
- #define MaxWarriors 40
- //Functions.cpp
- void SetCursorPosition(int x, int y);
- void SetColor(int text, int background);
- HWND GetConsoleHwnd();
- void DrawCenterLine();
- //Warrior.cpp
- class Warrior
- {
- private:
- int index;
- int xPos;
- int yPos;
- int maxHealth;
- int currentHealth;
- int damage;
- public:
- Warrior(int indexIN, int xPosIN, int yPosIN, int HealthIN, int damageIN)
- :index(indexIN), xPos(xPosIN), yPos(yPosIN), maxHealth(HealthIN), currentHealth(HealthIN), damage(damageIN)
- {}
- int Move(int x, int y);
- void DrawWarrior();
- void TypeInfo(int y);
- void TakeDamage(int inputDamage) { currentHealth -= inputDamage; }
- int GetDamage() { return damage; }
- int GetXPos() { return xPos; }
- int GetYPos() { return yPos; }
- int GetcurrentHealth() { return currentHealth; }
- };
- //Source.cpp
- #endif
Advertisement
Add Comment
Please, Sign In to add comment