Reaction Challenge (Janosch Spohner)

reaction.gif

The Game

This is the Reaction Challenge Spencer and I built. It is a competitive button pushing game where you have to click your button as soon as your LED flashes. If you press your button too early or too late, you loose and all thee lights will light up. Then the game will reset and start off by shooting at the green player. The game is basically a one dimensional tennis game where you have to hit the yellow ball across the court. The amount of time between each flash is randomized to make it harder for the player to anticipate when to press the button. The range we chose is from 100-500 mili-seconds so it can be a very short amount of time for the player to press their button.

The Machine

We used one single state machine to control the entire game. In total there are 11 different states. One state is called LOST which is where all the LED’s light up to signify the game is over and the other ones are all used to light up one single specific LED. The state machine starts out with the first yellow LED and then goes all the way to the green LED. If the green player hits the button at the right time the last yellow LED will light up and the light will travel down to the blue. If the green player misses he goes to the LOST state. The same thing happens for the blue button and it will repeat until one of the players miss times their button press. Below is a visual representation of this process.

Visual.JPG

 

The Code

The code was pretty straight forward since we just used one single state machine, however I would like to re write it with multiple state machines because I think it would allow us to make the game better while also giving us more options and take out some of the code. Below is what two of our displayed states look like in the loop (pretty easy to understand whats going on but kinda messy).

  //display current states
  if (currentState == BLUE) {
    digitalWrite(yellow1, LOW);
    digitalWrite(yellow2, LOW);
    digitalWrite(yellow3, LOW);
    digitalWrite(yellow4, LOW);
    digitalWrite(lBlue, HIGH);
    digitalWrite(lGreen, LOW);
  }
  else if (currentState == YELLOW1) {
    digitalWrite(yellow1, HIGH);
    digitalWrite(yellow2, LOW);
    digitalWrite(yellow3, LOW);
    digitalWrite(yellow4, LOW);
    digitalWrite(lBlue, LOW);
    digitalWrite(lGreen, LOW);
  }

Below is how we updated what our current state for three of the states. Basically if you were in the blue state and you were the blue player and you pressed the button it would go to the next state (yellow 1). If the player did not hit the button on time the game would be over and go to the LOST state. The timing as you can see was randomized every time we transitioned to another state. This makes for a jumping effect where the Lights don’t flash at even intervals which makes it very hard to judge when to press the button.

  //update states based on inputs
  if (currentState == BLUE) {
    if (pressedBlue) {
      currentState = YELLOW1;
    }
    if (currentTime - startTime > pause) {
      currentState = LOST;
      startTime = currentTime;
      pause = random(100, 500);
    }

  }

  if (currentState == YELLOW1) {
    if (pressedGreen) {
      currentState = LOST;
    }
    if (currentTime - startTime > pause) {
      currentState = YELLOW2;
      startTime = currentTime;
      pause = random(100, 500);
    }
  }
  if (currentState == YELLOW1BACK) {
    if (pressedBlue || pressedGreen) {
      currentState = LOST;
    }
    if (currentTime - startTime > pause) {
      currentState = BLUE;
      startTime = currentTime;
      pause = random(100, 500);
    }
  }

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s