Skip to content

Instantly share code, notes, and snippets.

@fantom44ik
Created May 15, 2025 13:23
#pragma once
#include <iostream>
#include <windows.h>
#include <conio.h>
using namespace std;
enum GameObject : short { HALL, WALL, COIN, ENEMY };
enum Color : short {
BLACK, DARKBLUE, DARKGREEN, TURQUOISE, DARKRED,
PURPLE, DARKYELLOW, GREY, DARKGREY, BLUE, GREEN,
CYAN, RED, PINK, YELLOW, WHITE
};
enum Key : short {
LEFT = 75, RIGHT = 77, UP = 72, DOWN = 80,
ENTER = 13, SPACE = 32, ESCAPE = 27, BACKSPACE = 8
};
const int HEIGHT = 25;
const int WIDTH = 65;
void hideCursor(HANDLE h);
void generateMap(int map[HEIGHT][WIDTH]);
void drawMap(HANDLE h, int map[HEIGHT][WIDTH]);
void drawHero(HANDLE h, COORD hero);
void updateCoinsInfo(HANDLE h, int coins);
void hideCursor(HANDLE h) {
CONSOLE_CURSOR_INFO cursor;
cursor.bVisible = false;
cursor.dwSize = 100;
SetConsoleCursorInfo(h, &cursor);
}
void generateMap(int map[HEIGHT][WIDTH]) {
for (int y = 0; y < HEIGHT; y++)
{
for (int x = 0; x < WIDTH; x++)
{
map[y][x] = rand() % 4; // 0 1 2 3
// рамка по краях
if (x == 0 || y == 0 || x == WIDTH - 1 || y == HEIGHT - 1)
map[y][x] = GameObject::WALL;
// отвори для входу і виходу
if (x == 0 && y == 2 ||
x == 1 && y == 2 ||
x == 2 && y == 2 ||
x == WIDTH - 1 && y == HEIGHT - 3 ||
x == WIDTH - 2 && y == HEIGHT - 3 ||
x == WIDTH - 3 && y == HEIGHT - 3)
map[y][x] = GameObject::HALL;
if (map[y][x] == GameObject::ENEMY) {
int r = rand() % 10; // 0 1 2 3 4 5 6 7 8 9
if (r != 0) {
map[y][x] = GameObject::HALL;
}
}
}
}
}
void drawMap(HANDLE h, int map[HEIGHT][WIDTH]) {
for (int y = 0; y < HEIGHT; y++)
{
for (int x = 0; x < WIDTH; x++)
{
switch (map[y][x]) {
case GameObject::HALL: // 0
// як показати коридор
SetConsoleTextAttribute(h, Color::BLACK);
cout << " ";
break;
case GameObject::WALL: // 1
SetConsoleTextAttribute(h, Color::DARKGREEN);
// cout << "#";
cout << (char)178;
break;
case GameObject::COIN:
SetConsoleTextAttribute(h, Color::YELLOW);
cout << ".";
break;
case GameObject::ENEMY:
SetConsoleTextAttribute(h, Color::RED);
cout << "O";
break;
// case GameObject::SMTH_ELSE:
// як показати щось інше
// break;
}
}
cout << "\n";
}
}
void drawHero(HANDLE h, COORD hero) {
SetConsoleCursorPosition(h, hero);
SetConsoleTextAttribute(h, Color::BLUE);
cout << "O";
}
void updateCoinsInfo(HANDLE h, int coins) {
COORD coins_info;
coins_info.X = WIDTH + 1;
coins_info.Y = 0;
SetConsoleCursorPosition(h, coins_info);
SetConsoleTextAttribute(h, Color::DARKYELLOW);
cout << "COINS: ";
SetConsoleTextAttribute(h, Color::YELLOW);
cout << coins;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment