Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

implement this game in C code LED Quick-Reaction Game Part 2 is exactly the same

ID: 3874540 • Letter: I

Question

implement this game in C code

LED Quick-Reaction Game Part 2 is exactly the same a project 3, but is repeated here for your convenience. Build the system of Figure 1 by modifying your existing breadboard. Although they are not shown in Figure 1, be sure to retain the PICkit 3 programmer connections, power supply circuitry, reset, etc. Layout your circuit so that the LEDs form a straight line from D1 to D4. Create a new project to program a Quick-Reaction LED game. Add a new source file and name it QuickReact.asm. This should be the only source file in your project. For this program we will modify the program you used in Part 1.   Let the SW1 become the “Start” button, and the SW2 button should be the “Player input button”. Initially, all four LEDs should be off. Upon pressing and releasing the Start button, the LEDs should light in sequence from left to right, changing state every 0.25 seconds: D1 lights for 0.25 seconds, then turns off for 0.25 seconds, then D2 lights for 0.25 seconds, etc. The player should press the Player input button sometime during the 0.25 second interval while the 4th LED is lit, attempting to press the button as close to the start time of the LED lighting as possible.  

The game should indicate how quickly the Player input button was pressed using the LEDs. If the player pressed the button within 50ms of the 4th LED turning on, the LEDs (D4D3D2D1) should display the value 1 (b’0001’). If the player pressed the button within 100ms, then the LEDs should display 2 (b’0010’). If pressed within 150ms then display 3 (b’0011’); if pressed within 200ms then display 4 (b’0100’); if pressed within 250ms then display 5 (b’0101’). If the Player takes more than 250ms to press SW2, then display 7 (b’0111’). In order to prevent a player from cheating by holding down the Player input button prior to the 4th LED lighting, the program must check if the button is already pressed when the 4th LED lights. If this occurs, all four LEDs should be lit to indicate an early press. The 4th LED should remain lit for 250ms regardless of whether SW2 is pressed. The LEDs should all be off for 250ms following the 4th LED lighting before displaying the results. The LEDs should then maintain their state until a new game is started with the Start button.  

Explanation / Answer

from gpiozero import LED, Button
from time import sleep
from random import uniform

led = LED(4)
right_button = Button(15)
left_button = Button(14)

led.on()
sleep(uniform(5, 10))
led.off()

def pressed(button):
        print(str(button.pin.number) + ' won the game')

right_button.when_pressed = pressed
left_button.when_pressed = pressed

left_name = input('left player name is ')
right_name = input('right player name is ')

def pressed(button):
      if button.pin.number == 14:
          print(left_name + ' won the game')
      else:
          print(right_name + ' won the game')
      exit()