Files
Uni_Stuff/n1.cpp
T

965 lines
29 KiB
C++

#include <iostream>
#include <fstream>
#include <vector>
#include <conio.h> //getch()
#include <windows.h> //Sleep()
#include <cstdlib> //rand()
#include <ctime> //time()
#include <cmath> //abs()
#include <string>
using namespace std;
#define RESET "\033[0m"
#define BOLD "\033[1m"
#define LAPIS_LAZULI "\033[38;5;20m"
#define YELLOW_GOLD "\033[38;5;220m"
#define ORANGE_PUMPKIN "\033[38;5;202m"
#define BRIGHT_BLUE "\033[94m"
#define NEON_PURPLE "\033[38;5;201m"
#define RED "\033[31m"
#define DARK_GREEN_EMERALD "\033[38;5;29m"
#define GRAY_DARK "\033[38;5;238m"
#define GREEN "\033[32m"
#define BLUE "\033[34m"
#define DARK_BROWN "\033[38;5;52m"
#define ELECTRIC_PURPLE "\033[38;5;93m"
#define YELLOW_VIVID "\033[38;5;226m"
#define BOX_TOP_LEFT "┌"
#define BOX_TOP_RIGHT "┐"
#define BOX_BOTTOM_LEFT "└"
#define BOX_BOTTOM_RIGHT "┘"
#define BOX_HORIZONTAL "─"
#define BOX_VERTICAL "│"
struct game_data {
string player;
string date;
double time;
int scores, lives;
string result;
};
struct stored_g_data {
int t_score;
string data;
};
struct xy {
int x;
int y;
};
struct brick {
int x, y;
char character;
};
vector<brick> bricks;
vector<xy> X_h_bricks; //collided X bricks
const int max_bricks = 41*7;
int initial_num_of_X_bricks = 0;
int initial_num_of_lives = 6;
const int hight = 28, width = 43;
char c_page[hight][width];
int paddle_width = 0;
const int paddle_w[11] = {-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5};
xy ball_pos = {0, 0}, ball_v = {0, 0};
xy paddle = {21, 26};
const xy speed[10] = {{0, 1}, {1, 5}, {1, 4}, {1, 3}, {1, 2}, {1, 1}, {2, 1}, {3, 1}, {4, 1}, {5, 1}};
int max_scores;
char brick_1_c = '#';
char brick_2_c = 'X';
char lr_wall_c = '|';
char u_wall_c = '_';
char corner_wall_c = '.';
char d_wall_c = '~';
char paddle_c = '=';
char ball_c = 'O';
char empty_space_c = ' ';
string file_name = "game_history";
//theme colors
string down_wall_co = "0";
string wall_co = "0";
string ball_co = "0";
string paddle_co = "0";
string X_brick_co = "0";
string brick_co = "0";
const string general_co_1 = ELECTRIC_PURPLE;
const string general_co_2 = YELLOW_VIVID;
void hide_cursor() {
HANDLE console_handle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO info;
info.dwSize = 100;
info.bVisible = FALSE;
SetConsoleCursorInfo(console_handle, &info);
}
void print_left_padding(int line_length) {
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
int screen_width = csbi.srWindow.Right - csbi.srWindow.Left + 1;
int left_padding = (screen_width - line_length) / 2;
if (left_padding < 0) left_padding = 0;
for (int i = 0; i < left_padding; i++) {
cout << ' ';
}
}
void print_centered_and_colored_box(const string& text, const string& text_color, const string& box_color, int padding = 1) {
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
int length = csbi.srWindow.Right - csbi.srWindow.Left + 1;
int width = text.length() + (padding * 2) + 2;
int left_padding = (length - width) / 2;
if (left_padding < 0) left_padding = 0;
/////////////////////////////////////
for (int i = 0; i < left_padding; i++) {
cout << ' ';
}
cout << box_color << BOX_TOP_LEFT;
for (int i = 0; i < width - 2; i++) {
cout << BOX_HORIZONTAL;
}
cout << BOX_TOP_RIGHT << RESET << endl;
/////////////////////////////////////
for (int i = 0; i < left_padding; i++) {
cout << ' ';
}
cout << box_color << BOX_VERTICAL << RESET;
for (int i = 0; i < padding; i++) cout << " ";
cout << text_color << BOLD << text << RESET;
for (int i = 0; i < padding; i++) cout << " ";
cout << box_color << BOX_VERTICAL << RESET << endl;
/////////////////////////////////////
for (int i = 0; i < left_padding; i++) {
cout << ' ';
}
cout << box_color << BOX_BOTTOM_LEFT;
for (int i = 0; i < width - 2; i++) {
cout << BOX_HORIZONTAL;
}
cout << BOX_BOTTOM_RIGHT << RESET << endl;
}
int menu_display(vector<string> list) {
char c;
int selection_num = 1;
int length = static_cast<int>(list.size());
while (true) {
for (int i = 1; i <= length; i++) {
const string each_option = list[i - 1];
if (selection_num == i) print_centered_and_colored_box(each_option, general_co_2, general_co_2);
else print_centered_and_colored_box(each_option, general_co_1, general_co_1);
}
c = getch();
char ch = c;
if ((ch == 13)) {
system("cls");
return selection_num;
}
else if (ch == 'w') selection_num--;
else if (ch == 's') selection_num++;
if (selection_num < 1) selection_num = 1;
else if (selection_num > length) selection_num = length;
system("cls");
}
}
void initial_bricks_vector(const int X_bricks_i_num) {
srand(time(nullptr));
int Xs[X_bricks_i_num];
int min_value = 0, max_value = max_bricks - 1;
for (int i = 0; i < X_bricks_i_num; i++) {
Xs[i] = min_value + (rand() % (max_value - min_value + 1));
}
int bricks_vector_index = 0;
for (int i = 1; i <= 7; i++) {
for (int j = 1; j <= 41; j++) {
bool is_it_X = false;
for(int k = 0; k < X_bricks_i_num; k++) {
if (bricks_vector_index == Xs[k]) {
is_it_X = true;
break;
}
}
brick a_brick;
if (is_it_X) a_brick = {j, i, brick_2_c};
else a_brick = {j, i, brick_1_c};
bricks.push_back(a_brick);
bricks_vector_index++;
}
}
}
void render() {
for (int i = 0; i < hight; i++) {
for (int j = 0; j < width; j++) {
bool TF = false;
int s;
if (paddle_width == 9) s = 1;
else if (paddle_width == 7) s = 2;
else if (paddle_width == 5) s = 3;
for (int h = s; h < (s + paddle_width); h++) { //paddle
if ((i == paddle.y)&&(j == (paddle.x + paddle_w[h]))) {
c_page[i][j] = paddle_c;
TF = true;
break;
}
}
if (TF == true) continue;
for (size_t k = 0; k < bricks.size(); k++) { //bricks
if ((j == bricks.at(k).x)&&(i == bricks.at(k).y)) {
if (bricks.at(k).character == brick_1_c) c_page[i][j] = brick_1_c;
else c_page[i][j] = brick_2_c;
TF = true;
break;
}
}
if (TF == true) continue;
if ((i == ball_pos.y)&&(j == ball_pos.x)) c_page[i][j] = ball_c; //ball
else if (((i == 0)&&(j == 0))||((i == 0)&&(j == (width - 1)))) c_page[i][j] = corner_wall_c; //corner walls
else if (i == 0) c_page[i][j] = u_wall_c; //up walls
else if ((j == 0)||(j == (width - 1))) c_page[i][j] = lr_wall_c;//left & right walls
else if (i == (hight - 1)) c_page[i][j] = d_wall_c; //down walls
else c_page[i][j] = empty_space_c; //empty space
}
}
}
void display(game_data &game) {
print_left_padding(width);
for (int i = 0; i < game.lives; i++) {
cout << RED << "♥";
}
cout << RESET << " " << game.scores << " " << bricks.size() << endl;
for (int i = 0; i < hight; i++) {
print_left_padding(width);
for (int j = 0; j < width; j++) {
if (c_page[i][j] != empty_space_c) {
string c_color = "";
if ((c_page[i][j] == u_wall_c)||(c_page[i][j] == lr_wall_c)||(c_page[i][j] == corner_wall_c)) c_color = wall_co;
else if (c_page[i][j] == brick_1_c) c_color = brick_co;
else if (c_page[i][j] == brick_2_c) c_color = X_brick_co;
else if (c_page[i][j] == paddle_c) c_color = paddle_co;
else if (c_page[i][j] == ball_c) c_color = ball_co;
else if (c_page[i][j] == d_wall_c) c_color = down_wall_co;
cout << c_color << c_page[i][j] << RESET;
}
else cout << c_page[i][j];
}
cout << endl;
}
}
char input() {
if (kbhit()) {
char c = getch();
if ((c == 'a')||(c == 'd')||(c == ' ')) return c;
else return '-';
}
return '-';
}
void paddle_position_update(char a_or_d) {
if (a_or_d == 'a') paddle.x -= 2;
else paddle.x += 2;
int d;
if (paddle_width == 9) d = 5;
else if (paddle_width == 7) d = 4;
else if (paddle_width == 5) d = 3;
if (paddle.x < d) paddle.x = d;
else if (paddle.x > (width - d)) paddle.x = width - d;
}
void abs_v_alteration(int n) {
int vx_sign;
if (ball_v.x != 0) vx_sign = ball_v.x /abs(ball_v.x);
else vx_sign = 1;
int vy_sign = ball_v.y/abs(ball_v.y);
int v_index;
for (int i = 0; i < 10; i++) {
if ((abs(ball_v.x) == speed[i].x)&&(abs(ball_v.y) == speed[i].y)) v_index = i;
}
v_index += n;
if (v_index < 0) v_index = 0;
else if (v_index >= 10) v_index = 9;
ball_v.x = speed[v_index].x * vx_sign;
ball_v.y = speed[v_index].y * vy_sign;
}
void ball_position_update(game_data &game, brick list[3]) {
int a, b, c, d, e;
if (abs(ball_v.y) == 1) {
a = ball_pos.y;
c = ball_pos.x;
e = abs(ball_v.x);
if (ball_v.x > 0) d = 1;
else if (ball_v.x < 0) d = -1;
if (ball_v.y > 0) b = 1;
else if (ball_v.y < 0) b = -1;
if (c_page[a + b][c] == empty_space_c) {
int f = 1;
while (f < 6) {
if ((c_page[a][c + f * d] != empty_space_c)||(c_page[a + b][c + f * d] != empty_space_c)) {
if (f != 1) {
ball_pos.x += (f - 1) * d;
ball_pos.y += b;
}
break;
}
else {
if (f == e) {
ball_pos.y += ball_v.y;
ball_pos.x += ball_v.x;
break;
}
}
f++;
}
}
}
else if (abs(ball_v.x) == 1) {
a = ball_pos.x;
c = ball_pos.y;
e = abs(ball_v.y);
if (ball_v.x > 0) b = 1;
else if (ball_v.x < 0) b = -1;
if (ball_v.y > 0) d = 1;
else if (ball_v.y < 0) d = -1;
if (c_page[c][a + b] == empty_space_c) {
int f = 1;
while (f < 6) {
if ((c_page[c + f * d][a] != empty_space_c)||(c_page[c + f * d][a + b] != empty_space_c)) {
if (f != 1) {
ball_pos.y += (f - 1) * d;
ball_pos.x += b;
}
break;
}
else {
if (f == e) {
ball_pos.x += b;
ball_pos.y += f * d;
break;
}
}
f++;
}
}
}
brick collided_brick_1 = {0, 0, '0'}, collided_brick_2 = {0, 0, '0'}, collided_brick_3 = {0, 0, '0'};
char Right = c_page[ball_pos.y][ball_pos.x + 1];
char Left = c_page[ball_pos.y][ball_pos.x - 1];
char Up = c_page[ball_pos.y - 1][ball_pos.x];
char Down = c_page[ball_pos.y + 1][ball_pos.x];
char R_U = c_page[ball_pos.y - 1][ball_pos.x + 1];
char L_U = c_page[ball_pos.y - 1][ball_pos.x - 1];
char R_D = c_page[ball_pos.y + 1][ball_pos.x + 1];
char L_D = c_page[ball_pos.y + 1][ball_pos.x - 1];
if ((ball_v.x > 0)&&(ball_v.y > 0)) {
if (Right == lr_wall_c) ball_v.x *= -1;
else if ((Right == brick_1_c)||(Right == brick_2_c)) {
ball_v.x *= -1;
collided_brick_1 = {ball_pos.x + 1, ball_pos.y, Right};
}
if (Down == d_wall_c) {
game.lives--;//losing 1 life if the ball hits the the ~ line
}
else if ((Down == brick_1_c)||(Down == brick_2_c)) {
ball_v.y *= -1;
collided_brick_2 = {ball_pos.x, ball_pos.y + 1, Down};
}
if ((Down == empty_space_c)&&(Right == empty_space_c)&&((R_D == brick_1_c)||(R_D == brick_2_c))) {
ball_v.x *= -1;
ball_v.y *= -1;
collided_brick_3 = {ball_pos.x + 1, ball_pos.y + 1, R_D};
}
int s;
if (paddle_width == 9) s = 0;
else if (paddle_width == 7) s = 1;
else if (paddle_width == 5) s = 2;
for (int h = s; h < s + paddle_width; h++) {
if (((ball_pos.y + 1) == paddle.y)&&(ball_pos.x == (paddle.x + paddle_w[h]))) {
ball_v.y *= -1;
abs_v_alteration(paddle_w[h]);
}
}
}
else if ((ball_v.x < 0)&&(ball_v.y > 0)) {
if (Left == lr_wall_c) ball_v.x *= -1;
else if ((Left == brick_1_c)||(Left == brick_2_c)) {
ball_v.x *= -1;
collided_brick_1 = {ball_pos.x - 1, ball_pos.y, Left};
}
if (Down == d_wall_c) {
game.lives--; //losing 1 life if the ball hits the the ~ line
}
else if ((Down == brick_1_c)||(Down == brick_2_c)) {
ball_v.y *= -1;
collided_brick_2 = {ball_pos.x, ball_pos.y + 1, Down};
}
if ((Down == empty_space_c)&&(Left == empty_space_c)&&((L_D == brick_1_c)||(L_D == brick_2_c))) {
ball_v.x *= -1;
ball_v.y *= -1;
collided_brick_3 = {ball_pos.x - 1, ball_pos.y + 1, L_D};
}
int s;
if (paddle_width == 9) s = 0;
else if (paddle_width == 7) s = 1;
else if (paddle_width == 5) s = 2;
for (int h = s; h < s + paddle_width; h++) {
if (((ball_pos.y + 1) == paddle.y)&&(ball_pos.x == (paddle.x + paddle_w[h]))) {
ball_v.y *= -1;
abs_v_alteration(-(paddle_w[h]));
}
}
}
else if ((ball_v.x > 0)&&(ball_v.y < 0)) {
if (Right == lr_wall_c) ball_v.x *= -1;
else if ((Right == brick_1_c)||(Right == brick_2_c)) {
ball_v.x *= -1;
collided_brick_1 = {ball_pos.x + 1, ball_pos.y, Right};
}
if (Up == d_wall_c) ball_v.y *= -1;
else if ((Up == brick_1_c)||(Up == brick_2_c)) {
ball_v.y *= -1;
collided_brick_2 = {ball_pos.x, ball_pos.y - 1, Up};
}
if ((Up == empty_space_c)&&(Right == empty_space_c)&&((R_U == brick_1_c)||(R_U == brick_2_c))) {
ball_v.x *= -1;
ball_v.y *= -1;
collided_brick_3 = {ball_pos.x + 1, ball_pos.y - 1, R_U};
}
}
else if ((ball_v.x < 0)&&(ball_v.y < 0)) {
if (Left == lr_wall_c) ball_v.x *= -1;
else if ((Left == brick_1_c)||(Left == brick_2_c)) {
ball_v.x *= -1;
collided_brick_1 = {ball_pos.x - 1, ball_pos.y, Left};
}
if (Up == d_wall_c) ball_v.y *= -1;
else if ((Up == brick_1_c)||(Up == brick_2_c)) {
ball_v.y *= -1;
collided_brick_2 = {ball_pos.x, ball_pos.y - 1, Up};
}
if ((Up == empty_space_c)&&(Left == empty_space_c)&&((L_U == brick_1_c)||(L_U == brick_2_c))) {
ball_v.x *= -1;
ball_v.y *= -1;
collided_brick_3 = {ball_pos.x - 1, ball_pos.y - 1, L_U};
}
}
list[0]= collided_brick_1;
list[1]= collided_brick_2;
list[2]= collided_brick_3;
}
void bricks_update(game_data &game, brick list[3]) {
for (int i = 0; i < 3; i++) {
if ((list[i].x != 0)||(list[i].y != 0)) {
if (list[i].character == brick_1_c) {
bool is_it_in_X_h_b = false;
for (size_t k = 0; k < X_h_bricks.size(); k++) {
if ((list[i].x == X_h_bricks[k].x)&&(list[i].y == X_h_bricks[k].y)) {
is_it_in_X_h_b = true;
X_h_bricks.erase(X_h_bricks.begin() + k);
game.scores += 5;
break;
}
}
if (!is_it_in_X_h_b) game.scores += 3;
for (size_t k = 0; k < bricks.size(); k++) {
if ((list[i].x == bricks[k].x)&&(list[i].y == bricks[k].y)) {
bricks.erase(bricks.begin() + k);
break;
}
}
}
else if (list[i].character == brick_2_c) {
for (size_t k = 0; k < bricks.size(); k++) {
if ((list[i].x == bricks[k].x)&&(list[i].y == bricks[k].y)) {
bricks[k].character = brick_1_c;
break;
}
}
xy an_X = {list[i].x, list[i].y};
X_h_bricks.push_back(an_X);
}
}
}
}
void add_to_history(game_data &game) {
time_t now = time(0);
game.date = ctime(&now);
if (!game.date.empty() && game.date.back() == '\n') {
game.date.pop_back();
}
string t = to_string(game.time);
string l = to_string(game.lives);
string s;
if (game.scores < 10) s = "000" + to_string(game.scores);
else if (game.scores < 100) s = "00" + to_string(game.scores);
else if (game.scores < 1000) s = "0" + to_string(game.scores);
else if (game.scores < 10000) s = to_string(game.scores);
else s = "3333";
string data_line = (s + "|" + l + "|" + game.player + "|" + game.date + "|" + t + "|" + game.result);
stored_g_data new_g_data = {game.scores, data_line};
vector<stored_g_data> stored_datas;
stored_datas.push_back(new_g_data);
ifstream i_history_file(file_name);
if (i_history_file.is_open()) {
string first_line;
getline(i_history_file, first_line);
string line;
while (getline(i_history_file, line)) {
int s_scores = 0;
string str_scores = line.substr(0, 4);
for (int i = 0; i < 4; i++) {
s_scores = s_scores * 10 + (str_scores[i] - '0');
}
stored_g_data a_g_data = {s_scores, line};
stored_datas.push_back(a_g_data);
}
i_history_file.close();
}
for (size_t k = 1; k < stored_datas.size(); k++) {
stored_g_data key = stored_datas[k];
int j = k - 1;
while (j >= 0 && stored_datas[j].t_score < key.t_score) {
stored_datas[j + 1] = stored_datas[j];
j--;
}
stored_datas[j + 1] = key;
}
ofstream o_history_file(file_name, ios::trunc);
if (o_history_file.is_open()) {
o_history_file << "Scores|lives|Player|Date|Time|Result\n";
for (size_t k = 0; k < stored_datas.size(); k++) {
o_history_file << stored_datas[k].data << '\n';
}
o_history_file.close();
}
}
void reset() {
size_t f = bricks.size();
for (size_t h = 0; h < f; h++) {
bricks.pop_back();
}
size_t l = X_h_bricks.size();
for (size_t h = 0; h < l; h++) {
X_h_bricks.pop_back();
}
initial_num_of_X_bricks = 0;
initial_num_of_lives = 6;
paddle_width = 0;
ball_pos = {0, 0};
ball_v = {0, 0};
paddle = {21, 26};
}
bool pause_menu() {
vector<string> pause_options = {"Resume", "Back to menu"};
int p_ans = menu_display(pause_options);
switch (p_ans) {
case 1: {
string sent_1 = "Press space key to resume";
print_left_padding(sent_1.length());
cout << general_co_2 << sent_1 << RESET << endl;
char r;
char rh;
do {
r = getch();
rh = r;
} while (rh != ' ');
system("cls");
return false;
}
case 2:
return true;
default: {
return true;
break;
}
}
}
bool game_loop(game_data &game) {
int delay = 200;
int counter = 0;
while (true) {
counter++;
char m = input();
char mh = m;
if (mh == ' ') {
bool to_exit = pause_menu();
if (to_exit) return false;
}
else if ((mh == 'a')||(mh == 'd')) paddle_position_update(mh);
brick b_list[3] = {{0, 0, '0'}, {0, 0, '0'}, {0, 0, '0'}};
ball_position_update(game, b_list);
bricks_update(game, b_list);
render();
display(game);
if (ball_pos.y == 26) {
srand(time(nullptr));
ball_pos.x = 2 + (rand() % 39);
ball_pos.y = 9 + (rand() % 16);
int v_index = rand() % 10;
ball_v.x = -(speed[v_index].x);
ball_v.y = -(speed[v_index].y);
}
string sent_2 = "Press space key to pause";
print_left_padding(sent_2.length());
cout << sent_2 << endl;
Sleep(delay);
if (counter > 4000) {
delay -= 20;
if (delay < 50) delay = 50;
counter = 0;
}
if (bricks.size() == 0) {
game.result = "Victory";
return true;
}
else if (game.lives == 0) {
game.result = "Game Over";
return true;
}
system("cls");
}
}
void settings() {
vector<string> setting_options = {"Difficulty Level", "Theme", "Back to menu"};
while (true) {
int s_ans = menu_display(setting_options);
switch (s_ans) {
case 1: {
string sent_3 = "Difficulty Level :";
print_left_padding(sent_3.length());
cout << general_co_2 << sent_3 << RESET << endl;
vector<string> d_levels_options = {"Easy", "Normal", "Hard", "Back"};
int d_l_ans = menu_display(d_levels_options);
switch (d_l_ans) {
case 1: {
initial_num_of_lives = 5;
initial_num_of_X_bricks = 1*41;
paddle_width = 9;
break;
}
case 2: {
initial_num_of_lives = 4;
initial_num_of_X_bricks = 2*41;
paddle_width = 7;
break;
}
case 3: {
initial_num_of_lives = 3;
initial_num_of_X_bricks = 3*41;
paddle_width = 5;
break;
}
case 4:
continue; //?
default:
break;
}
break;
}
case 2: {
string sent_4 = "Theme :";
print_left_padding(sent_4.length());
cout << general_co_2 << sent_4 << RESET << endl;
vector<string> theme_options = {"Visionary", "Apples in the woods", "Back"};
int th_ans = menu_display(theme_options);
switch (th_ans) {
case 1: {
down_wall_co = BRIGHT_BLUE;
wall_co = LAPIS_LAZULI;
ball_co = RED;
paddle_co = NEON_PURPLE;
X_brick_co = ORANGE_PUMPKIN;
brick_co = YELLOW_GOLD;
break;
}
case 2: {
down_wall_co = BLUE;
wall_co = DARK_BROWN;
ball_co = GRAY_DARK;
paddle_co = DARK_GREEN_EMERALD;
X_brick_co = RED;
brick_co = GREEN;
break;
}
case 3:
continue; //?
default:
break;
}
break;
}
case 3:
return;
default:
break;
}
}
}
void new_game() {
game_data new_game;
string sent_5 = "Enter player's name: ";
print_left_padding(sent_5.length());
cout << sent_5 << endl;
print_left_padding(sent_5.length());
cin >> new_game.player;
system("cls");
new_game.scores = 0;
if (initial_num_of_lives == 6) new_game.lives = 4;
else new_game.lives = initial_num_of_lives;
if (paddle_width == 0) paddle_width = 7;
if (initial_num_of_X_bricks == 0) initial_num_of_X_bricks = 41*2;
if (down_wall_co == "0") down_wall_co = BRIGHT_BLUE;
if (wall_co == "0") wall_co = LAPIS_LAZULI;
if (ball_co == "0") ball_co = RED;
if (paddle_co == "0") paddle_co = NEON_PURPLE;
if (X_brick_co == "0") X_brick_co = ORANGE_PUMPKIN;
if (brick_co == "0") brick_co = YELLOW_GOLD;
srand(time(nullptr));
ball_pos.x = 2 + (rand() % 39);
ball_pos.y = 9 + (rand() % 16);
int v_index = rand() % 10;
ball_v.x = -(speed[v_index].x);
ball_v.y = -(speed[v_index].y);
const int x = initial_num_of_X_bricks;
initial_bricks_vector(x);
render();
display(new_game);
string sent_6 = "Press space key to start";
print_left_padding(sent_6.length());
cout << sent_6 << endl;
char ans;
char ansh;
do {
ans = input();
ansh = ans;
} while (ansh != ' ');
system("cls");
clock_t start = clock();
bool is_game_done = game_loop(new_game);
if (is_game_done) {
clock_t end = clock();
double duration = double(end - start) / CLOCKS_PER_SEC;
new_game.time = duration/60; //in minutes
string sent_7 = new_game.result;
string result_color;
if (sent_7 == "Victory") result_color = GREEN;
else result_color = RED;
print_left_padding(sent_7.length());
cout << result_color << sent_7 << RESET << endl;
string lives_and_scores = "Lives: " + to_string(new_game.lives) + "Scores: " + to_string(new_game.scores);
print_left_padding(lives_and_scores.length());
cout << lives_and_scores << endl;
add_to_history(new_game);
reset();
string sent_8 = "Press space key to go to menu";
print_left_padding(sent_8.length());
cout << sent_8 << endl;
char b;
char bh;
do {
b = getch();
bh = b;
} while (bh != ' ');
system("cls");
}
}
void help() {
string guide_script_1 = "The player controls the ball's trajectory by moving a horizontal racket at the\n bottom of the screen.\nThe ball bounces upward after hitting the racket and breaks the bricks arranged\n at the top of the screen.\nThe ball's movement is based on simple physics; the angle of the ball's bounce\n depends on where it hits the racket.\nIf the ball reaches the bottom edge of the screen, the player loses a life and\n the ball appears on the screen in a new position.\nThe game continues until either all the bricks are destroyed or the player runs out of lives.\n";
string guide_script_2 = "In every menu, use w, s and Enter keys to select an option.\nIn order to move the paddle use a and d keys";
cout << guide_script_1 << endl << guide_script_2 << endl << endl;
string sent_9 = "Press space key to go to menu";
print_left_padding(sent_9.length());
cout << sent_9 << endl;
char b_f_help;
char b_f_helph;
do {
b_f_help = getch();
b_f_helph = b_f_help;
} while (b_f_helph != ' ');
system("cls");
}
void game_history() {
ifstream the_file(file_name);
if (the_file.is_open()) {
string line;
while (getline(the_file, line)) {
cout << line << endl;
}
the_file.close();
}
string sent_10 = "Press space key to go to menu";
print_left_padding(sent_10.length());
cout << sent_10 << endl;
char b_f_ghistory;
char b_f_ghistoryh;
do {
b_f_ghistory = getch();
b_f_ghistoryh = b_f_ghistory;
} while (b_f_ghistoryh != ' ');
system("cls");
}
bool exit() {
string sent_11 = "Are you sure?";
print_left_padding(sent_11.length());
cout << sent_11 << endl;
vector<string> exit_options = {"Yes", "No"};
int e_ans = menu_display(exit_options);
switch (e_ans) {
case 1:
return true;
case 2:
return false;
default: {
return true;
break;
}
}
}
void general_menu() {
vector<string> general_options = {"New Game", "Game History", "Settings", "Help", "Exit"};
while (true) {
cout << endl << endl;
string title_text = "~Break Out~";
print_left_padding(title_text.length());
cout << LAPIS_LAZULI << title_text << RESET << endl << endl << endl;
int g_ans = menu_display(general_options);
switch (g_ans) {
case 1: {
new_game();
break;
}
case 2: {
game_history();
break;
}
case 3: {
settings();
break;
}
case 4: {
help();
break;
}
case 5: {
bool out = exit();
if (out) return;
else continue;
break;
}
default:
break;
}
}
}
int main() {
hide_cursor();
general_menu();
}