【发布时间】: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