pygame.time.set_timer() - 4 2 地板点击 [英] pygame.time.set_timer() - 4 2 the floor click

查看:36
本文介绍了pygame.time.set_timer() - 4 2 地板点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这不是每 444 毫秒播放一次我的 click.wav?它似乎只是随机播放.

Why doesn't this play my click.wav every 444ms? it just seems to play it at random intervals.

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()

如果有人知道我可以如何更轻松地做到这一点,那就太好了.

and if anyone knows how I could do this more easily that would be good.

谢谢!

@JFSebeastian:这是代码的输出:

@JFSebeastian: Here's the output for 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) 这就是为什么它看起来是随机的.

Use event.type == pygame.USEREVENT + 1, otherwise the event may be generated for other reasons (whatever 1 event type corresponds to in pygame) that is why it appears random.

代码的输出显示时间间隔大多为440±10 ms 除了 910, 7 对.

The output for the code shows that the time intervals are mostly 440±10 ms with the exception of 910, 7 pair.

±10 毫秒听起来很正常.要获得更严格的计时,您可以使用 clock.tick() 而不是 clock.tick(60).

±10 milliseconds for a timer sounds normal for fps=60. To get tighter timings, you could use clock.tick() instead of clock.tick(60).

910, 7 对表明 set_timer()/tick() 可能使用 time.sleep() 模拟某处.time.sleep() 可能会比指定的睡眠时间更长,这就是计时器可能会跳过节拍的原因.尝试不应该休眠的 clock.tick_busy_loop() ,看看是否可以重现跳过.

910, 7 pair suggests that set_timer()/tick() might use a time.sleep() analog somewhere. time.sleep() may sleep more than specified that is why the timer may skip a beat. Try clock.tick_busy_loop() that shouldn't sleep and see if you can reproduce the skips.

这篇关于pygame.time.set_timer() - 4 2 地板点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