简体   繁体   中英

How to delay a thing in python/pygame while not delaying other things?

I am very new to python and I am trying out pygame. I have a variable, which i want to make True, wait 1 second, then make False. I have seen the time.sleep() command, but it delays the whole program, when I only want to delay this, while still doing other things. Does anyone know how to do this?

from time import sleep, time

started = time() # Current time saved, say 13:00
sleep(60) # <- This will freeze stuff
ended = time() # Ended at 13:01

print(started-ended,'seconds were slept')

Now, the above example shows how you can utilize time() to check for certain durations. The same method can be used in a loop to measure time as long as the starting time was placed outside of the loop like so:

Solution:

from time import sleep, time
last_time = time()
while 1:
    if time()-last_time > 1:
        print('One second has passed')
        last_time = time()
    else:
        ## The print() and sleep() is just here for debug purposes
        ## And in a graphical application you can remove the two below.
        print('Still waiting for one second to pass')
        sleep(0.25)

Why is this a solution?

First of all, I like to get control over things on as low a level as possible without wasting time on confusing code (in python, "as low as possible" is not necessarily at a lower level but you can control certain aspects of functionality by not using pre-packaged modules).

Almost all graphical modules for Python (Pygame, Pyglet, Panda etc) have internal timers, they usually correlate with the timing in the rendering process and if not it's still a built-in function to control timings with lack of precision and flexibility.

Now they are good for basic stuff I must agree, but as soon as you want to do multiple things simultaneously or even use timing-loops outside of the graphical library you're better off keeping it simple and clean with a loop using native time() .. Especially if you want to go down to micro-seconds (not counting in errors due to hardware and OS implementations) you also need to keep the built-in timers away from the table.

Third but not least, this will help you do actual programming instead of relying on modules for everything because sooner or later, you'll put your programmer mind to the test and even the smallest things such as a timer-loop or equation setup will help you solve problems in the long run.

Best, use threading feature of python. This will automatically call the func after 2 seconds, even though rest of the program will run without any delay

import threading 

def func(): 
    print("2 seconds finished") 

timer = threading.Timer(2.0, func)
timer.start()

Also can use timer.cancel(), if you want to stop the task before happening.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM