【问题标题】:Is there a way to control the FPS in Pygame Zero?有没有办法控制 Pygame Zero 中的 FPS?
【发布时间】:2019-01-18 09:13:27
【问题描述】:

我正在使用 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 python-3.x raspberry-pi pygame


    【解决方案1】:

    在您的运行循环中,只需使用: 只需使用clock.tick(60)#或任何你想要的fps

    【讨论】:

    • 所以我应该把“clock.tick(60)”放在“def update()”循环中?
    • 你有一个while循环可以无限循环遍历它自己吗?
    • 不,我使用的是 pygame 零,而不是常规的 pygame。所以我相信它的工作方式有点不同。但我注意到,当我通过 IDLE 而不是 Visual Studio Code 运行代码时,它似乎运行得更好。所以我希望这可能是问题所在。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-30
    • 1970-01-01
    • 2014-04-26
    • 2018-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多