diff --git a/carrot_game/carrot_game.ino b/carrot_game/carrot_game.ino index 920624e..8d5d200 100644 --- a/carrot_game/carrot_game.ino +++ b/carrot_game/carrot_game.ino @@ -11,8 +11,11 @@ const bool SOUND_ENABLE = false; const short INIT_X = 0; const short INIT_Y = 48; +const short MAX_X = 90; +// number of milliseconds we refresh screen const unsigned long display_interval = 100; +// number of milliseconds we refresh nunchuck info const unsigned long nunchuk_interval = 100; const unsigned int sound_tempo = 10; @@ -21,9 +24,11 @@ U8G2_ST7920_128X64_2_SW_SPI u8g2(U8G2_R0, /* clock=*/ 2, /* data=*/ 10, /* cs=*/ ArduinoNunchuk nunchuk = ArduinoNunchuk(); Tone tone1; +// Carrot position short posX = INIT_X; short posY = INIT_Y; unsigned int nextLaneY = INIT_Y - 16; + unsigned long last_display = millis(); unsigned long last_nunchuck = millis(); unsigned long last_note = millis(); @@ -31,12 +36,16 @@ unsigned long next_note = millis(); short notes_index = 0; short intro_size = sizeof(notes_intro)/sizeof(notes_intro[0]); -short fishes[8][2] = {{random(128,255) , 32}, {random(128,255) , 32}, {random(128,255),48}, {random(128,255),48}, {random(128,255),16}, {random(128,255),16}, {random(128,255),1}, {random(128,255) , 1} }; +short fishes[8][2] = { + {0,1},{0,1},{0,16},{0,16}, + {0,32},{0,32},{0,48},{0,48} + }; bool game_init = true; bool jump = false; bool jumpBack = false; bool jumpInProgress = false; -unsigned int score = 50; +unsigned int score = 0; +unsigned int max_score = 0; //set A3 and A2 has +5V / GND void nunchuck_setpowerpins() @@ -89,36 +98,58 @@ void display_welcome_page() { } void display_game() { + u8g2.setFont(u8g2_font_4x6_tf); + u8g2.drawStr(108, 50, "SCORE:"); + u8g2.drawStr(108, 10, "MAX:"); + //draw score u8g2.setFont(u8g2_font_5x7_mf); char score_str[] = "000"; sprintf(score_str, "%03d", score); u8g2.drawStr(110, 62, score_str); + sprintf(score_str, "%03d", max_score); + u8g2.drawStr(110, 20, score_str); + //draw fishes for (int i = 0; i < 8; i++) { if(fishes[i][0] != NULL){ - u8g2.drawXBMP(fishes[i][0], fishes[i][1], 16, 16, fish_reverse); + if(fishes[i][0] < MAX_X){ + u8g2.drawXBMP(fishes[i][0], fishes[i][1], 16, 16, fish_reverse); + } } } + //draw carrot u8g2.drawXBMP(posX, posY, 16, 16, carrot); //swimelanes - u8g2.drawHLine(0, 0, 128); //top - u8g2.drawHLine(0, 16, 128); - u8g2.drawHLine(0, 32, 128); - u8g2.drawHLine(0, 48, 128); - u8g2.drawHLine(0, 63, 128); //bottom + u8g2.drawHLine(0, 0, MAX_X + 16); //top + u8g2.drawHLine(0, 16, MAX_X + 16); + u8g2.drawHLine(0, 32, MAX_X + 16); + u8g2.drawHLine(0, 48, MAX_X + 16); + u8g2.drawHLine(0, 63, MAX_X + 16); //bottom + u8g2.drawVLine(MAX_X + 16, 0, 48); +} + +void init_game(){ + score = 0; + posX = INIT_X; + posY = INIT_Y; + for ( short i = 0 ; i < 8; i++){ + fishes[i][0]= random(MAX_X + 1,255); + } } void compute_fishes_position(){ short fish_step = (200 + score)/100; for (int i = 0; i < 8; i++) { if(fishes[i][0] != NULL){ + //fish alive if(fishes[i][0] <= fish_step) { - fishes[i][0] = 128; + fishes[i][0] = random(MAX_X+1,255); score = score - 10; play_sound(NOTE_B1,300); + //end game if (score <= 0){ play_sound(NOTE_G1,300); play_sound(NOTE_F1,300); @@ -128,15 +159,20 @@ void compute_fishes_position(){ game_init = true; } } + //move fish to the left else{ fishes[i][0] = fishes[i][0] - fish_step; } //does carrot eat the fish - if( (posX > (fishes[i][0] - 8) && posX < (fishes[i][0] + 8) ) && (posY > (fishes[i][1] - 8) && posY < (fishes[i][1] + 8) )){ + if( (posX > (fishes[i][0] - 8) && posX < (fishes[i][0] + 8) ) + && (posY > (fishes[i][1] - 8) && posY < (fishes[i][1] + 8) ) + ){ fishes[i][0] = random(128,255); play_sound(NOTE_B5,100); score = score + 10; - + if (score > max_score){ + max_score = score; + } } Serial.print("score: "); Serial.println(score); @@ -145,7 +181,7 @@ void compute_fishes_position(){ } void compute_cat_position() { if (game_init) { - if (posX < 112) { + if (posX < MAX_X - 16) { posX = posX + 2; } else { @@ -172,7 +208,7 @@ void compute_cat_position() { posX = posX - 4; } } - if (nunchuk.analogX > 140 && posX < 112) { + if (nunchuk.analogX > 140 && posX < (MAX_X - 16)) { if (nunchuk.analogX > 180) { posX = posX + 8; } else { @@ -244,7 +280,6 @@ void loop(void) { do { if(game_init){ display_welcome_page(); - score = 50; } else{ display_game(); } @@ -257,10 +292,8 @@ void loop(void) { if(init){ if(nunchuk.zButton == 1){ if(game_init){ + init_game(); game_init = false; - //set default pos to avoid issue - posX = INIT_X; - posY = INIT_Y; } } }