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?
The Program in Action
When you run fastdraw.py, the output will look like this:
Fast Draw, by Al Sweigart [email protected]
Time to test your reflexes and see if you are the fastest
draw in the west!
When you see "DRAW", you have 0.3 seconds to press Enter.
But you lose if you press Enter before "DRAW" appears.
Press Enter to begin...
It is high noon...
You took 0.3485 seconds to draw. Too slow!
Enter QUIT to stop, or press Enter to play again.
Thanks for playing!
How It Works
The input() function pauses the program while waiting for the user to enter a string. This simple behavior means that we can’t create real-time games with just input(). However, your programs will buffer keyboard input, meaning that if you pressed the C, A, and T keys before input() is called, those characters will be saved, and they’ll appear immediately once input() executes.
By recording the time just before the input() call on line 22 and the time just after the input() call on line 24, we can determine how long the player took to press ENTER. However, if they pressed enter before input() was called, the buffered enter press causes input() to return immediately (usually in about 3 milliseconds). This is why line 26 checks if the time was less than 0.01 seconds, or 10 milliseconds, to determine that the player pressed enter too soon.
1. """Fast Draw, by Al Sweigart [email protected]
2. Test your reflexes to see if you're the fastest draw in the west.
3. View this code at https://nostarch.com/big-book-small-python-projects
4. Tags: tiny, beginner, game"""
6. import random, sys, time
8. print('Fast Draw, by Al Sweigart [email protected]')
10. print('Time to test your reflexes and see if you are the fastest')
11. print('draw in the west!')
12. print('When you see "DRAW", you have 0.3 seconds to press Enter.')
13. print('But you lose if you press Enter before "DRAW" appears.')
15. input('Press Enter to begin...')
17. while True:
19. print('It is high noon...')
20. time.sleep(random.randint(20, 50) / 10.0)
22. drawTime = time.time()
23. input() # This function call doesn't return until Enter is pressed.
24. timeElapsed = time.time() - drawTime
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!')
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!')
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!')
Exploring the Program
Try to find the answers to the following questions. Experiment with some modifications to the code and rerun the program to see what effect the changes have.
What happens if you change drawTime = time.time() on line 22 to drawTime = 0?
What happens if you change timeElapsed > 0.3 on line 30 to timeElapsed < 0.3?
What happens if you change time.time() - drawTime on line 24 to time.time() + drawTime?
What happens if you delete or comment out input('Press Enter to begin...') on line 15?