Reaction Challenge (Alexandra E. Morton)

Slightly inspired by 1D Ping Pong and built off of the Back/Forth program we made in class, we created a cooperative reaction game.

The game has a starting speed and speed goal that you can set at the top of the program. The point of the program is to hit the light back and forth until the goal speed it met, at which point the WIN display will happen (in this case, all the LEDs lighting up) and the game can be restarted after a second by hitting one of the buttons. When the light is hit successfully, the speed of its movement increases by .1 secs; if the light runs off the LEDs before the player can hit it, the speed decrease by .05 secs.

img_20170206_121412

  • P1: player1(Steve) was last to hit, turn currentLed on
  •                Leads to:  (a) R2 [reduces speed],
  •                                   (b) P2 [increases speed],
  •                               or (c) Off1 [turns led off and goes to next led].
  • Off1: player1(Steve) was last to hit, turn currentLed off, then move one LED up [1->5]
  •                Leads to:  (a) P1 [turns on next led].
  • P2: player2(Jane) was last to hit, turn currentLed on
  •                Leads to:  (a) R1 [reduces speed],
  •                                   (b) P1 [increases speed],
  •                               or (c) Off2 [turns led off and goes to next led].
  • Off2: player2(Jane) was last to hit, turn currentLed off, then move one LED down [5->1]
  •                Leads to:  (a) P1 [turns on next led].
  • R1: start: LED is on player1’s side, Steve must be hit to begin game,
  • or miss: if player2(Jane) hit the light in player1’s direction, but the light ran off the board before player1(Steve) hit, reduce speed.
  •                Leads to:  (a) R1 [sets light in motion].
  • R2: miss: if player1(Steve) hit the light in player2’s direction, but the light ran off the board before player2(Jane) hit, reduce speed.
  •                Leads to:  (a) R1 [sets light in motion].
  • WIN: gameGoal was reached – intact WIN display, may restart game by hitting either Steve or Jane after 1 sec.
  •                Leads to:  (a) P1 [restarts game].

 

One of the things that made this difficult was turning this code from Back/Forth:

for (int pin = led5 ; pin >= led1 ; pin = pin – 1 ) {

digitalWrite(pin, HIGH);

delay(100);

digitalWrite(pin, LOW);

}

Into a state function that could use millis(). As it is, we ended up with this [not exact code, only the relevant parts]:

… if (gameState == PLAYER_TWO) {//player2 was last to hit
else if ( gameTime – startTime > gameSpeed) {
gameState = PLAYER2_OFF;
startTime = gameTime;
}
else if (gameState == PLAYER2_OFF) { //turns off current LED and moves to the next one down
if ( gameTime – startTime > gameSpeed) {
currentLed = currentLed – 1;
gameState = PLAYER_TWO;
startTime = gameTime;
}
} …

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