Skip to content

Instantly share code, notes, and snippets.

@VKONSTANTINIUS
Created April 3, 2025 21:38
Show Gist options
  • Save VKONSTANTINIUS/33e2a218a00feb00ade1a3edd1e3e946 to your computer and use it in GitHub Desktop.
Save VKONSTANTINIUS/33e2a218a00feb00ade1a3edd1e3e946 to your computer and use it in GitHub Desktop.
//Проект "Лабіринт"
//Завдання:
//Якщо персонаж дійшов до виходу з лабіринту в нижньому правому куті,
//гра завершується перемогою
//(вивести діалог із повідомленням : "Перемога – знайдено вихід").
//
//Якщо всі монетки лабіринту зібрані, гра завершується перемогою
//(вивести діалог із повідомленням : "Перемога – монети зібрано").
//
//Додати новий тип об'єктів лабіринту – "ліки", які при збиранні відновлюють здоров'я на 25 очок.
//Здоров'я персонажа не може бути більше 100 очок. Якщо здоров'я на максимумі, ліки не можна зібрати.
//Якщо здоров'я закінчилося (впало до 0), гра завершується поразкою
//(вивести діалог із повідомленням: "Поразка – здоров'я закінчилося").
#include <windows.h>
#include <iostream>
#include <conio.h> // _getch
using namespace std;
int main()
{
srand(time(0));
system("mode con cols=50 lines=20");
system("title Maze");
// hide standard console cursor
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cci;
cci.bVisible = false;
cci.dwSize = 100;
SetConsoleCursorInfo(h, &cci);
const int WIDTH = 45;
const int HEIGHT = 15;
int maze[HEIGHT][WIDTH];
int coins_get = 0, coins_total = 0; // монети
int health = 100; // здров"я персонажа
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 };
system("cls");
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";
}
/////////////////////////////////////////////////////////////////////
// показники персонажа
COORD infobox;
infobox.X = WIDTH + 1;
infobox.Y = 1;
SetConsoleCursorPosition(h, infobox);
SetConsoleTextAttribute(h, YELLOW);
cout << "COINS: ";
SetConsoleTextAttribute(h, YELLOW);
cout << coins_get << "/" << coins_total <<"\n";
infobox.Y = 2;
SetConsoleCursorPosition(h, infobox);
SetConsoleTextAttribute(h, RED);
cout << "HEALTH: ";
SetConsoleTextAttribute(h, RED);
cout << health << "\n";
COORD pers = { 0, 2 };
SetConsoleCursorPosition(h, pers);
SetConsoleTextAttribute(h, BLUE);
cout << "@";
while (true)
{
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 (pers.X > 0 && direct == LEFT && maze[pers.Y][pers.X - 1] != WALL)
{
pers.X--;
}
else if (pers.Y > 0 && direct == UP && maze[pers.Y - 1][pers.X] != WALL)
{
pers.Y--;
}
else if (pers.Y > 0 && direct == DOWN && maze[pers.Y + 1][pers.X] != WALL)
{
pers.Y++;
}
SetConsoleCursorPosition(h, pers);
SetConsoleTextAttribute(h, BLUE);
cout << "@";
if (maze[pers.Y][pers.X] == COIN) {
coins_get++;
maze[pers.Y][pers.X] = HALL;
infobox.X = WIDTH + 1;
infobox.Y = 1;
SetConsoleCursorPosition(h, infobox);
SetConsoleTextAttribute(h, YELLOW);
cout << "COINS: ";
SetConsoleTextAttribute(h, YELLOW);
cout << coins_get << "/" << coins_total << "\n";
}
if (maze[pers.Y][pers.X] == ENEMY) {
health -= 25;
maze[pers.Y][pers.X] = HALL;
infobox.X = WIDTH + 1;
infobox.Y = 2;
SetConsoleCursorPosition(h, infobox);
SetConsoleTextAttribute(h, RED);
cout << "HEALTH: ";
SetConsoleTextAttribute(h, RED);
cout << health << " \n";
}
if (maze[pers.Y][pers.X] == APTEKA) {
if (health < 76){
health += 25;
maze[pers.Y][pers.X] = HALL;
infobox.X = WIDTH + 1;
infobox.Y = 2;
SetConsoleCursorPosition(h, infobox);
SetConsoleTextAttribute(h, RED);
cout << "HEALTH: ";
SetConsoleTextAttribute(h, RED);
cout << health << " \n";
}
else {
SetConsoleTextAttribute(h, RED);
cout << "+";
}
}
if (pers.X == WIDTH - 1 && pers.Y == HEIGHT - 3) {
system("cls");
cout << "Congratulations, you have exited the maze.\n";
system("pause");
main();
}
else if (coins_get == coins_total) {
system("cls");
cout << "Congratulations, you have collected all the coins\n";
system("pause");
main();
}
else if (health == 0) {
system("cls");
cout << "YOU DIED :.-(\n";
system("pause");
main();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment