2019-06-08 05:29:34

I'm wondering the best way to make a timer in python that can be paused and resumed. The agk3 comes with a timer but its not able to be paused.
Thanks in advance for any help.

2019-06-08 10:43:41 (edited by magurp244 2019-06-08 10:47:05)

You can use the time module. What you do is set a baseline time variable, then compare it to the current time to see how much time has passed. For example:

import time

baseline = time.time()
while True:
    if time.time() - baseline == 1:
        print("one second has passed")
        break

Now, to pause it you can just make baseline equal time.time() preemptively, like so:

import time

baseline = time.time()
pause = True
while True:
    if pause == True:
        baseline = time.time()
    elif time.time() - baseline == 1:
        print("one second has passed")
        break

This ensures that any checks you have that requires a certain amount of time to trigger won't go off, effectively pausing them.

-BrushTone v1.3.3: Accessible Paint Tool
-AudiMesh3D v1.0.0: Accessible 3D Model Viewer

2019-06-08 15:32:27

Should've said earlier this is for a game. So if I put this as a timer class it wouldn't lag? I want to make a timer class that I can use with as less lag as possible.

2019-06-09 09:57:44

That depends. The time module runs straight from the system clock, so it shouldn't lag necessarily, but lag is often caused when calculations, or the number of calculations is more than the processor can handle in a given cycle. Part of the practice of optimization is to find parts of code that take a lot of cycles and optimize them to use less or run more efficiently, thus reducing lag. The time module can be used to help you pace the number of calculations and calls being used so it doesn't run the processor full throttle, and is a common technique in engine development. You could also use the timeit module for benchmarking how long it takes a piece of code to execute in a given cycle to help optimize.

Fun fact, most older dos games didn't use time management and would use loops to go as fast as the processors could allow, so while they played fine on something like a 386 at the time, as computers became more powerful they would run unplayably fast without emulation like Dosbox to throttle the number processor cycles fed to them.

-BrushTone v1.3.3: Accessible Paint Tool
-AudiMesh3D v1.0.0: Accessible 3D Model Viewer

2019-06-12 10:54:21 (edited by r3dux 2019-06-12 16:31:03)

Hi kingzombie,

From reading your question, I'm not sure if you want a timer to just wait a set amount of time while nothing else happens or if you want to perform some event a set amount of time after something else happens.

If you don't need to do anything and just want to wait for a period of time to elapse then you can just call sleep(3) etc to sleep (i.e. wait) for three seconds in this example. This is what's called a blocking operation because the code doesn't continue until it has waited for the specified duration.

If you want other things to happen while waiting for a period of time to elapse then you need a non-blocking way of doing this. Here's a quick example of a rubbish programming joke I knocked together for you:

import threading
from time import sleep

def answer():
    print("Java.")

print("Knock knock?")
print("Who's there?")

timer = threading.Timer(10.0, answer)
timer.start()  # After 10 seconds, the 'answer' function will be called

## (in the meanwhile you can do other stuff...)
for loop in range(1, 11):
    print('.')          # Print a full stop
    sleep(1)          # Sleep for 1 second

This produces the output:

Knock knock?
Who's there?
.
.
.
.
.
.
.
.
.
.
Java.

The knock-knock / who's there appears immediately, then we wait 10 seconds, printing one full-stop per second, then finally the 10 second timer expires and the 'answer' function is called to print the punchline: "Java".