Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include "windows.h"
- #include "conio.h"
- using namespace std;
- enum CellType{floor_cell = 0, wall = 1, player = 2, finish = 3};
- struct Vector2
- {
- int x;
- int y;
- public:
- Vector2(){
- x = 0;
- y = 0;
- }
- Vector2(int new_x, int new_y)
- {
- x = new_x;
- y = new_y;
- }
- friend
- Vector2 operator + (Vector2 v1, Vector2 v2)
- {
- return Vector2(v1.x + v2.x, v1.y + v2.y);
- }
- };
- class Game {
- private:
- char symbols[4];
- int lab[24][24] = { {0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,},
- {1,0,1,0,0,1,0,1,0,0,0,1,1,1,1,0,0,0,0,1,},
- {1,0,1,1,0,1,0,1,0,1,0,0,0,0,1,0,1,1,0,1,},
- {1,0,0,0,0,0,0,1,0,1,1,1,1,0,1,0,0,1,0,1,},
- {1,1,1,1,1,1,0,1,0,1,0,0,1,0,1,1,0,1,0,1,},
- {1,0,0,1,0,0,0,0,0,1,1,0,0,0,0,0,0,1,0,1,},
- {1,0,1,1,0,1,1,1,1,1,0,0,1,0,1,1,1,1,0,1,},
- {1,0,0,0,0,0,0,0,0,1,1,1,1,0,1,0,0,0,0,1,},
- {1,1,1,1,1,1,0,1,1,1,0,0,0,0,1,0,1,1,1,1,},
- {1,1,0,0,0,1,0,0,1,1,0,1,1,1,1,0,0,0,0,1,},
- {1,0,0,1,0,0,0,0,0,1,0,0,0,0,1,1,1,1,0,1,},
- {1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,0,0,0,0,1,},
- {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,1,1,1,},
- {1,0,1,1,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1,},
- {1,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,1,1,0,1,},
- {1,0,1,1,0,1,1,0,1,0,1,1,1,0,1,0,0,0,0,1,},
- {1,0,1,0,0,0,1,0,1,0,0,0,1,1,1,1,1,1,1,1,},
- {1,0,1,0,1,0,1,0,1,0,1,0,0,0,0,0,0,0,0,1,},
- {1,0,1,0,1,0,1,0,1,0,1,1,1,1,1,1,1,1,0,1,},
- {1,0,1,0,1,0,1,0,1,0,0,0,0,0,0,0,0,0,0,1,},
- {1,0,1,0,1,0,0,0,1,0,1,1,1,1,1,1,0,1,0,1,},
- {1,7,1,0,1,1,1,1,1,0,0,0,1,0,1,0,0,1,0,1,},
- {1,0,1,0,0,0,0,0,1,1,1,0,0,0,1,0,1,1,0,1,},
- {1,3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,}
- };
- void Drawlab()
- {
- for (int x = 0; x <= 23; x++)
- {
- for (int y = 0; y <= 23; y++)
- {
- Vector2 pos = Vector2(x, y);
- cout << symbols[lab[x][y]];
- }
- cout << endl;
- }
- }
- int ap = 0;
- Vector2 PlayerCoords;
- bool running = true;
- void PrintSymbol(Vector2 pos, CellType cell_type)
- {
- SetCursorPosition(pos);
- cout << symbols[static_cast<int>(cell_type)];
- }
- void SetCursorPosition(Vector2 coord)
- {
- COORD pos = { coord.x, coord.y };
- HANDLE output = GetStdHandle(STD_OUTPUT_HANDLE);
- SetConsoleCursorPosition(output, pos);
- }
- void HideCursor() {
- CONSOLE_CURSOR_INFO lpCursor;
- HANDLE output = GetStdHandle(STD_OUTPUT_HANDLE);
- GetConsoleCursorInfo(output, &lpCursor);
- lpCursor.bVisible = false;
- SetConsoleCursorInfo(output, &lpCursor);
- }
- //void Draw(char c, int x, int y) {
- //COORD pos = { x, y };
- //HANDLE output = GetStdHandle(STD_OUTPUT_HANDLE);
- //SetConsoleCursorPosition(output, pos);
- //std::cout << c;
- //}
- int CTTI(CellType ct)
- {
- return static_cast<int>(ct);
- }
- bool CheckCell(Vector2 v) {
- bool wallka(lab[v.y][v.x] == CTTI(wall));
- bool finishka(lab[v.y][v.x] == CTTI(finish));
- if (finishka == true)
- {
- cout << "You win!!!!!";
- return true;
- }
- return !wallka;
- }
- bool Finish(Vector2 j)
- {
- return false;
- }
- void MovePlayer(Vector2 shift)
- {
- Vector2 new_coords = PlayerCoords + shift;
- if (!CheckCell(new_coords))
- return;
- PrintSymbol(PlayerCoords, CellType::floor_cell);
- PlayerCoords = new_coords;
- PrintSymbol(PlayerCoords, CellType::player);
- }
- //void PrintMesAbotStart()
- //{
- //hero_y = 30;
- //hero_x = 5;
- //cout << "Press W or S to select level";
- //}
- public:
- Game() {
- symbols[static_cast<int>(floor_cell)] = ' ';
- symbols[static_cast<int>(wall)] = 178;
- symbols[static_cast<int>(player)] = '@';
- symbols[static_cast<int>(finish)] = '*';
- }
- void Run()
- {
- Drawlab();
- const int NotUsed = system("color 7B");
- HideCursor();
- PrintSymbol(Vector2(0, 0), CellType::player);
- while (running)
- {
- char c = _getch();
- switch (c) {
- case 27:
- std::cout << "Game over!" << '\n';
- running = false;
- break;
- case 119:
- Vector2(+3, +2);
- //cout<<player;
- break;
- case 115:
- Vector2(+37, +22);
- // cout << player;
- break;
- case 72:
- MovePlayer( Vector2(0,-1) );
- break;
- case 80:
- MovePlayer(Vector2(0, +1));
- break;
- case 75:
- MovePlayer(Vector2(-1,0));
- break;
- case 77:
- MovePlayer(Vector2(+1, 0));
- break;
- }
- }
- }
- };
- int main()
- {
- Game * game = new Game();
- game->Run();
- }
Add Comment
Please, Sign In to add comment