Hello I am working on a Whack a mole project, and I'm uneable to solve the following problem.
There is a part in my code that controls the duration of the game, and I want it to run for set amount of time "period".
My functions are called inside a while loop and so to check that I don't go over the time on every while loop I run the equation (millis() - startMillis < period) as a conditional, therefore, if I go over the time my code stops and moves on to a light pattern.
The code:
startMillis = millis();
while (millis() - startMillis < period) {
int newColor = pickNewColor(mantas);
bool hit = false;
ledOn(newColor);
while (!hit && (millis() - startMillis < period)) {
if ( isWhacked(newColor) == true) {
ledOffAll();
score_buenos ++;
hit = true;
delay(200);
}
else {
boomerang(isButtonPressed2());
hit = false;
score_malos++;
}
}
delay(20);
}
If you read the code you will understand that once you are in the second while loop, unless you whack a mole, you can stay still for as long as you want and the game would never finish, but the moment you Whack another mole it finishes.
My question is the following. How can I implement a function that would stop the game the moment we have run out of time and not after a whack??
I have tried doing this:
while (millis() - startMillis < period) {
int newColor = pickNewColor(mantas);
bool hit = false;
ledOn(newColor);
while (!hit) {
if ((millis() - startMillis < period) == false){
hit = true
}
else if ( isWhacked(newColor) == true) {
ledOffAll();
score_buenos ++;
hit = true;
delay(200);
}
else if (isWhacked(newColor) == false){
boomerang(isButtonPressed2());
hit = false;
score_malos++;
}
If i leave the final "else" statment out does it mean that if no mole is whacked it will read in loop until it runs outs of time and the first if statement will brake the loop?
Do you think it's a good way of approching the problem?
Any other ideas?
Thank you for your suggestion but I think you are missing the point. My question really is if I the optio I'm suggesting can work. Will an if loop run forever if there is no else statement at the end??
I don't have a coding answer however if I were to write such a program, I would have to create a flow chart first.
Lately I've been using diagrams.net to make flow charts.
I realize flow charting is not as much fun as coding, however I've found in many cases looking at the flow chart of my initial program concept there were changes that can be made to significantly simplify the program.