feat: add lives

This commit is contained in:
valvin 2024-02-18 21:42:31 +01:00
parent 8a307b6435
commit 9afc60eab5
1 changed files with 19 additions and 7 deletions

View File

@ -46,6 +46,7 @@ bool jumpBack = false;
bool jumpInProgress = false; bool jumpInProgress = false;
unsigned int score = 0; unsigned int score = 0;
unsigned int max_score = 0; unsigned int max_score = 0;
short lives = 5;
//set A3 and A2 has +5V / GND //set A3 and A2 has +5V / GND
void nunchuck_setpowerpins() void nunchuck_setpowerpins()
@ -94,20 +95,29 @@ void display_welcome_page() {
u8g2.setFont(u8g2_font_5x7_mf); u8g2.setFont(u8g2_font_5x7_mf);
u8g2.drawStr(0, 7, "Welcome to Carrot game!"); u8g2.drawStr(0, 7, "Welcome to Carrot game!");
u8g2.drawStr(15, 25, "Press Z to start."); u8g2.drawStr(15, 25, "Press Z to start.");
u8g2.drawXBMP(posX, posY, 16, 16, fish); u8g2.drawXBMP(INIT_X, INIT_Y, 16, 16, fish);
if(max_score != 0){
char num_str[] = "000";
sprintf(num_str, "%03d", max_score);
u8g2.drawStr(50, 50, "Last score: ");
u8g2.drawStr(110, 50, num_str);
}
} }
void display_game() { void display_game() {
u8g2.setFont(u8g2_font_4x6_tf); u8g2.setFont(u8g2_font_4x6_tf);
u8g2.drawStr(108, 50, "SCORE:"); u8g2.drawStr(108, 50, "SCORE:");
u8g2.drawStr(108, 30, "LIVES:");
u8g2.drawStr(108, 10, "MAX:"); u8g2.drawStr(108, 10, "MAX:");
//draw score //draw score
u8g2.setFont(u8g2_font_5x7_mf); u8g2.setFont(u8g2_font_5x7_mf);
char score_str[] = "000"; char num_str[] = "000";
sprintf(score_str, "%03d", score); sprintf(num_str, "%03d", score);
u8g2.drawStr(110, 62, score_str); u8g2.drawStr(110, 62, num_str);
sprintf(score_str, "%03d", max_score); sprintf(num_str, "%03d", max_score);
u8g2.drawStr(110, 20, score_str); u8g2.drawStr(110, 20, num_str);
sprintf(num_str, "%03d", lives);
u8g2.drawStr(110, 40, num_str);
//draw fishes //draw fishes
for (int i = 0; i < 8; i++) { for (int i = 0; i < 8; i++) {
if(fishes[i][0] != NULL){ if(fishes[i][0] != NULL){
@ -132,6 +142,7 @@ void display_game() {
void init_game(){ void init_game(){
score = 0; score = 0;
max_score = 0; max_score = 0;
lives = 5;
posX = INIT_X; posX = INIT_X;
posY = INIT_Y; posY = INIT_Y;
for ( short i = 0 ; i < 8; i++){ for ( short i = 0 ; i < 8; i++){
@ -148,10 +159,11 @@ void compute_fishes_position(){
{ {
fishes[i][0] = random(MAX_X+1,255); fishes[i][0] = random(MAX_X+1,255);
score = score - 10; score = score - 10;
lives = lives - 1;
play_sound(NOTE_B1,300); play_sound(NOTE_B1,300);
//end game //end game
if (score <= 0){ if (score <= 0 || lives == 0){
play_sound(NOTE_G1,300); play_sound(NOTE_G1,300);
play_sound(NOTE_F1,300); play_sound(NOTE_F1,300);
play_sound(NOTE_E1,300); play_sound(NOTE_E1,300);