Предмет: Информатика, автор: sashagavrysh2021

80 БАЛЛОВ

Допрацюйте проєкт, розроблений на занятті. Додайте
кнопку для рестарту гри.
Крім того, по закінченню гри виведіть у Serial Monitor загальний рахунок гравця (кількість комбінацій, які йому вдалося повторити).

const int LED_RED = 11;
const int LED_GREEN = 9;
const int LED_BLUE = 7;
const int LED_YELLOW = 5;

const int LED_CORRECT = 4;
const int LED_WRONG = 2;

const int BTN_RED = 12;
const int BTN_GREEN = 10;
const int BTN_BLUE = 8;
const int BTN_YELLOW = 6;

int counter;

int GAME_SPEED = 250;
int GAME_STATUS = 0;
int GAME_SEQUENCE[50];
int GAME_STEP = 0;
int READ_STEP = 0;

void setup(){
Serial.begin(9600);
randomSeed(analogRead(0));
pinMode(LED_RED, OUTPUT);
pinMode(LED_GREEN, OUTPUT);
pinMode(LED_BLUE, OUTPUT);
pinMode(LED_YELLOW, OUTPUT);
pinMode(LED_CORRECT, OUTPUT);
pinMode(LED_WRONG, OUTPUT);
pinMode(BTN_RED, INPUT_PULLUP);
pinMode(BTN_GREEN, INPUT_PULLUP);
pinMode(BTN_BLUE, INPUT_PULLUP);
pinMode(BTN_YELLOW, INPUT_PULLUP);
Serial.begin(9600);
}


void loop(){
switch(GAME_STATUS){
case 0:
resetGame();
break;
case 1:
playSequence();
break;
case 2:
readSequence();
break;
case 3:
gameOver();
break;
if (digitalRead(counter) == LOW) {
GAME_STATUS = 0;
resetGame();

}


}




}

void resetGame(){
READ_STEP = 0;
GAME_STEP = 0;
for (int i = 0; i < 50; i++){
GAME_SEQUENCE[i] = random(4) + 1;
}
GAME_STATUS = 1;
Serial.println("reset");
digitalWrite(LED_CORRECT, 0);
digitalWrite(LED_WRONG, 0);
}

void playSequence(){
for (int i = 0; i <= GAME_STEP; i++){
delay(GAME_SPEED * 2);
setLED(GAME_SEQUENCE[i]);
delay(GAME_SPEED);
setLED(0);
}
GAME_STATUS = 2;
Serial.println("play");
}

void readSequence(){
int btn_value = readButtons();

if (btn_value > 0){
if (btn_value == GAME_SEQUENCE[READ_STEP]){
Serial.println("read");
setLED(btn_value);
digitalWrite(LED_CORRECT, 1);
delay(GAME_SPEED);
setLED(0);
digitalWrite(LED_CORRECT, 0);

if (READ_STEP == GAME_STEP){
READ_STEP = 0;
GAME_STEP = GAME_STEP + 1;
GAME_STATUS = 1;
setLEDs(true, true, true, true);
delay(GAME_SPEED);
setLEDs(false, false, false, false);
if (GAME_SPEED > 100){
GAME_SPEED = GAME_SPEED - 15;
}
}
else {
READ_STEP = READ_STEP + 1;
}
delay(1000);
}
else {
GAME_STATUS = 3;
}
}
delay(10);
}

void gameOver(){
digitalWrite(LED_WRONG, 1);
Serial.println("game over");
GAME_STATUS = 4;
}

int readButtons(void){
if(digitalRead(BTN_RED) == 0) return 1;
else if(digitalRead(BTN_GREEN) == 0) return 2;
else if(digitalRead(BTN_BLUE) == 0) return 3;
else if(digitalRead(BTN_YELLOW) == 0) return 4;

return 0;
}


