diff --git a/carrot_game/bitmap.h b/carrot_game/bitmap.h new file mode 100644 index 0000000..142e63d --- /dev/null +++ b/carrot_game/bitmap.h @@ -0,0 +1,13 @@ +PROGMEM const unsigned char carrot[]= { + 0x60, 0x20, 0xec, 0x30, 0xfe, 0x7f, 0xf6, 0x7f, 0xf6, 0x7f, 0xff, 0xff, + 0xff, 0xed, 0xff, 0xff, 0xfe, 0x7f, 0xf6, 0x7f, 0xc6, 0x3f, 0x06, 0x1f, + 0xcc, 0x0f, 0xec, 0x0f, 0xf8, 0x0f, 0xf0, 0x1f }; +PROGMEM const unsigned char fish[]={ + 0x00, 0x00, 0x20, 0x00, 0x60, 0x00, 0xe0, 0x03, 0xe0, 0x07, 0xe6, 0x1f, + 0xfe, 0x37, 0xfc, 0x7f, 0xfc, 0x3f, 0xee, 0x1f, 0xe6, 0x0f, 0xe0, 0x07, + 0x70, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00 }; + +PROGMEM const unsigned char fish_reverse[] { + 0x00, 0x00, 0x00, 0x04, 0x00, 0x06, 0xc0, 0x07, 0xe0, 0x07, 0xf8, 0x67, + 0xec, 0x7f, 0xfe, 0x3f, 0xfc, 0x3f, 0xf8, 0x77, 0xf0, 0x67, 0xe0, 0x07, + 0x00, 0x0e, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00 }; \ No newline at end of file diff --git a/carrot_game/carrot_game.ino b/carrot_game/carrot_game.ino new file mode 100644 index 0000000..da61652 --- /dev/null +++ b/carrot_game/carrot_game.ino @@ -0,0 +1,285 @@ +#include +#include +#include +#include +#include +#include +#include "bitmap.h" +#include "sound.h" + +const short initX = 0; +const short initY = 48; + +const unsigned long display_interval = 100; +const unsigned long nunchuk_interval = 100; +const unsigned int sound_tempo = 10; + +U8G2_ST7920_128X64_2_SW_SPI u8g2(U8G2_R0, /* clock=*/ 2, /* data=*/ 10, /* cs=*/ 7); +//A5 A4 A3 A2 +ArduinoNunchuk nunchuk = ArduinoNunchuk(); +Tone tone1; + +short posX = initX; +short posY = initY; +unsigned int nextLaneY = initY - 16; +unsigned long last_display = millis(); +unsigned long last_nunchuck = millis(); +unsigned long last_note = millis(); +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} }; +bool game_init = true; +bool jump = false; +bool jumpBack = false; +bool jumpInProgress = false; +int score = 50; + +//set A3 and A2 has +5V / GND +void nunchuck_setpowerpins() +{ + #define pwrpin PORTC3 + #define gndpin PORTC2 + DDRC |= _BV(pwrpin) | _BV(gndpin); + PORTC &=~ _BV(gndpin); + PORTC |= _BV(pwrpin); + delay(100); // wait for things to stabilize +} + +void setup(void) { + + Serial.begin(115200); + nunchuck_setpowerpins(); + nunchuk.init(); + + u8g2.begin(); + + tone1.begin(8); + +} + +void play_intro(){ + if(millis() >= next_note){ + next_note = millis() + interval_intro[notes_index]; + tone1.play(notes_intro[notes_index], interval_intro[notes_index]); + if(notes_index < intro_size - 1){ + notes_index++; + } + //else{ + // notes_index = 0; + //} + } +} + +void display_welcome_page() { + u8g2.setFont(u8g2_font_5x7_mf); + u8g2.drawStr(0, 7, "Welcome to Carrot game!"); + u8g2.drawStr(15, 25, "Press Z to start."); + u8g2.drawXBMP(posX, posY, 16, 16, fish); +} + +void display_game() { + u8g2.setFont(u8g2_font_5x7_mf); + char score_str[] = "000"; + sprintf(score_str, "%03d", score); + u8g2.drawStr(110, 62, score_str); + for (int i = 0; i < 8; i++) { + if(fishes[i][0] != NULL){ + u8g2.drawXBMP(fishes[i][0], fishes[i][1], 16, 16, fish_reverse); + } + } + 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 +} + +void compute_fishes_position(){ + for (int i = 0; i < 8; i++) { + if(fishes[i][0] != NULL){ + if(fishes[i][0] <= 2) + { + fishes[i][0] = 128; + score = score - 10; + tone1.play(NOTE_B1,300); + if (score <= 0){ + tone1.play(NOTE_G1,300); + tone1.play(NOTE_F1,300); + tone1.play(NOTE_E1,300); + tone1.play(NOTE_D1,300); + tone1.play(NOTE_C1,300); + game_init = true; + } + } + else{ + fishes[i][0] = fishes[i][0] - (200 + score)/100; + } + //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) )){ + fishes[i][0] = random(128,255); + tone1.play(NOTE_B5,100); + score = score + 10; + + } + Serial.print("score: "); + Serial.println(score); + } + } +} +void compute_cat_position() { + if (game_init) { + if (posX < 112) { + posX = posX + 2; + } + else { + posX = initX; + if (posY < 48) { + posY = posY + 16; + } else { + posY = initY; + } + } + } + //game + else { + //compute X pos + if (nunchuk.analogX < 120 && posX >= 4) { + if (nunchuk.analogX < 80) { + if(posX >= 8){ + posX = posX - 8; + }else{ + posX = posX - 4; + } + + } else { + posX = posX - 4; + } + } + if (nunchuk.analogX > 140 && posX < 112) { + if (nunchuk.analogX > 180) { + posX = posX + 8; + } else { + posX = posX + 4; + } + } + //compute jump + //detect a new jump + if(nunchuk.zButton == 1 && !jump && !jumpBack && posY > 0){ + Serial.println("jump!"); + //tone1.play(NOTE_B4,500); + jump = true; + jumpInProgress = true; + nextLaneY = posY - 16; + posY = posY - 4; + Serial.println(nextLaneY); + } + else { + //continue a current jump + if (jump) { + + if (posY > nextLaneY && jumpInProgress) { + posY = posY - 4; + } else { + if (posY != nextLaneY){ + jumpInProgress = false; + posY = posY + 4; + } + else{ + jump = false; + } + } + } + } + + if(nunchuk.cButton == 1 && !jump && !jumpBack && posY < 48){ + Serial.println("jump bottom!"); + //tone1.play(NOTE_B2,500); + jumpBack = true; + jumpInProgress = true; + nextLaneY = posY + 16; + posY = posY + 4; + Serial.println(nextLaneY); + } + else { + //continue a current jump + if (jumpBack) { + + if (posY < nextLaneY && jumpInProgress) { + posY = posY + 4; + } else { + if (posY != nextLaneY){ + jumpInProgress = false; + posY = posY - 4; + } + else{ + jumpBack = false; + } + } + } + } + + } +} + +void loop(void) { + if(millis() - last_display >= display_interval){ + u8g2.firstPage(); + do { + if(game_init){ + display_welcome_page(); + score = 50; + } else{ + display_game(); + } + + } while ( u8g2.nextPage() ); + last_display = millis(); + } + if(millis() - last_nunchuck >= nunchuk_interval){ + nunchuk.update(); + if(init){ + if(nunchuk.zButton == 1){ + if(game_init){ + game_init = false; + //set default pos to avoid issue + posX = initX; + posY = initY; + } + } + } + + last_nunchuck = millis(); + if(game_init){ + play_intro(); + } + else{ + compute_cat_position(); + compute_fishes_position(); + } + } + // Serial.print(posX); + // Serial.print(' '); + // Serial.println(posY); + // Serial.print(nunchuk.analogX, DEC); + // Serial.print(' '); + // Serial.println(nunchuk.analogY, DEC); + //Serial.print(' '); + // Serial.print(nunchuk.accelX, DEC); + // Serial.print(' '); + // Serial.print(nunchuk.accelY, DEC); + // Serial.print(' '); + // Serial.print(nunchuk.accelZ, DEC); + // Serial.print(' '); + // Serial.print(nunchuk.zButton, DEC); + // Serial.print(' '); + // Serial.println(nunchuk.cButton, DEC); + //delay(interval); + +} + diff --git a/carrot_game/sound.h b/carrot_game/sound.h new file mode 100644 index 0000000..27255a9 --- /dev/null +++ b/carrot_game/sound.h @@ -0,0 +1,23 @@ +const unsigned int notes_intro[] = { + NOTE_E4, + NOTE_E4, + NOTE_E4, + NOTE_C4, + NOTE_E4, + NOTE_G4, + 0, + NOTE_G3, + 0, +}; + +const unsigned long interval_intro[] = { + 50, + 50, + 100, + 50, + 100, + 200, + 200, + 400, + 800 +}; \ No newline at end of file diff --git a/resources/avatar_black.bmp b/resources/avatar_black.bmp new file mode 100644 index 0000000..2dccdf7 Binary files /dev/null and b/resources/avatar_black.bmp differ diff --git a/resources/avatar_black.xbm b/resources/avatar_black.xbm new file mode 100644 index 0000000..4162c8e --- /dev/null +++ b/resources/avatar_black.xbm @@ -0,0 +1,6 @@ +#define avatar_black_width 16 +#define avatar_black_height 16 +static unsigned char avatar_black_bits[] = { + 0x60, 0x20, 0xec, 0x30, 0xfe, 0x7f, 0xf6, 0x7f, 0xf6, 0x7f, 0xff, 0xff, + 0xff, 0xed, 0xff, 0xff, 0xfe, 0x7f, 0xf6, 0x7f, 0xc6, 0x3f, 0x06, 0x1f, + 0xcc, 0x0f, 0xec, 0x0f, 0xf8, 0x0f, 0xf0, 0x1f }; diff --git a/resources/poisson.bmp b/resources/poisson.bmp new file mode 100644 index 0000000..7316fb1 Binary files /dev/null and b/resources/poisson.bmp differ diff --git a/resources/poisson.xbm b/resources/poisson.xbm new file mode 100644 index 0000000..82b61cc --- /dev/null +++ b/resources/poisson.xbm @@ -0,0 +1,6 @@ +#define poisson_width 16 +#define poisson_height 16 +static unsigned char poisson_bits[] = { + 0x00, 0x00, 0x00, 0x04, 0x00, 0x06, 0xc0, 0x07, 0xe0, 0x07, 0xf8, 0x67, + 0xec, 0x7f, 0xfe, 0x3f, 0xfc, 0x3f, 0xf8, 0x77, 0xf0, 0x67, 0xe0, 0x07, + 0x00, 0x0e, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00 };