Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Denkotleta221/0122b94304beb1c30b025429667d72fb to your computer and use it in GitHub Desktop.
Save Denkotleta221/0122b94304beb1c30b025429667d72fb to your computer and use it in GitHub Desktop.
#include <iostream>
#include <windows.h> // COORD HANDLE SetConsoleTextAttribute SetConsoleCursorPosition
#include <conio.h> // _getch
using namespace std;
enum GameObject : short { HALL, WALL, COIN, ENEMY };
enum Key : short {
LEFT = 75, RIGHT = 77, UP = 72, DOWN = 80,
ENTER = 13, SPACE = 32, ESCAPE = 27, BACKSPACE = 8
};
enum Color : short {
BLACK, DARKBLUE, DARKGREEN, TURQUOISE, DARKRED,
PURPLE, DARKYELLOW, GREY, DARKGREY, BLUE, GREEN,
CYAN, RED, PINK, YELLOW, WHITE
};
// 1)
void show_color(Color color) {
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(h, color);
}
// 2)
void cursor_position(COORD coord) {
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(h, coord);
}
// 3)
void model(int height, int width, int array[][65]) {
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
array[y][x] = rand() % 4; // 0 1 2 3
// рамка по краях
if (x == 0 || y == 0 || x == width - 1 || y == height- 1)
array[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)
array[y][x] = GameObject::HALL;
if (array[y][x] == GameObject::ENEMY) {
int r = rand() % 10; // 0 1 2 3 4 5 6 7 8 9
if (r != 0) {
array[y][x] = GameObject::HALL;
}
}
}
}
}
// 4)
void view(int height, int width, int array[][65]) {
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
switch (array[y][x]) {
case GameObject::HALL: // 0
// як показати коридор
show_color(BLACK);
cout << " ";
break;
case GameObject::WALL: // 1
show_color(DARKGREEN);
// cout << "#";
cout << (char)178;
break;
case GameObject::COIN:
show_color(YELLOW);
cout << ".";
break;
case GameObject::ENEMY:
show_color(RED);
cout << "O";
break;
// case GameObject::SMTH_ELSE:
// як показати щось інше
// break;
}
}
cout << "\n";
}
}
// 5)
void game(int width, int array[][65]) {
COORD hero;
hero.X = 0;
hero.Y = 2;
cursor_position(hero);
show_color(BLUE);;
cout << "O";
int coins_collected = 0;
COORD coins_info;
coins_info.X = width + 1;
coins_info.Y = 0;
cursor_position(coins_info);
show_color(DARKYELLOW);
cout << "COINS: ";
show_color(YELLOW);
cout << coins_collected;
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 && array[hero.Y][hero.X - 1] != GameObject::WALL) {
has_been_moved = true;
hero.X--;
}
break;
case Key::RIGHT:
if (array[hero.Y][hero.X + 1] != GameObject::WALL) {
has_been_moved = true;
hero.X++;
}
break;
case Key::UP:
if (array[hero.Y - 1][hero.X] != GameObject::WALL) {
has_been_moved = true;
hero.Y--;
}
break;
case Key::DOWN:
if (array[hero.Y + 1][hero.X] != GameObject::WALL) {
has_been_moved = true;
hero.Y++;
}
break;
}
if (has_been_moved) {
// стирання ГГ в "старих" координатах
cursor_position(old_position);
show_color(BLACK);
cout << " ";
// перемальовка ГГ в нових координатах
cursor_position(hero);
show_color(BLUE);
cout << "O";
}
// приклад перевірки на перетинання з якимось об'єктом в лабірінті
if (array[hero.Y][hero.X] == GameObject::COIN) {
// cout << "COIN";
coins_collected++;
array[hero.Y][hero.X] = GameObject::HALL;
COORD coins_info;
coins_info.X = width + 1;
coins_info.Y = 0;
cursor_position(coins_info);
show_color(DARKYELLOW);
cout << "COINS: ";
show_color(YELLOW);
cout << coins_collected;
}
if (array[hero.Y][hero.X] == GameObject::ENEMY) {
cout << "ENEMY";
}
}
}
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 (логіка заповнення масиву)
model(HEIGHT, WIDTH, map);
// VIEW (подання - як буде виглядати ігрова локація)
view(HEIGHT, WIDTH, map);
// показ ГГ
game(WIDTH, map);
Sleep(INFINITE);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment