So my team and I have been trying to get a version of wack-a-mole but with vex equipment. We got everything done except the keeping score and we’re gonna have LED 5 be the one keeping the score. We are gonna try to do that when the LED lights up then you press the button at the same time and when you get it on time LED flashes once. BUT we don’t know how to have the score to continue to add up.
What you’re doing with if statements could have been done with an if-else block; however it should be done with a switch statement. Also, when using repetitive code, such as the whack-a-mole blink led code, it is best to create a dynamic method to reduce the typing and create a better structured code infrastructure.
int value, score;
tSensors SCORE_LED = GreenLED5;
//Blinks LED and checks if button is pressed
//creating a "Whack-A-Mole" effect
void blinkLED(tSensors led) {
int lightDuration = 600;
//Turn on led for 300ms
turnLEDOn(led);
waitInMilliseconds(lightDuration / 2);
//While LED is on, check if button is pressed
//if button is pressed, blink LED #5
if(SensorValue[Button1]==1) {
turnLEDOn(SCORE_LED);
wait(2);
turnLEDOff(SCORE_LED);
//Add to score
score ++;
}
//wait 300 ms before cutting mole LED off
//then pause thread for 600ms to wait for next round
waitInMilliseconds(lightDuration / 2);
turnLEDOff(led);
waitInMilliseconds(lightDuration);
}
//Flashes score with the score LED
void flashScore() {
//use for loop to flash LED as many times
//as the score that the player has
for (int i = 0; i < score; i ++) {
//blink score LED for 150ms
turnLEDOn(SCORE_LED);
waitInMilliseconds(150);
turnLEDOff(SCORE_LED);
}
}
task main() {
while (1) {
//Random led value
value = rand() % 4 + 1;
//Switch case to led that was selected
switch (value) {
case 1:
blinkLED(GreenLED1);
break;
case 2:
blinkLED(GreenLED2);
break;
case 3:
blinkLED(GreenLED3);
break;
case 4:
blinkLED(GreenLED4);
break;
}
//flash player score, then wait 1 second for next loop
flashScore();
wait(1);
}
}