void setLED(int id){
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 setLEDs(boolean red, boolean green, boolean blue, boolean 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 resetGame(){
SCORE = 0;
}

Ответы

Автор ответа: fxhcgfjkh454554jk
0

Ответ:Вам потрібно видалити останню функцію resetGame(), оскільки вона вже існує. Ви також повинні створити нову функцію для обробки рестарту гри.

Ось змінений код:

```cpp

const int LED_RED = 11;

const int LED_GREEN = 9;

const int LED_BLUE = 7;

const int LED_YELLOW = 5;

const int LED_CORRECT = 4;

const int LED_WRONG = 2;

const int BTN_RED = 12;

const int BTN_GREEN = 10;

const int BTN_BLUE = 8;

const int BTN_YELLOW = 6;

int counter;

int GAME_SPEED = 250;

int GAME_STATUS = 0;

int GAME_SEQUENCE[50];

int GAME_STEP = 0;

int READ_STEP = 0;

int SCORE = 0;

void setup(){

 Serial.begin(9600);

 randomSeed(analogRead(0));

 pinMode(LED_RED, OUTPUT);

 pinMode(LED_GREEN, OUTPUT);

 pinMode(LED_BLUE, OUTPUT);

 pinMode(LED_YELLOW, OUTPUT);

 pinMode(LED_CORRECT, OUTPUT);

 pinMode(LED_WRONG, OUTPUT);

 pinMode(BTN_RED, INPUT_PULLUP);

 pinMode(BTN_GREEN, INPUT_PULLUP);

 pinMode(BTN_BLUE, INPUT_PULLUP);

 pinMode(BTN_YELLOW, INPUT_PULLUP);

 Serial.begin(9600);

}

void loop(){

 switch(GAME_STATUS){

   case 0:

     resetGame();

     break;

   case 1:

     playSequence();

     break;

   case 2:

     readSequence();

     break;

   case 3:

     gameOver();

     break;

   case 4:

     restartGame();

     break;

 }

}

void resetGame(){

 READ_STEP = 0;

 GAME_STEP = 0;

 for (int i = 0; i < 50; i++){

   GAME_SEQUENCE[i] = random(4) + 1;

 }

 GAME_STATUS = 1;

 SCORE = 0;

 Serial.println("reset");

 digitalWrite(LED_CORRECT, 0);

 digitalWrite(LED_WRONG, 0);

}

void playSequence(){

 for (int i = 0; i <= GAME_STEP; i++){

   delay(GAME_SPEED * 2);

   setLED(GAME_SEQUENCE[i]);

   delay(GAME_SPEED);

   setLED(0);

 }

 GAME_STATUS = 2;

 Serial.println("play");

}

void readSequence(){

 int btn_value = readButtons();

 if (btn_value > 0){

   if (btn_value == GAME_SEQUENCE[READ_STEP]){

     Serial.println("read");

     setLED(btn_value);

     digitalWrite(LED_CORRECT, 1);

     delay(GAME_SPEED);

     setLED(0);

     digitalWrite(LED_CORRECT, 0);

     if (READ_STEP == GAME_STEP){

       READ_STEP = 0;

       GAME_STEP = GAME_STEP + 1;

       GAME_STATUS = 1;

       SCORE++;

       setLEDs(true, true, true, true);

       delay(GAME_SPEED);

       setLEDs(false, false, false, false);

       if (GAME_SPEED > 100){

         GAME_SPEED = GAME_SPEED - 15;

       }

     }

     else {

       READ_STEP = READ_STEP + 1;

     }

     delay(1000);

   }

   else {

     GAME_STATUS = 3;

   }

 }

 delay(10);

}

void gameOver(){

 digitalWrite(LED_WRONG, 1);

 Serial.print("game over. Score: ");

 Serial.println(SCORE);

 GAME_STATUS = 4;

}

void restartGame(){

 if (digitalRead

Объяснение:


sashagavrysh2021: Вибач но ти недописав останню функцыю
sashagavrysh2021: но дякую за выдповыдь
fxhcgfjkh454554jk: я же написала что мы немного меняем...
Похожие вопросы
Предмет: Химия, автор: kodraaurika
Предмет: Математика, автор: vozmilovanatolii883