Created
April 12, 2025 04:25
-
-
Save Helen460/7e9a8916e2a232b93eb924cc9e2c2d2f to your computer and use it in GitHub Desktop.
7DZ
This file contains hidden or 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 <iostream> | |
| #include <windows.h> // COORD, HANDLE, SetConsoleTextAttribute, SetConsoleCursorPosition | |
| #include <conio.h> // _getch, _kbhit | |
| #include <string> | |
| using namespace std; | |
| // ENUMS | |
| enum GameObject : short { HALL, WALL, COIN, ENEMY, MEDKIT }; | |
| 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 | |
| }; | |
| // CONSTANTS | |
| const int HEIGHT = 25; | |
| const int WIDTH = 65; | |
| const int MAX_HP = 100; | |
| // GLOBAL HANDLE | |
| HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE); | |
| //////////////////////////////////////////////////////////// | |
| // 1. Вивід тексту з кольором і позицією | |
| void show_text(string text = "TEXT", int color = 7, short x = -1, short y = -1) { | |
| if (x != -1 && y != -1) | |
| SetConsoleCursorPosition(h, { x, y }); | |
| SetConsoleTextAttribute(h, color); | |
| cout << text; | |
| } | |
| //////////////////////////////////////////////////////////// | |
| // 2. Генерація карти | |
| void fill_map(int map[][WIDTH], const int height, const int width) { | |
| for (int y = 0; y < height; y++) { | |
| for (int x = 0; x < width; x++) { | |
| map[y][x] = rand() % 5; // HALL, WALL, COIN, ENEMY, MEDKIT | |
| // стіни по периметру | |
| if (x == 0 || y == 0 || x == width - 1 || y == height - 1) | |
| map[y][x] = 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] = HALL; | |
| // менше ворогів | |
| if (map[y][x] == ENEMY && rand() % 10 != 0) | |
| map[y][x] = HALL; | |
| } | |
| } | |
| } | |
| //////////////////////////////////////////////////////////// | |
| // 3. Малювання карти | |
| void draw_map(int map[][WIDTH], const int height, const int width) { | |
| for (int y = 0; y < height; y++) { | |
| for (int x = 0; x < width; x++) { | |
| switch (map[y][x]) { | |
| case HALL: | |
| show_text(" ", BLACK); | |
| break; | |
| case WALL: | |
| show_text((char)178 + string(""), DARKGREEN); | |
| break; | |
| case COIN: | |
| show_text(".", YELLOW); | |
| break; | |
| case ENEMY: | |
| show_text("O", RED); | |
| break; | |
| case MEDKIT: | |
| show_text("+", CYAN); | |
| break; | |
| } | |
| } | |
| cout << "\n"; | |
| } | |
| } | |
| //////////////////////////////////////////////////////////// | |
| // 4. Показ ГГ | |
| void show_hero(COORD pos, int color = BLUE) { | |
| SetConsoleCursorPosition(h, pos); | |
| SetConsoleTextAttribute(h, color); | |
| cout << "O"; | |
| } | |
| //////////////////////////////////////////////////////////// | |
| // 5. Панель інформації | |
| void update_info(int coins, int hp) { | |
| show_text("COINS: ", DARKYELLOW, WIDTH + 2, 0); | |
| show_text(to_string(coins), YELLOW); | |
| show_text("HP: ", DARKRED, WIDTH + 2, 1); | |
| show_text(to_string(hp), RED); | |
| } | |
| //////////////////////////////////////////////////////////// | |
| // 6. Рух персонажа | |
| bool move_hero(COORD& hero, int map[][WIDTH], int& coins, int& hp, int& total_coins) { | |
| if (_kbhit()) { | |
| int code = _getch(); | |
| if (code == 224) code = _getch(); | |
| COORD old = hero; | |
| bool moved = false; | |
| switch (code) { | |
| case LEFT: | |
| if (hero.X > 0 && map[hero.Y][hero.X - 1] != WALL) { | |
| hero.X--; | |
| moved = true; | |
| } | |
| break; | |
| case RIGHT: | |
| if (map[hero.Y][hero.X + 1] != WALL) { | |
| hero.X++; | |
| moved = true; | |
| } | |
| break; | |
| case UP: | |
| if (map[hero.Y - 1][hero.X] != WALL) { | |
| hero.Y--; | |
| moved = true; | |
| } | |
| break; | |
| case DOWN: | |
| if (map[hero.Y + 1][hero.X] != WALL) { | |
| hero.Y++; | |
| moved = true; | |
| } | |
| break; | |
| } | |
| // перемальовуємо ГГ | |
| if (moved) { | |
| SetConsoleCursorPosition(h, old); | |
| show_text(" ", BLACK); | |
| show_hero(hero); | |
| } | |
| int& cell = map[hero.Y][hero.X]; | |
| // Збір монет | |
| if (cell == COIN) { | |
| coins++; | |
| total_coins--; | |
| cell = HALL; | |
| update_info(coins, hp); | |
| } | |
| // Аптечка | |
| if (cell == MEDKIT) { | |
| if (hp < MAX_HP) { | |
| hp = min(MAX_HP, hp + 25); | |
| cell = HALL; | |
| update_info(coins, hp); | |
| } | |
| } | |
| // Ворог | |
| if (cell == ENEMY) { | |
| hp -= 20; | |
| update_info(coins, hp); | |
| if (hp <= 0) { | |
| show_text("ПОРАЗКА – ЗДОРОВ’Я ЗАКІНЧИЛОСЯ", RED, 10, HEIGHT / 2); | |
| return false; | |
| } | |
| } | |
| // Вихід | |
| if (hero.X == WIDTH - 3 && hero.Y == HEIGHT - 3) { | |
| show_text("ПЕРЕМОГА – ЗНАЙДЕНО ВИХІД", GREEN, 10, HEIGHT / 2); | |
| return false; | |
| } | |
| // Всі монети зібрані | |
| if (total_coins == 0) { | |
| show_text("ПЕРЕМОГА – МОНЕТИ ЗІБРАНО", GREEN, 10, HEIGHT / 2); | |
| return false; | |
| } | |
| } | |
| return true; | |
| } | |
| //////////////////////////////////////////////////////////// | |
| // MAIN | |
| int main() { | |
| srand(time(0)); | |
| system("title Bomberman"); | |
| // Приховати курсор | |
| CONSOLE_CURSOR_INFO cursor; | |
| cursor.bVisible = false; | |
| cursor.dwSize = 100; | |
| SetConsoleCursorInfo(h, &cursor); | |
| // Створення карти | |
| int map[HEIGHT][WIDTH] = {}; | |
| fill_map(map, HEIGHT, WIDTH); | |
| draw_map(map, HEIGHT, WIDTH); | |
| int coins_collected = 0; | |
| int hp = MAX_HP; | |
| int total_coins = 0; | |
| for (int y = 0; y < HEIGHT; y++) | |
| for (int x = 0; x < WIDTH; x++) | |
| if (map[y][x] == COIN) | |
| total_coins++; | |
| // Герой | |
| COORD hero = { 0, 2 }; | |
| show_hero(hero); | |
| update_info(coins_collected, hp); | |
| // Гра | |
| while (true) { | |
| if (!move_hero(hero, map, coins_collected, hp, total_coins)) | |
| break; | |
| Sleep(50); | |
| } | |
| // Завершення | |
| SetConsoleTextAttribute(h, WHITE); | |
| SetConsoleCursorPosition(h, { 0, HEIGHT + 1 }); | |
| system("pause"); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment