Aggiunta struttura status e sistemato alcuni conteggi

This commit is contained in:
Carlo 2025-11-08 12:18:32 +01:00
parent d985458100
commit 62f904d39a

54
main.c
View file

@ -5,6 +5,7 @@
#include <ncurses.h>
#include <time.h>
/************* STATIC DEFINITIONS ************/
#define DELAY 30000
#define ENEMY_N 5
#define BULLET_N 3
@ -12,7 +13,10 @@
#define HEIGHT 30
#define MIN_COLS 110
#define LIVES_N 5
#define TEXTLINES 4
#define BORDERLINES 2
/************* GAME STRUCTURES **************/
struct speed {
int slowness;
int count;
@ -25,8 +29,15 @@ struct game_obj {
struct speed y_speed;
};
struct status {
int lives;
int score;
int top_score;
int remaining_bullets;
};
/************** FUNZIONI GUI ***************/
#define TITLE_LINES 6
char* title[] =
{
"###########################################################################################################",
@ -39,7 +50,7 @@ char* title[] =
char menu_desc[] = "BENVENUTO. Premi qualsiasi tasto per giocare...";
#define GAMEOVER_LINES 4
char* gameover[] =
{
"o8888o o8888o o8oo8o o8888o o8888o 88 8o o8888o o8888o",
@ -54,11 +65,11 @@ void print_menu(WINDOW* win) {
int height;
int width;
getmaxyx(win, height, width);
for(int i = 0; i < TITLE_LINES; i++) {
for(int i = 0; i < (TEXTLINES+BORDERLINES); i++) {
mvwprintw(win, i+1, (width-(strlen(title[0])))/2, "%s", title[i]);
}
wattron(win, A_BLINK);
mvwprintw(win, TITLE_LINES+1, (width-(strlen(menu_desc)))/2, "%s", menu_desc);
mvwprintw(win, (TEXTLINES+BORDERLINES)+1, (width-(strlen(menu_desc)))/2, "%s", menu_desc);
wattroff(win, A_BLINK);
refresh();
return;
@ -70,30 +81,30 @@ void print_gameover(WINDOW* win) {
int ch;
wclear(win);
getmaxyx(win, height, width);
for(int i = 0; i < GAMEOVER_LINES; i++) {
for(int i = 0; i < TEXTLINES; i++) {
mvwprintw(win, i+1, (width-(strlen(gameover[0])))/2, "%s", gameover[i]);
}
wattron(win, A_BLINK);
mvwprintw(win, GAMEOVER_LINES+1, (width-(strlen(gameover_desc)))/2, "%s", gameover_desc);
mvwprintw(win, TEXTLINES+1, (width-(strlen(gameover_desc)))/2, "%s", gameover_desc);
wattroff(win, A_BLINK);
refresh();
while((ch = wgetch(win)) != 'q');
return;
}
void print_status_bar(WINDOW* main_win, int y, int x, int lives, int score, int top_score, int shots) {
void print_status_bar(WINDOW* main_win, int y, int x, struct status game_status) {
int status_x = x + 1;
int status_y = y;
status_x++;
// lives
mvwprintw(main_win, status_y, status_x, "LIVES:\t%d", lives);
mvwprintw(main_win, status_y, status_x, "LIVES:\t%d", game_status.lives);
// score
mvwprintw(main_win, status_y+1, status_x, "SCORE:\t%d", score);
mvwprintw(main_win, status_y+1, status_x, "SCORE:\t%d", game_status.score);
// top score
mvwprintw(main_win, status_y+2, status_x, "TOP SCORE:\t%d", top_score);
mvwprintw(main_win, status_y+2, status_x, "TOP SCORE:\t%d", game_status.top_score);
// bullets
mvwprintw(main_win, status_y+3, status_x, "BULLETS:\t%d", shots);
mvwprintw(main_win, status_y+3, status_x, "BULLETS:\t%d", game_status.remaining_bullets);
// refresh window
wrefresh(main_win);
@ -112,9 +123,13 @@ int main() {
// Giocatore
struct game_obj player = {};
int input;
int top_score = 0;
int score = 0;
int lives = LIVES_N;
// Status
struct status game_status = {};
game_status.top_score = 0;
game_status.score = 0;
game_status.lives = LIVES_N;
game_status.remaining_bullets = BULLET_N;
// Proiettile
struct game_obj bullet[BULLET_N] = {};
@ -228,7 +243,7 @@ int main() {
if(bullet[j].x == enemy[i].x && bullet[j].y == enemy[i].y) {
enemy[i].active = 0;
bullet[j].active = 0;
score++;
game_status.score++;
}
}
}
@ -254,8 +269,8 @@ int main() {
}
if(enemy[i].y > height - 2) {
enemy[i].active = 0;
lives--;
if(lives == 0) {
game_status.lives--;
if(game_status.lives == 0) {
player.active = 0;
print_gameover(stdscr);
}
@ -270,7 +285,7 @@ int main() {
if(bullet[j].x == enemy[i].x && bullet[j].y == enemy[i].y) {
enemy[i].active = 0;
bullet[j].active = 0;
score++;
game_status.score++;
}
}
}
@ -286,6 +301,7 @@ int main() {
next_bullet = i;
}
}
game_status.remaining_bullets = BULLET_N - bullet_attivi;
// Disegna scena
werase(game_window);
@ -310,7 +326,7 @@ int main() {
}
// Status Bar
print_status_bar(stdscr, win_starty, (win_startx + width), lives, score, top_score, (BULLET_N - bullet_attivi));
print_status_bar(stdscr, win_starty, (win_startx + width), game_status);
}