0

So I am currently trying to make a clicking game for a final project but I have no clue how to make the timer go during the game and stop the game once the timer is up.

I've tried to implement a sleep timer into the program like

import time

seconds = 60

for i in range(seconds):
    print(str(seconds - i) + " seconds remain")
    time.sleep(1)

but it doesn't go simultaneously with the clicking game. The code I made for the clicking game is:

def display(x,y):
    global numClicks
    clear()
    numClicks +=1
    drawString("Number of clicks: "+str(numClicks),
100,150,"Arial",28,"normal")
    update()

numClicks = 0
beginGrfx(800,500)
onscreenclick(display)
endGrfx()

The clicking game works and the timer starts at 60 seconds but it doesn't count down after that. Is there a different timer code I should be using or what?

  • I would recommend you using threads. You can checkout this question https://stackoverflow.com/q/34562473/11335072 – palvarez Jun 06 '19 at 19:25

1 Answers1

0

From that same module, you can use time.time() method to get the current time. And then assuming you know how long the timer will run for, you can add to that amount and have a while loop run the program until that time is up. E.g:

start_time = time.time()
end_time = start_time + 10 # ten second timer

while time.time() < end_time:
    # run game logic...
Calvin Godfrey
  • 2,171
  • 1
  • 11
  • 27