我如何反复延迟随机选择? [重复]

问题描述 投票:0回答:2
    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 = [100,200,300]

我需要知道如何对“过程”进行编程,使其在“ 0.5”秒后重复自身,然后再次睡眠“ 0.5”秒,以此类推,直到永远重复此过程。

python random repeat
2个回答
0
投票

如果要以0.5秒的延迟将process置于无限循环中,则可以执行以下操作:

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

0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.