Stap 3: Meer code
We beginnen met de "reset spel" functie waar wij onze GAME_SEQUENCE matrix opvullen
<p>void resetGame(){<br> // reset steps READ_STEP = 0; GAME_STEP = 0; // create random sequence for(int i=0; i<GAME_MAX_SEQUENCE; i++){<br> GAME_SEQUENCE[i] = random(4) + 1;<br> }</p><p> // Go to next game state; show led sequence GAME_STATUS = 1; }</p>
Onze volgende spel stap zou de afspelen-stap waar we de reeks spelen
void playSequence(){<br> // play a step of our sequence for(int i=0; i<=GAME_STEP; i++){ Serial.print("Set LED"); Serial.println(GAME_SEQUENCE[i]); delay(GAME_SPEED*2); setLED(GAME_SEQUENCE[i]); playTone(GAME_SEQUENCE[i]); delay(GAME_SPEED); clearLEDs(); } // Go to next step: reading buttons GAME_STATUS = 2; }
Na het spelen van onze reeks wachten we drukknop invoerfilter
void readSequence(){<br> // read our buttons int button_value = readButtons(); if(button_value > 0){ // a button has been pressed if(button_value == GAME_SEQUENCE[READ_STEP]){ // correct value! setLED(button_value); playTone(button_value); digitalWrite(LED_CORRECT, HIGH); delay(GAME_SPEED); clearLEDs(); digitalWrite(LED_CORRECT, LOW); // Lets speed it up! GAME_SPEED = GAME_SPEED-15; Serial.println("Correct!"); if(READ_STEP == GAME_STEP){ // reset read step READ_STEP = 0; // Go to next game step GAME_STEP++; // Go to playback sequence mode of our game GAME_STATUS = 1; Serial.println("Go To Next Step"); // Light all LEDs to indicate next sequence setLEDs(true,true,true,true); delay(GAME_SPEED); setLEDs(false,false,false,false); }else{ READ_STEP++; } delay(10); }else{ // wrong value! // Go to game over mode GAME_STATUS = 3; Serial.println("Game Over!"); } } delay(10); }
Tot slot voeren we de uit de "game over" modus wanneer u een fout maakt:
void gameOver(){<br> // Red RGB digitalWrite(LED_WRONG, HIGH); // Play Pwa Pwa Pwa tone(BUZZER, 98, TONE_DURATION); delay(TONE_DURATION); tone(BUZZER, 93, TONE_DURATION); delay(TONE_DURATION); tone(BUZZER, 87, TONE_DURATION); delay(TONE_DURATION); delay(GAME_SPEED); }
We hebben ook enkele hulpfuncties die ons leven gemakkelijker maken, ze kunnen overal worden gebruikt in onze code
void setLED(int id){<br> switch(id){ case 0: setLEDs(false,false,false,false); break; case 1: setLEDs(true,false,false,false); break; case 2: setLEDs(false,true,false,false); break; case 3: setLEDs(false,false,true,false); break; case 4: setLEDs(false,false,false,true); break; } }
void playTone(int id){ switch(id){ case 0: noTone(BUZZER); break; case 1: tone(BUZZER, RED_TONE, TONE_DURATION); break; case 2: tone(BUZZER, GREEN_TONE, TONE_DURATION); break; case 3: tone(BUZZER, BLUE_TONE, TONE_DURATION); break; case 4: tone(BUZZER, YELLOW_TONE, TONE_DURATION); break; } }
void setLEDs(boolean red, boolean green, boolean blue, int yellow ){ if (red) digitalWrite(LED_RED, HIGH); else digitalWrite(LED_RED, LOW); if (green) digitalWrite(LED_GREEN, HIGH); else digitalWrite(LED_GREEN, LOW); if (blue) digitalWrite(LED_BLUE, HIGH); else digitalWrite(LED_BLUE, LOW); if (yellow) digitalWrite(LED_YELLOW, HIGH); else digitalWrite(LED_YELLOW, LOW); }
void clearLEDs(){ setLEDs(false,false,false,false); }
int readButtons(void){ if (digitalRead(BUTTON_RED) == 0) return 1; else if (digitalRead(BUTTON_GREEN) == 0) return 2; else if (digitalRead(BUTTON_BLUE) == 0) return 3; else if (digitalRead(BUTTON_YELLOW) == 0) return 4; return 0; }