Your game has a main loop. (Yes, it does.)
Each time through the loop when you go to check state, move the players, redraw the screen, etc., you check the time left on your timer. If at least 1 second has elapsed, you print out your "seconds remaining" quip. If At least 30 seconds has elapsed, you trigger whatever your action is.
Answer from tylerl on Stack OverflowYour game has a main loop. (Yes, it does.)
Each time through the loop when you go to check state, move the players, redraw the screen, etc., you check the time left on your timer. If at least 1 second has elapsed, you print out your "seconds remaining" quip. If At least 30 seconds has elapsed, you trigger whatever your action is.
You can't do it without blocking or threading unless you are willing to lose precision...
I'd suggest sometime like this, but threading is the correct way to do this...
import time
counter = 31
start = time.time()
while True:
### Do other stuff, it won't be blocked
time.sleep(0.1)
print "looping..."
### When 1 sec or more has elapsed...
if time.time() - start > 1:
start = time.time()
counter = counter - 1
### This will be updated once per second
print "%s seconds remaining" % counter
### Countdown finished, ending loop
if counter <= 0:
break
or even...
import time
max = 31
start = time.time()
while True:
### Do other stuff, it won't be blocked
time.sleep(0.1)
print "looping..."
### This will be updated every loop
remaining = max + start - time.time()
print "%s seconds remaining" % int(remaining)
### Countdown finished, ending loop
if remaining <= 0:
break
python - How do I make a time delay? - Stack Overflow
raspbian - delay Without Sleep functions error - Raspberry Pi Stack Exchange
How to enable delay in python script instead of time.sleep(t) - Editor Scripting - Epic Developer Community Forums
How to delay without freeze in Python?
Videos
Hi, Im fairly new to python and programming in general. Was wondering if theres a way to create a time delay without the use of time.sleep() as Im only allowed to use the os module and datetime module in my assignment.
This delays for 2.5 seconds:
import time
time.sleep(2.5)
Here is another example where something is run approximately once a minute:
import time
while True:
print("This prints once a minute.")
time.sleep(60) # Delay for 1 minute (60 seconds).
Use sleep() from the time module. It can take a float argument for sub-second resolution.
from time import sleep
sleep(0.1) # Time in seconds
I am creating a game with pygame, I am encountering a problem with generating enemies ships. I need to generate a new ship every 5 seconds but the problem that when I use sleep method, the whole game freezes. I tried some other methods like threading.thread and threading.Timer but also didn't work with me. What's the solution for this? Or at least any other alternative method. Thanks
def generate_enemy(so_settings, screen, ship, enemies):
#generate = threading.Timer(5.0, create_enemy(enemies, so_settings, screen, ship))
generate = threading.Thread(target=create_enemy(enemies, so_settings, screen, ship))
generate.start()
for enemy in enemies.sprites():
enemy.draw_enemy()
update_enemy(ship, enemies) def create_enemy(enemies, so_settings, screen, ship):
time.sleep(5) new_enemy = Enemy(so_settings, screen, ship) enemies.add(new_enemy) ----> sprite group
Interpreting your description literally, you need to put the print statement before the call to func2().
However, I'm guessing what you really want is for func2() to a background task that allows func1() to return immediately and not wait for func2() to complete it's execution. In order to do this, you need to create a thread to run func2().
import time
import threading
def func1():
t = threading.Thread(target=func2)
t.start()
print("Do stuff here")
def func2():
time.sleep(10)
print("Do more stuff here")
func1()
print("func1 has returned")
You could use threading.Timer:
from __future__ import print_function
from threading import Timer
def func1():
func2()
print("Do stuff here")
def func2():
Timer(10, print, ["Do more stuff here"]).start()
func1()
But as @unholysampler already pointed out it might be better to just write:
import time
def func1():
print("Do stuff here")
func2()
def func2():
time.sleep(10)
print("Do more stuff here")
func1()