+-

为什么这不会每444毫秒播放一次我的click.wav?它似乎只是随机播放.
import pygame
pygame.init()
size = (700, 500)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("444ms click")
done = False
clock = pygame.time.Clock()
noise = pygame.mixer.Sound("click.wav")
pygame.time.set_timer(1, 444)
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == 1:
noise.play()
clock.tick(60)
pygame.quit()
如果有人知道我怎么能更容易地做到这一点会很好.
谢谢!
@JFSebeastian:这是the code的输出:
447
436
443
430
449
431
448
432
910
7
407
447
442
432
446
431
448
432
450
432
446
472
最佳答案
使用event.type == pygame.USEREVENT 1,否则可能由于其他原因(无论1个事件类型对应于pygame)生成事件,这就是它出现随机的原因.
the code的输出显示时间间隔大多为440±10 ms,但910,7对除外.
对于fps = 60,定时器的±10毫秒听起来是正常的.要获得更紧密的时序,可以使用clock.tick()而不是clock.tick(60).
910,7对表明set_timer()/ tick()可能在某处使用time.sleep()模拟. time.sleep()可能会睡眠超过指定的时间,这就是定时器可能跳过节拍的原因.尝试不应该睡觉的clock.tick_busy_loop(),看看你是否可以重现跳过.
点击查看更多相关文章
转载注明原文:python – pygame.time.set_timer() – 4 2楼层点击 - 乐贴网