【问题标题】:slow framerate in pygame only during a specific function or similar effect?仅在特定功能或类似效果期间,pygame 中的帧速率变慢?
【发布时间】:2021-09-12 17:33:24
【问题描述】:

有没有办法仅在特定功能运行时降低程序的帧速率?我创建了一个类,可以一次在屏幕上呈现一个字符来模拟打字。正如预期的那样,每个字符都以帧速率的速度渲染,这不是我想要的效果。更改帧率会有所帮助,但我不想影响我可能拥有的任何其他动画。有没有办法做到这一点,或者可能是我没有想到的解决方法? pygame.wait 挂起整个程序。

# simulate printing text one character at a time on the screen
class TextPrint:
    def __init__(self, xpos, ypos, fontSize, fontColor, string, gameSurface):
        self.xpos = xpos
        self.ypos = ypos
        self.font = pygame.font.SysFont('Lucida Console', fontSize)
        self.fontColor = fontColor
        self.displayText = string
        self.active = False # is the text area currently in use
        self.activeIndex = 0 # index that is being added to current render
        self.gameSurface = gameSurface
    
    def update(self):
        self.surf = self.font.render(self.displayText[:self.activeIndex], True, self.fontColor)
        self.rect = self.surf.get_rect(x=self.xpos,y=self.ypos)
        if self.activeIndex > len(self.displayText): pass
        else: self.activeIndex += 1

    def draw(self):
        self.gameSurface.blit(self.surf, self.rect)

【问题讨论】:

  • 您不必每帧都更改动画。设置一个计时器,在其中将增量时间(如 pygame.time.Clock.tick() 的输出)添加到变量,当它超过阈值时,将其重置为零并将 activeIndex 提前 1。

标签: python string pygame frame-rate


【解决方案1】:

也许你可以尝试的是,有一个像count = 50 这样的变量,然后有一个函数在游戏循环中调用一次并将count 的值减1,然后当count 有一个值时0,然后你可以输入一个字符,然后将count重置为50,然后重复整个过程,直到打印出所有文本。

这是我关于堆栈溢出的第一个答案,我回答这个是因为我已经实现了这样的事情。

【讨论】:

  • 您可能希望使用timer 和自定义事件而不是使用这样的计数器,并在检测到事件时添加字符
猜你喜欢
  • 2021-05-17
  • 2012-06-04
  • 2013-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-30
  • 1970-01-01
相关资源
最近更新 更多