Advertisement
kaly1

Reaction_game_C2

Nov 18th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. from gpiozero import Button,LED
  2. from time import sleep,time
  3. from random import randint
  4. from sys import exit
  5. from math import trunc
  6.  
  7. def speed_time():
  8. if float(speed) > 1:
  9. print("That's more than",speed,"seconds")
  10. elif float(speed) <= 1:
  11. print("That's more than",speed,"second")
  12.  
  13. def game_start():
  14. print("Let's begin")
  15. print("Look at the LED")
  16. print("When it turns on, press the button quickly\n")
  17.  
  18. btn = Button(17)
  19. led = LED(16)
  20.  
  21. lives = input("How many lives do you want?")
  22. game_lives = trunc(float(lives))
  23. if game_lives > 1 and game_lives < 5:
  24. print("You have",game_lives,"lives")
  25. elif game_lives >=5:
  26. print(game_lives,"is too much")
  27. print("Maximum allowed is 4")
  28. game_lives = 4
  29. print("You have",game_lives,"lives")
  30. elif game_lives <= 0:
  31. print("Haha, game over then!")
  32. exit()
  33. else:
  34. print("You have",game_lives,"life\nYou like living on the edge I see")
  35.  
  36.  
  37. speed = input("How fast in seconds do you think you can press the button?")
  38. if float(speed) > 1:
  39. print("\nYou have to press the button in less than",speed,"seconds from when it turns on")
  40. print("Otherwise, it is game over")
  41. game_start()
  42. elif float(speed) == 0:
  43. print("\nReally, 0 second. Be my guest. You'll lose")
  44. game_start()
  45. else:
  46. print("\nYou have to press the button in less than",speed,"second from when it turns on")
  47. print("Otherwise, it is game over")
  48. game_start()
  49.  
  50.  
  51. while game_lives > 0:
  52. sleep(randint(1,10))
  53. start = time()
  54. led.on()
  55. btn.wait_for_press()
  56. end = time()
  57. led.off()
  58. rct_time = end - start
  59. if rct_time >=float(speed):
  60. game_lives = game_lives - 1
  61. if rct_time > 1:
  62. print("It took you",rct_time,"seconds to press the button")
  63. speed_time()
  64. elif rct_time <= 1:
  65. print("It took you",rct_time,"second to press the button")
  66. speed_time()
  67.  
  68. if game_lives > 1:
  69. print("You have",game_lives,"lives left\n")
  70. elif game_lives == 1:
  71. print("You have 1 life left, be careful\n")
  72. else:
  73. print("You have 0 lives left.\nGame over!")
  74. else:
  75. if rct_time > 1:
  76. print("It took you", rct_time, "seconds to press the button")
  77. elif rct_time < 1:
  78. print("It took you", rct_time, "second to press the button")
  79. else:
  80. print("It took you", rct_time, "second to press the button")
  81. speed_time()
  82. print("Well done!")
  83. if game_lives > 1:
  84. print("You still have",game_lives,"lives left\n")
  85. elif game_lives == 1:
  86. print("You still have 1 life left\n")
  87. else:
  88. print("You have 0 lives left.\nGame over!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement