Created
October 11, 2024 10:36
-
-
Save insassiable/c61610ab85cf907ca43fb556ff4eed67 to your computer and use it in GitHub Desktop.
Либиринт
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <windows.h> | |
#include <iostream> | |
#include <conio.h> | |
using namespace std; | |
enum Objects { HALL, WALL, COIN, ENEMY, MKIT, COFFEE, BOSS }; | |
enum Keycodes { DOWN = 80, UP = 72, LEFT = 75, RIGHT = 77, ENTER = 13, ESCAPE = 27, BACKSPACE = 8, SPACEBAR = 32 }; | |
enum Colors { BLACK, DARKBLUE, DARKGREEN, DARKCYAN, DARKRED, BROWN = 4, DARKMAGENTA, DARKPINK = 5, DARKYELLOW, GREY, GRAY = 7, DARKGREY, BLUE, GREEN, CYAN, RED, PINK, YELLOW, WHITE }; | |
int main() | |
{ | |
srand(time(0)); | |
system("mode con cols=70 lines=25"); | |
system("title My Labyrinth"); | |
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE); | |
CONSOLE_CURSOR_INFO cci; | |
cci.bVisible = false; | |
cci.dwSize = 100; | |
SetConsoleCursorInfo(h, &cci); | |
int width = 50; | |
int height = 20; | |
int** maze = new int* [height]; | |
for (int y = 0; y < height; y++) { | |
maze[y] = new int[width]; | |
} | |
for (int y = 0; y < height; y++) | |
{ | |
for (int x = 0; x < width; x++) | |
{ | |
maze[y][x] = rand() % 5; | |
if (x == 0 || y == 0 || x == width - 1 || y == height - 1) | |
maze[y][x] = WALL; | |
if (x == 0 && y == 2 || x == 1 && y == 2 | |
|| x == width - 1 && y == height - 3) | |
maze[y][x] = HALL; | |
if (maze[y][x] == WALL) { | |
SetConsoleTextAttribute(h, DARKGREEN); | |
cout << (char)178; | |
} | |
else if (maze[y][x] == COIN) { | |
SetConsoleTextAttribute(h, YELLOW); | |
cout << (char)250; | |
} | |
else if (maze[y][x] == ENEMY) | |
{ | |
int r = rand() % 10; | |
if (r == 0) | |
{ | |
SetConsoleTextAttribute(h, RED); | |
cout << (char)1; | |
} | |
else | |
{ | |
maze[y][x] = HALL; | |
cout << " "; | |
} | |
} | |
else if (maze[y][x] == MKIT) { | |
int r = rand() % 50; | |
if (r == 0) | |
{ | |
SetConsoleTextAttribute(h, WHITE * 16 + RED); | |
cout << "+"; | |
SetConsoleTextAttribute(h, GREY); | |
} | |
else | |
{ | |
maze[y][x] = HALL; | |
cout << " "; | |
} | |
} | |
else | |
{ | |
cout << " "; | |
} | |
} | |
cout << "\n"; | |
} | |
int coins = 0; | |
int health = 100; | |
int energy = 500; | |
COORD info; | |
info.X = width + 1; | |
info.Y = 1; | |
SetConsoleCursorPosition(h, info); | |
SetConsoleTextAttribute(h, RED); | |
cout << "Health: " << health; | |
info.Y++; | |
SetConsoleCursorPosition(h, info); | |
SetConsoleTextAttribute(h, YELLOW); | |
cout << "Coins: " << coins; | |
info.Y++; | |
SetConsoleCursorPosition(h, info); | |
SetConsoleTextAttribute(h, BLUE); | |
cout << "Energy: " << energy; | |
COORD pers = { 0, 2 }; | |
SetConsoleCursorPosition(h, pers); | |
SetConsoleTextAttribute(h, BLUE); | |
cout << (char)1; | |
while (true) { | |
int direction = _getch(); | |
if (direction == 224) direction = _getch(); | |
SetConsoleCursorPosition(h, pers); | |
cout << " "; | |
bool moved = false; | |
if (direction == RIGHT && maze[pers.Y][pers.X + 1] != WALL) | |
{ | |
pers.X++; | |
moved = true; | |
} | |
else if (direction == LEFT && pers.X > 0 && maze[pers.Y][pers.X - 1] != WALL) | |
{ | |
pers.X--; | |
moved = true; | |
} | |
else if (direction == UP && maze[pers.Y - 1][pers.X] != WALL) | |
{ | |
pers.Y--; | |
moved = true; | |
} | |
else if (direction == DOWN && maze[pers.Y + 1][pers.X] != WALL) | |
{ | |
pers.Y++; | |
moved = true; | |
} | |
SetConsoleCursorPosition(h, pers); | |
SetConsoleTextAttribute(h, BLUE); | |
cout << (char)1; | |
if (maze[pers.Y][pers.X] == COIN) { | |
coins++; | |
maze[pers.Y][pers.X] = HALL; | |
info.X = width + 1; | |
info.Y = 2; | |
SetConsoleCursorPosition(h, info); | |
SetConsoleTextAttribute(h, YELLOW); | |
cout << "Coins: " << coins; | |
} | |
if (maze[pers.Y][pers.X] == ENEMY) { | |
health -= 10; | |
maze[pers.Y][pers.X] = HALL; | |
info.X = width + 1; | |
info.Y = 1; | |
SetConsoleCursorPosition(h, info); | |
SetConsoleTextAttribute(h, RED); | |
cout << "Health: " << health << " "; | |
} | |
if (maze[pers.Y][pers.X] == MKIT) { | |
health += 5; | |
maze[pers.Y][pers.X] = HALL; | |
info.X = width + 1; | |
info.Y = 1; | |
SetConsoleCursorPosition(h, info); | |
SetConsoleTextAttribute(h, RED); | |
cout << "Health: " << health << " "; | |
} | |
if (health > 100) { | |
health = 100; | |
info.X = width + 1; | |
info.Y = 1; | |
SetConsoleCursorPosition(h, info); | |
SetConsoleTextAttribute(h, RED); | |
cout << "Health: " << health << " "; | |
} | |
if (health <= 0) { | |
system("cls"); | |
SetConsoleTextAttribute(h, RED); | |
cout << "YOU LOSE! ENERGY IS OFF!\n"; | |
break; | |
} | |
if (maze[pers.Y][pers.X]) { | |
system("cls"); | |
SetConsoleTextAttribute(h, CYAN); | |
cout << "YOU WIN!!!\n"; | |
break; | |
} | |
if (moved == true) { | |
energy--; | |
info.X = width + 1; | |
info.Y = 3; | |
SetConsoleCursorPosition(h, info); | |
SetConsoleTextAttribute(h, BLUE); | |
cout << "Energy: " << energy << " "; | |
if (energy <= 0) { | |
system("cls"); | |
SetConsoleTextAttribute(h, RED); | |
cout << "YOU LOSE! ENERGY IS OFF!\n"; | |
break; | |
} | |
} | |
} | |
SetConsoleTextAttribute(h, BLACK); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment