-
-
Save ViktorLychkatyi/293ca814ac9edf562a66af2e68d45974 to your computer and use it in GitHub Desktop.
laby
This file contains 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> | |
#include <conio.h> | |
using namespace std; | |
enum KeyCodes { ENTER = 13, ESCAPE = 27, LEFT = 75, RIGHT = 77, UP = 72, DOWN = 80, SPACEBAR = 32 }; | |
enum Colors { DARKGREEN = 2, RED = 12, YELLOW = 14, BLUE = 9, PINK = 4, WHITE = 5, MAGENTA = 13 }; | |
enum Objects { HALL, WALL, COIN, ENEMY, BOMB, BONUS_ENERGY, BONUS_HP, SECOND_ENEMY, THIRD_ENEMY }; | |
void generation_location(int HEIGHT, int WIDTH, int location[][50]); | |
void show_location(int HEIGHT, int WIDTH, int location[][50], HANDLE h); | |
void show_hero(HANDLE h, COORD position); | |
void clear_position(HANDLE h, COORD position); | |
int hero_coins(int location[][50], COORD position, int& coins); | |
void print_message_coin(int x, int y, int color, int message); | |
void print_message(int x, int y, int color, string message); | |
int hero_HP(int location[][50], COORD position, int& HP); | |
int object_BONUS_HP(int location[][50], COORD position, int& HP); | |
void print_message_HP(int x, int y, int color, int HP); | |
int hero_energy(int location[][50], COORD position, int& energy); | |
int object_BONUS_ENERGY(int location[][50], COORD position, int& energy); | |
void print_message_energy(int x, int y, int color, int energy); | |
void engine(int HEIGHT, int WIDTH, int location[][50], COORD& position, int& coins, int& HP); | |
void generation_location(int HEIGHT, int WIDTH, int location[][50]) { | |
// генерация локации | |
for (int y = 0; y < HEIGHT; y++) // перебор строк | |
{ | |
for (int x = 0; x < WIDTH; x++) // перебор столбцов | |
{ | |
// по дефолту пишется случайное число | |
location[y][x] = rand() % 9; // 0 1 2 3 | |
// стены по краям | |
if (x == 0 || y == 0 || x == WIDTH - 1 || y == HEIGHT - 1) | |
location[y][x] = WALL; | |
//int prob = rand() % 2; | |
// вход и выход | |
if (x == 0 && y == 2 || x == WIDTH - 1 && y == HEIGHT - 3) | |
location[y][x] = HALL; | |
if (x == 2 && y == 2) | |
location[y][x] = HALL; | |
if (location[y][x] == ENEMY) { | |
// вероятность того, останется враг или нет | |
int prob = rand() % 5; // 0-9 | |
if (prob != 0) // 1 2 3 4 5 6 7 8 9 | |
location[y][x] = HALL; | |
} | |
if (location[y][x] == SECOND_ENEMY) { | |
// вероятность того, останется враг или нет | |
int prob = rand() % 10; // 0-9 | |
if (prob != 0) // 1 2 3 4 5 6 7 8 9 | |
location[y][x] = HALL; | |
} | |
if (location[y][x] == THIRD_ENEMY) { | |
// вероятность того, останется враг или нет | |
int prob = rand() % 10; // 0-9 | |
if (prob != 0) // 1 2 3 4 5 6 7 8 9 | |
location[y][x] = HALL; | |
} | |
if (location[y][x] == COIN) { | |
int prob = rand() % 2; // 0-9 | |
if (prob != 0) // 1 2 3 4 5 6 7 8 9 | |
location[y][x] = HALL; | |
} | |
if (location[y][x] == BOMB) { | |
int prob = rand() % 5; // 0-9 | |
if (prob != 0) // 1 2 3 4 5 6 7 8 9 | |
location[y][x] = HALL; | |
} | |
if (location[y][x] == BONUS_ENERGY) { | |
int prob = rand() % 10; // 0-9 | |
if (prob != 0) // 1 2 3 4 5 6 7 8 9 | |
location[y][x] = HALL; | |
} | |
if (location[y][x] == BONUS_HP) { | |
int prob = rand() % 10; // 0-9 | |
if (prob != 0) // 1 2 3 4 5 6 7 8 9 | |
location[y][x] = HALL; | |
} | |
} | |
} | |
} | |
void show_location(int HEIGHT, int WIDTH, int location[][50], HANDLE h) { | |
for (int y = 0; y < HEIGHT; y++) | |
{ | |
for (int x = 0; x < WIDTH; x++) { | |
switch (location[y][x]) { | |
case HALL: | |
cout << " "; | |
break; | |
case WALL: | |
SetConsoleTextAttribute(h, DARKGREEN); | |
cout << (char)177; | |
break; | |
case COIN: | |
SetConsoleTextAttribute(h, YELLOW); | |
cout << (char)15; | |
break; | |
case ENEMY: | |
SetConsoleTextAttribute(h, RED); | |
cout << (char)2; | |
break; | |
case SECOND_ENEMY: | |
SetConsoleTextAttribute(h, RED); | |
cout << (char)5; | |
break; | |
case THIRD_ENEMY: | |
SetConsoleTextAttribute(h, RED); | |
cout << (char)6; | |
break; | |
case BOMB: | |
SetConsoleTextAttribute(h, MAGENTA); | |
cout << (char)253; | |
break; | |
case BONUS_ENERGY: | |
SetConsoleTextAttribute(h, YELLOW); | |
cout << (char)4; | |
break; | |
case BONUS_HP: | |
SetConsoleTextAttribute(h, PINK); | |
cout << (char)3; | |
break; | |
default: | |
cout << location[y][x]; | |
break; | |
} | |
} | |
cout << "\n"; | |
} | |
} | |
void show_hero(HANDLE h, COORD position) { | |
SetConsoleCursorPosition(h, position); | |
SetConsoleTextAttribute(h, BLUE); | |
cout << (char)1; | |
} | |
void clear_position(HANDLE h, COORD position) { | |
SetConsoleCursorPosition(h, position); | |
cout << " "; | |
} | |
int hero_coins(int location[][50], COORD position, int& coins) { | |
if (location[position.Y][position.X] == COIN) { | |
coins++; | |
location[position.Y][position.X] = HALL; | |
print_message_coin(1, 16, 14, coins); | |
} | |
return coins; | |
} | |
void print_message_coin(int x, int y, int color, int message) { | |
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE); | |
COORD position_text; | |
position_text.X = x; | |
position_text.Y = y; | |
SetConsoleCursorPosition(h, position_text); | |
SetConsoleTextAttribute(h, color); | |
cout << "Coins: " << message; | |
} | |
int hero_HP(int location[][50], COORD position, int& HP) { | |
if (location[position.Y][position.X] == ENEMY) { | |
location[position.Y][position.X] = HALL; | |
int damage = 20; | |
HP -= damage; | |
} | |
return HP; | |
} | |
int object_BONUS_HP(int location[][50], COORD position, int& HP) { | |
if (location[position.Y][position.X] == BONUS_HP) { | |
location[position.Y][position.X] = HALL; | |
if (HP < 100) { | |
int add_HP = 20; | |
HP += add_HP; | |
} | |
if (HP < 100) { | |
int HP = 100; | |
} | |
} | |
return HP; | |
} | |
void print_message_HP(int x, int y, int color, int HP) { | |
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE); | |
COORD position_text; | |
position_text.X = x; | |
position_text.Y = y; | |
SetConsoleCursorPosition(h, position_text); | |
SetConsoleTextAttribute(h, RED); | |
cout << "HP: " << HP << " "; | |
} | |
int hero_energy(int& energy) { | |
return energy; | |
} | |
int object_BONUS_ENERGY(int location[][50], COORD position, int& energy) { | |
if (location[position.Y][position.X] == BONUS_ENERGY) { | |
location[position.Y][position.X] = HALL; | |
int add_energy = 60; | |
energy += add_energy; | |
} | |
return energy; | |
} | |
void print_message_energy(int x, int y, int color, int energy) { | |
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE); | |
COORD position_text; | |
position_text.X = x; | |
position_text.Y = y; | |
SetConsoleCursorPosition(h, position_text); | |
SetConsoleTextAttribute(h, DARKGREEN); | |
cout << "Energy: " << energy << " "; | |
} | |
void engine(int HEIGHT, int WIDTH, int location[][50], COORD& position, int& coins, int& HP, int& energy) { | |
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE); | |
while (true) { | |
int code = _getch(); | |
if (code == 224) | |
code = _getch(); | |
clear_position(h, position); | |
switch (code) { | |
case ENTER: | |
break; | |
case SPACEBAR: | |
break; | |
case ESCAPE: | |
return; | |
case RIGHT: | |
if (position.X < WIDTH - 1 && location[position.Y][position.X + 1] != WALL) { | |
position.X++; | |
energy -= 10; | |
hero_energy(energy); | |
break; | |
} | |
if (position.X == WIDTH - 1 && position.Y == HEIGHT - 3) { | |
MessageBoxA(NULL, "Победа - найден выход!", "Победа!", MB_OK | MB_ICONINFORMATION); | |
_getch(); | |
return; | |
} | |
case LEFT: | |
if (position.X > 0 && location[position.Y][position.X - 1] != WALL) | |
position.X--; | |
energy -= 10; | |
hero_energy(energy); | |
break; | |
case UP: | |
if (position.Y > 0 && location[position.Y - 1][position.X] != WALL) | |
position.Y--; | |
energy -= 10; | |
hero_energy(energy); | |
break; | |
case DOWN: | |
if (position.Y < HEIGHT - 1 && location[position.Y + 1][position.X] != WALL) | |
position.Y++; | |
energy -= 10; | |
hero_energy(energy); | |
break; | |
default: | |
cout << code << "\n"; | |
break; | |
} | |
show_hero(h, position); | |
hero_HP(location, position, HP); | |
hero_coins(location, position, coins); | |
hero_energy(energy); | |
object_BONUS_ENERGY(location, position, energy); | |
object_BONUS_HP(location, position, HP); | |
print_message_HP(43, 16, 14, HP); | |
if (HP <= 0) { | |
MessageBoxA(NULL, "Вы погибли!", "Игра окончена", MB_OK | MB_ICONERROR); | |
return; | |
} | |
print_message_energy(20, 16, 14, energy); | |
if (energy <= 0) { | |
MessageBoxA(NULL, "У вас закончилась энергия!", "Игра окончена", MB_OK | MB_ICONERROR); | |
return; | |
} | |
} | |
} | |
void clear_line(int x, int y, int length) { | |
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE); | |
COORD position; | |
position.X = x; | |
position.Y = y; | |
SetConsoleCursorPosition(h, position); | |
for (int i = 0; i < length; ++i) { | |
cout << " "; | |
} | |
} | |
int main() { | |
system("color 1F"); | |
int coins = 0; | |
int HP = 100; | |
int energy = 500; | |
system("title Бомбер!"); | |
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE); | |
CONSOLE_CURSOR_INFO info; | |
info.bVisible = false; | |
info.dwSize = 100; | |
SetConsoleCursorInfo(h, &info); | |
const int WIDTH = 50; | |
const int HEIGHT = 15; | |
int location[HEIGHT][WIDTH] = {}; | |
COORD position; | |
position.X = 1; | |
position.Y = 2; | |
generation_location(HEIGHT, WIDTH, location); | |
show_location(HEIGHT, WIDTH, location, h); | |
show_hero(h, position); | |
print_message_coin(1, 16, 14, coins); | |
print_message_HP(43, 16, 14, HP); | |
print_message_energy(20, 16, 14, energy); | |
engine(HEIGHT, WIDTH, location, position, coins, HP, energy); | |
Sleep(INFINITE); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment