Help with Game

I am a beginner at programming and using micro-controllers. As a first project, I decided to make a two player reaction game, where players compete against each other to press a button first when the GO Led flashes at a random time. However, I’m having problems with making a while loop, so that the person that hits the button first wins. Any additional help or pointers on my program would be appreciated, Cheers

//Reaction game, The game starts with the buttons flashing, then the GO button turns on at a random time, at which moment the person who hits the button first wins, ie. their LED lights up. 

int player1 = 7;//Set constants
int player2 = 8;//etc.
int GO = 6;
int Button = 9;
int Button2 = 10 ;
int PLAYER1BUTTON= 0;
int PLAYER2BUTTON = 0;
int var;
void setup ()
{
  //Set LEDs as Outputs and Buttons as Inputs
pinMode (player1, OUTPUT);
pinMode (player2, OUTPUT) ;
pinMode (GO, OUTPUT);
pinMode (Button, INPUT);
pinMode (Button2, INPUT);
}    
void loop ()//Main program

{  
  
randomSeed(analogRead(5));
   long timedelay = random(5000); // Sets the time delay radomly, up to 5 seconds.
   unsigned long timenow = millis();
   long timego = timenow + timedelay;
   digitalWrite (GO, HIGH);
   delay (1000);
   digitalWrite (GO, LOW);
}  

//Check the state of the button
while (var < 200)
{
 PLAYER1BUTTON = digitalRead(Button);
 PLAYER2BUTTON = digitalRead(Button2);
  var++;
  }
//If player 1 and Player 2 don't press, both Leds are off
 if (PLAYER1BUTTON == LOW && PLAYER2BUTTON == LOW) 
 {
  digitalWrite (player1, LOW);
  digitalWrite (player2, LOW);
 }
  if (PLAYER1BUTTON == HIGH && PLAYER2BUTTON == LOW)//If player 1 is faster than player 2, player 2 led lights up
  {
  digitalWrite(player1,HIGH);
  delay (2000);
  digitalWrite (player1, LOW);
  }
   if (PLAYER1BUTTON == LOW && PLAYER2BUTTON == HIGH)//If player 2 is faster than player 1, player 1 led lights up
   
  {
  digitalWrite(player2,HIGH);
  delay (2000);
  digitalWrite(player2, LOW);
  }
 
}

Don't do it that way, it will cause many problems. Instead, look into Interrupts and how they work.

All you need to do is make the two scores into there own functions, and just have the two interrupts in the loop(). If one is triggered, it will go to the function and display the winner. You may also want a reset feature, either with a button or a timer, your choice.

Your loop that gathers the digitalRead of the buttons doesn’t do anything with that information. It might help to break out of that loop as soon as one or the other of the buttons is pushed. Otherwise you’re just ignoring the button states and checking the button again.

Try this modification:

//Check the state of the button
var = 0;
while (var < 200)
{
 PLAYER1BUTTON = digitalRead(Button);
 PLAYER2BUTTON = digitalRead(Button2);
  var++;
  if (PLAYER1BUTTON == HIGH || PLAYER2BUTTON == HIGH)
    break;
  }

You will probably need a number bigger than 200. MUCH bigger. Like 30000. In fact, you might also need to put a delay(1) in the while loop.

When I try to add the change and verify, the error that expected constructor, destructor, or type conversion before = token shows up

Try removing the } on line 31.

Actually I did some tinkering and it worked, Thank you very much for your help. Its was much appreciated and once again thank you for taking time out of your day. Also thanks to HazardsMind for the suggestion, I have a reset button connected and I'll try to write it using your suggestion as well

Did you fix the random part at the beginning of the loop() function? Because it looks like you're calculating the random OK, but then ignoring it and using a set 1000 millisecond delay.

Yup, I realized my mistake and changed it to use the random instead, Thanks for pointing that out, Getting used to the nuisances of programming

Getting used to the nuisances of programming

My new "Phrase du Jour"