We could just use the time.sleep() function to wait the random amount of time determined for the countdown. Something like this:

Download: file
# 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().

Download: file
# 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.

This guide was first published on Dec 29, 2016. It was last updated on Dec 29, 2016.
This page (The Countdown) was last updated on Sep 22, 2020.