We could just use the time.sleep() function to wait the random amount of time determined for the countdown. Something like this:
# Wait a random amount of time count_time = random.randrange(SHORTEST_DELAY, LONGEST_DELAY+1) time.sleep(count_time)
However, we need to monitor the buttons during the countdown to make sure one of the players did not cheat (misdraw). We can do this by using a while loop instead of time.sleep() for the countdown and polling the buttons inside the loop. That would look something like the following, note that there is now no use of time.sleep().
# Wait a random amount of time
count_time = random.randrange(SHORTEST_DELAY, LONGEST_DELAY+1)
start_time = time.monotonic()
while time.monotonic() - start_time < count_time:
# Check if player draws too soon
if cpx.button_a:
show_outcome(1, False)
if cpx.button_b:
show_outcome(2, False)
But what's the show_outcome() function? Well, we'll talk about that next.
Page last edited March 08, 2024
Text editor powered by tinymce.