我正在使用pygame zero进行一个简单的项目,到目前为止,我已经能够显示一组图像以形成非常简单的动画。我以 60 fps 的速度将视频渲染成.png序列。似乎 pygame zero 渲染它们的速度比这快一些,我只是想知道是否有办法将 FPS 锁定为 60,以便一切都按照我的期望呈现。我有一些声音文件,我想与图像序列同步,所以让它们以恒定的 FPS 渲染会非常有帮助。
我还注意到声音在播放后不断循环,所以我试图在播放后停止它,但声音在最后中断,因为动画似乎过早完成。
这是我到目前为止的代码:
import pgzrun
WIDTH = 480
HEIGHT = 360
# boot1 graphics
boot1 = Actor('boot1_1')
boot1.frame = 1
boot1.active = True
# boot2 graphics
boot2 = Actor('boot2_1')
boot2.frame = 1
boot2.active = False
# overlay
overlay = Actor("overlay_a")
def update_boot1():
if boot1.active:
boot1.x = WIDTH/2
boot1.image = "boot1_{}".format(boot1.frame)
boot1.frame += 1
else:
boot1.x = 1000
if boot1.frame > 59:
#boot1.frame = 1
boot2.active = True
update_boot2()
boot1.active = False
def update_boot2():
if boot2.active:
boot2.x = WIDTH/2
sounds.boot1.play()
boot2.image = "boot2_{}".format(boot2.frame)
boot2.frame += 1
else:
boot2.x = 1000
if boot2.frame > 233:
boot2.frame = 233
boot2.active = False
sounds.boot1.stop()
def draw():
screen.clear
screen.fill((0, 75, 0))
boot2.draw()
boot1.draw()
overlay.draw()
# running the animation
def update(dt):
update_boot1()
update_boot2()
pgzrun.go()
我也没有找到一种方法来"卸载"图像序列,或者在我完成它们时使它们不可见,所以我只是用".x = 1000"将它们扔到一边。
也许有点晚了,但我已经找到了答案。如果您要转到您的python-folder/site-packages/pgzero/game.py并编辑第235行。它应该是"dt = clock.tick(60)/1000.0",其中 60 是 fps。
我已经在 chromebook(我知道很蹩脚)上用 python 3.9.1 对其进行了测试,这实际上确实改变了帧速率,甚至需要小数。
希望这对您或任何需要这个的人有所帮助。
在你的运行循环中,只需使用: 只需使用 Clock.tick(60)#or 您想要的任何 fps