Fast Draw
This program tests your reaction speed: press ENTER as soon as you see the word DRAW. But careful, though. Press it before DRAW appears, and you lose. Are you the fastest keyboard in the west?
fast_draw.py
1"""Fast Draw, by Al Sweigart al@inventwithpython.com
2Test your reflexes to see if you're the fastest draw in the west.
3This code is available at https://nostarch.com/big-book-small-python-programming
4Tags: tiny, beginner, game"""
5
6import random, sys, time
7
8print('Fast Draw, by Al Sweigart al@inventwithpython.com')
9print()
10print('Time to test your reflexes and see if you are the fastest')
11print('draw in the west!')
12print('When you see "DRAW", you have 0.3 seconds to press Enter.')
13print('But you lose if you press Enter before "DRAW" appears.')
14print()
15input('Press Enter to begin...')
16
17while True:
18 print()
19 print('It is high noon...')
20 time.sleep(random.randint(20, 50) / 10.0)
21 print('DRAW!')
22 drawTime = time.time()
23 input() # This function call doesn't return until Enter is pressed.
24 timeElapsed = time.time() - drawTime
25
26 if timeElapsed < 0.01:
27 # If the player pressed Enter before DRAW! appeared, the input()
28 # call returns almost instantly.
29 print('You drew before "DRAW" appeared! You lose.')
30 elif timeElapsed > 0.3:
31 timeElapsed = round(timeElapsed, 4)
32 print('You took', timeElapsed, 'seconds to draw. Too slow!')
33 else:
34 timeElapsed = round(timeElapsed, 4)
35 print('You took', timeElapsed, 'seconds to draw.')
36 print('You are the fastest draw in the west! You win!')
37
38 print('Enter QUIT to stop, or press Enter to play again.')
39 response = input('> ').upper()
40 if response == 'QUIT':
41 print('Thanks for playing!')
42 sys.exit()