Created
May 7, 2025 13:55
-
-
Save fantom44ik/a0f57d738de12ff2698119cc03f106b2 to your computer and use it in GitHub Desktop.
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 | |
using namespace std; | |
enum GameObject : short { HALL, WALL, COIN, ENEMY, HEALTH_SUPPLY, EXIT }; | |
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 | |
}; | |
int main() { | |
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE); | |
srand(time(0)); | |
rand(); | |
system("title Bomberman"); | |
// cursor hide | |
CONSOLE_CURSOR_INFO cursor; | |
cursor.bVisible = false; | |
cursor.dwSize = 100; // 1-100 | |
SetConsoleCursorInfo(h, &cursor); | |
const int HEIGHT = 25; | |
const int WIDTH = 65; | |
int map[HEIGHT][WIDTH] = {}; | |
// ASCII Table | |
//for (int i = 0; i < 256; i++) | |
//{ | |
// cout << (char)i << " - " << i << "\n"; | |
//} | |
// MODEL (логіка заповнення масиву) | |
for (int y = 0; y < HEIGHT; y++) | |
{ | |
for (int x = 0; x < WIDTH; x++) | |
{ | |
map[y][x] = rand() % 5; // 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 (x == WIDTH - 1 && y == HEIGHT - 3) | |
map[y][x] = GameObject::EXIT; | |
if (map[y][x] == GameObject::HEALTH_SUPPLY) { | |
int r = rand() % 15; // 0 1 2 3 4 5 6 7 8 9 | |
if (r != 0) { | |
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; | |
} | |
} | |
} | |
} | |
/////////////////////////////////////////////////////////// | |
// VIEW (подання - як буде виглядати ігрова локація) | |
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::EXIT: | |
SetConsoleTextAttribute(h, Color::PINK); | |
cout << "#"; | |
break; | |
case GameObject::HEALTH_SUPPLY: | |
SetConsoleTextAttribute(h, Color::GREEN); | |
cout << "+"; | |
break; | |
// case GameObject::SMTH_ELSE: | |
// як показати щось інше | |
// break; | |
} | |
} | |
cout << "\n"; | |
} | |
//////////////////////////////////////////////////////////// | |
// показ ГГ | |
COORD hero; | |
hero.X = 0; | |
hero.Y = 2; | |
SetConsoleCursorPosition(h, hero); | |
SetConsoleTextAttribute(h, Color::BLUE); | |
cout << "O"; | |
int coins_collected = 0; | |
int health_points = 100; | |
COORD health; | |
health.X = WIDTH + 1; | |
health.Y = 0; | |
SetConsoleCursorPosition(h, health); | |
SetConsoleTextAttribute(h, Color::GREEN); | |
cout << "HEALTH: "; | |
cout << " "; | |
SetConsoleCursorPosition(h, health); | |
SetConsoleTextAttribute(h, Color::DARKGREEN); | |
cout << "HEALTH: " << health_points; | |
COORD coins_info; | |
coins_info.X = WIDTH + 1; | |
coins_info.Y = 1; | |
SetConsoleCursorPosition(h, coins_info); | |
SetConsoleTextAttribute(h, Color::DARKYELLOW); | |
cout << "COINS: "; | |
SetConsoleTextAttribute(h, Color::YELLOW); | |
cout << coins_collected; | |
COORD win_condition; | |
win_condition.X = WIDTH + 1; | |
win_condition.Y = 3; | |
SetConsoleCursorPosition(h, win_condition); | |
SetConsoleTextAttribute(h, Color::CYAN); | |
cout << "WIN CONDITIONS: FIND EXIT, COLLECT 165 COINS"; | |
// ігровий двигунчик | |
while (true) { | |
int code = _getch(); // get character | |
if (code == 224) { | |
code = _getch(); | |
} | |
COORD old_position = hero; | |
bool has_been_moved = false; // false - пересування не було | |
// cout << code << "\n"; | |
switch (code) { | |
case Key::LEFT: | |
if (hero.X > 0 && map[hero.Y][hero.X - 1] != GameObject::WALL) { | |
has_been_moved = true; | |
hero.X--; | |
} | |
break; | |
case Key::RIGHT: | |
if (map[hero.Y][hero.X + 1] != GameObject::WALL) { | |
has_been_moved = true; | |
hero.X++; | |
} | |
break; | |
case Key::UP: | |
if (map[hero.Y - 1][hero.X] != GameObject::WALL) { | |
has_been_moved = true; | |
hero.Y--; | |
} | |
break; | |
case Key::DOWN: | |
if (map[hero.Y + 1][hero.X] != GameObject::WALL) { | |
has_been_moved = true; | |
hero.Y++; | |
} | |
break; | |
} | |
if (has_been_moved) { | |
// стирання ГГ в "старих" координатах | |
SetConsoleCursorPosition(h, old_position); | |
SetConsoleTextAttribute(h, Color::BLACK); | |
cout << " "; | |
// перемальовка ГГ в нових координатах | |
SetConsoleCursorPosition(h, hero); | |
SetConsoleTextAttribute(h, Color::BLUE); | |
cout << "O"; | |
} | |
// приклад перевірки на перетинання з якимось об'єктом в лабірінті | |
if (map[hero.Y][hero.X] == GameObject::COIN) { | |
// cout << "COIN"; | |
coins_collected++; | |
map[hero.Y][hero.X] = GameObject::HALL; | |
COORD coins_info; | |
coins_info.X = WIDTH + 1; | |
coins_info.Y = 1; | |
SetConsoleCursorPosition(h, coins_info); | |
SetConsoleTextAttribute(h, Color::DARKYELLOW); | |
cout << "COINS: "; | |
SetConsoleTextAttribute(h, Color::YELLOW); | |
cout << coins_collected; | |
} | |
if (map[hero.Y][hero.X] == GameObject::ENEMY) { | |
health_points -= 25; | |
map[hero.Y][hero.X] = GameObject::HALL; | |
COORD health; | |
health.X = WIDTH + 1; | |
health.Y = 0; | |
SetConsoleCursorPosition(h, health); | |
SetConsoleTextAttribute(h, Color::GREEN); | |
cout << "HEALTH: "; | |
cout << " "; | |
SetConsoleCursorPosition(h, health); | |
SetConsoleTextAttribute(h, Color::DARKGREEN); | |
cout << "HEALTH: " << health_points; | |
} | |
if (map[hero.Y][hero.X] == GameObject::HEALTH_SUPPLY && health_points < 100) { | |
health_points += 25; | |
map[hero.Y][hero.X] = GameObject::HALL; | |
COORD health; | |
health.X = WIDTH + 1; | |
health.Y = 0; | |
SetConsoleCursorPosition(h, health); | |
SetConsoleTextAttribute(h, Color::GREEN); | |
cout << "HEALTH: "; | |
cout << " "; | |
SetConsoleCursorPosition(h, health); | |
SetConsoleTextAttribute(h, Color::DARKGREEN); | |
cout << "HEALTH: " << health_points; | |
} | |
if (map[hero.Y][hero.X] == GameObject::EXIT) { | |
system("cls"); | |
SetConsoleCursorPosition(h, { WIDTH / 3, HEIGHT / 2 }); | |
SetConsoleTextAttribute(h, Color::GREEN); | |
cout << "Wictory - you have found the exit!"; | |
Sleep(3000); | |
} | |
if (coins_collected >= 165) { | |
system("cls"); | |
SetConsoleCursorPosition(h, { WIDTH / 3, HEIGHT / 2 }); | |
SetConsoleTextAttribute(h, Color::GREEN); | |
cout << "Wictory - you have collected all coins!"; | |
Sleep(3000); | |
} | |
if (health_points <= 0) { | |
system("cls"); | |
SetConsoleCursorPosition(h, { WIDTH / 3, HEIGHT / 2 }); | |
SetConsoleTextAttribute(h, Color::RED); | |
cout << "Game Over - you have lost all your health!"; | |
Sleep(3000); | |
} | |
} | |
Sleep(INFINITE); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment