feat: add leds integration for lives

This commit is contained in:
valvin 2024-03-10 22:01:15 +01:00
parent 04d24f0f9e
commit 4b3cc98b15
1 changed files with 41 additions and 8 deletions

View File

@ -18,7 +18,9 @@ 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;
const int dataPin = 11;
const int clockPin = 5;
const int latchPin = 6;
U8G2_ST7920_128X64_2_SW_SPI u8g2(U8G2_R0, /* clock=*/ 2, /* data=*/ 10, /* cs=*/ 7);
//A5 A4 A3 A2
ArduinoNunchuk nunchuk = ArduinoNunchuk();
@ -48,9 +50,21 @@ bool game_init = true;
bool jump = false;
bool jumpBack = false;
bool jumpInProgress = false;
unsigned int score = 0;
unsigned int max_score = 0;
int score = 0;
int max_score = 0;
short lives = 5;
int lives_leds[] = {
0b00000000,
0b00000001,
0b00000011,
0b00000111,
0b00001111,
0b00011111,
0b00111111,
0b00111111,
0b00111111,
0b00111111
};
//set A3 and A2 has +5V / GND
void nunchuck_setpowerpins()
@ -62,11 +76,14 @@ void nunchuck_setpowerpins()
PORTC |= _BV(pwrpin);
delay(100); // wait for things to stabilize
}
void setup(void) {
//init serial for debug
Serial.begin(115200);
//init leds
pinMode( dataPin, OUTPUT );
pinMode( clockPin, OUTPUT );
pinMode( latchPin, OUTPUT );
//init nunchuck
nunchuck_setpowerpins();
nunchuk.init();
@ -74,9 +91,18 @@ void setup(void) {
u8g2.begin();
//init sound
tone1.begin(8);
//test leds
for(int i = 0; i <= 9; i++){
update_lives_leds(lives_leds[i]);
delay(200);
}
}
void update_lives_leds( int value ){
digitalWrite( latchPin, LOW );
shiftOut( dataPin, clockPin, MSBFIRST,value );
digitalWrite( latchPin, HIGH );
}
void play_intro(){
if(SOUND_ENABLE){
if(millis() >= next_note){
@ -167,6 +193,7 @@ void init_game(){
score = 0;
max_score = 0;
lives = 5;
update_lives_leds(lives_leds[lives]);
posX = INIT_X;
posY = INIT_Y;
for ( short i = 0 ; i < NUM_FISHES; i++){
@ -177,7 +204,7 @@ void init_game(){
void missed_or_bomb(){
score = score - 10;
lives = lives - 1;
update_lives_leds(lives_leds[lives]);
play_sound(NOTE_B1,300);
if (score <= 0 || lives == 0){
play_sound(NOTE_G1,300);
@ -197,8 +224,11 @@ void compute_fishes_position(){
if(fishes[i][0] <= fish_step)
{
fishes[i][0] = random(MAX_X+1,255);
// do not remove lives if it was a bomb
if(fishes[i][2] != FT_BOMB){
missed_or_bomb();
}
}
//move fish to the left
else{
fishes[i][0] = fishes[i][0] - fish_step;
@ -219,7 +249,10 @@ void compute_fishes_position(){
}
if(fishes[i][2] == FT_LIFE){
if(lives < 6){
lives = lives + 1;
update_lives_leds(lives_leds[lives]);
}
}
fishes[i][0] = random(MAX_X+1,255);
//random type