如何重复延迟随机选择


global bullet
global ball
global ball_x
pred1 = [bullet, ball]
pred2 = random.choice(pred1)
process = image(pred2, ball_x, 10)
time.sleep(0.5)

ball_x=[1000200]

我需要知道如何对"过程"进行编程,使其在"0.5"秒后重复,然后再睡"0.5"秒钟,以此类推,永远重复这个过程。

如果你想把process放在一个0.5秒延迟的无休止循环中,你可以做这样的

while True:
process = image(pred2, ball_x, 10)
time.sleep(0.5)

首先添加import timefrom time import sleep。然后在while True:循环中执行time.sleep(0.5)

您可以尝试下面的代码,它将在睡觉和投球之间交替,然后休息。

global bullet
global ball
global ball_x
pred1 = [bullet, ball]
throw = False
while true:
pred2 = random.choice(pred1)
if throw:
time.sleep(0.5)
throw = False
else:
process = image(pred2, ball_x, 10)
time.sleep(0.5)
throw = True

最新更新