Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save VKONSTANTINIUS/0290e4a74ef1186a1a8cd302c19793c1 to your computer and use it in GitHub Desktop.
Save VKONSTANTINIUS/0290e4a74ef1186a1a8cd302c19793c1 to your computer and use it in GitHub Desktop.
#include <windows.h>
#include <iostream>
#include <conio.h>
using namespace std;
const int WIDTH = 45;
const int HEIGHT = 15;
enum maze_objects { HALL, WALL, COIN, ENEMY, APTEKA };
enum direction { DOWN = 80, UP = 72, LEFT = 75, RIGHT = 77 };
enum colors { BLUE = 9, RED = 12, YELLOW = 14, DARKGREEN = 2 };
HANDLE h;
int maze[HEIGHT][WIDTH];
int coins_get = 0, coins_total = 0;
int health = 100;
COORD pers = { 0, 2 };
void ConsoleSett() { //Функція налаштувань консолі
system("mode con cols=50 lines=20");
system("title Maze");
h = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cci;
cci.bVisible = false;
cci.dwSize = 100;
SetConsoleCursorInfo(h, &cci);
system("cls");
}
void DrawMaze() { //Функція що створює лабіринт
for (int y = 0; y < HEIGHT; y++) {
for (int x = 0; x < WIDTH; x++) {
int random = rand() % 8;
maze[y][x] = random;
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;
coins_total++;
}
else if (maze[y][x] == ENEMY) {
int r = rand() % 50;
if (r == 0) {
SetConsoleTextAttribute(h, RED);
cout << "@";
}
else {
maze[y][x] = HALL;
cout << " ";
}
}
else if (maze[y][x] == APTEKA) {
int r = rand() % 25;
if (r == 0) {
SetConsoleTextAttribute(h, RED);
cout << "+";
}
else {
maze[y][x] = HALL;
cout << " ";
}
}
else {
maze[y][x] = HALL;
cout << " ";
}
}
cout << "\n";
}
}
void DrawStats() { //Функція відображення показників героя
COORD infobox;
infobox.X = WIDTH + 1;
infobox.Y = 1;
SetConsoleCursorPosition(h, infobox);
SetConsoleTextAttribute(h, YELLOW);
cout << "COINS: " << coins_get << "/" << coins_total << "\n";
infobox.Y = 2;
SetConsoleCursorPosition(h, infobox);
SetConsoleTextAttribute(h, RED);
cout << "HEALTH: " << health << "\n";
}
void UpdateStats() { //Функція оновлення показників героя після взаємодії з предметами
COORD infobox;
infobox.X = WIDTH + 1;
infobox.Y = 1;
SetConsoleCursorPosition(h, infobox);
SetConsoleTextAttribute(h, YELLOW);
cout << "COINS: " << coins_get << "/" << coins_total << " ";
infobox.Y = 2;
SetConsoleCursorPosition(h, infobox);
SetConsoleTextAttribute(h, RED);
cout << "HEALTH: " << health << " ";
}
void HeroMove() { //Рух героя
int direct = _getch();
if (direct == 224)
direct = _getch();
SetConsoleCursorPosition(h, pers);
cout << " ";
if (GetAsyncKeyState(VK_ESCAPE)) {
system("cls");
cout << "ESCAPE!\n";
exit(0);
}
if (direct == RIGHT && maze[pers.Y][pers.X + 1] != WALL) pers.X++;
else if (direct == LEFT && maze[pers.Y][pers.X - 1] != WALL) pers.X--;
else if (direct == UP && maze[pers.Y - 1][pers.X] != WALL) pers.Y--;
else if (direct == DOWN && maze[pers.Y + 1][pers.X] != WALL) pers.Y++;
SetConsoleCursorPosition(h, pers);
SetConsoleTextAttribute(h, BLUE);
cout << "0";
}
void TakeCoin() { //Взаємодія з предметами
if (maze[pers.Y][pers.X] == COIN) {
coins_get++;
maze[pers.Y][pers.X] = HALL;
UpdateStats();
}
else if (maze[pers.Y][pers.X] == ENEMY) {
health -= 25;
maze[pers.Y][pers.X] = HALL;
UpdateStats();
}
else if (maze[pers.Y][pers.X] == APTEKA) {
if (health < 76) {
health += 25;
maze[pers.Y][pers.X] = HALL;
UpdateStats();
}
else {
SetConsoleTextAttribute(h, RED);
cout << "+";
}
}
}
void VictoryCheck() { //Перевірка виходу з лабіринту
if (pers.X == WIDTH - 1 && pers.Y == HEIGHT - 3) {
system("cls");
cout << "Congratulations, you have exited the maze.\n";
system("pause");
exit(0);
}
else if (coins_get == coins_total) {
system("cls");
cout << "Congratulations, you have collected all the coins\n";
system("pause");
exit(0);
}
else if (health == 0) {
system("cls");
cout << "YOU DIED :.-(\n";
system("pause");
exit(0);
}
return;
}
int main() {
if (GetAsyncKeyState(VK_ESCAPE))
{
system("cls");
cout << "ESCAPE!\n";
exit(0);
}
srand(time(0));
ConsoleSett();
DrawMaze();
DrawStats();
SetConsoleCursorPosition(h, pers);
SetConsoleTextAttribute(h, BLUE);
cout << "0";
while (true) {
HeroMove();
TakeCoin();
VictoryCheck();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment