【问题标题】:Pygame - Blitting Text even after the Key is PressedPygame - 即使在按下键后也会显示文本
【发布时间】:2013-08-29 15:58:05
【问题描述】:

所以我正在尝试在“场景”类中创建一个函数,以便在您按“z”时弹出文本并继续播放一段时间。

如果我使用 pygame.key.get_pressed() 它只会在按下 Z 时闪击。我希望它在按下 Z 时弹出并继续在屏幕上停留一段时间。

##This is inside the "scene" class##

def printText(self, surface):
    if self.counter < 20:
        text = pygame.font.SysFont("Pixelated Regular", 30)
        label = text.render("Hello", 0, (0,0,0,))
        surface.blit(label, (100,100))
        self.counter += 1


##This is inside the main##
if key[pygame.K_z]:
        robsHouse.printText(screen)

以防我之前没有说清楚:我基本上希望它的文本即使在我放开“z”之后也能被传送几帧。

提前致谢。

【问题讨论】:

  • 那么你的问题是什么?

标签: python loops text pygame blit


【解决方案1】:

我要做的是创建一个布尔值来定义按钮是否被按下

这是一个例子:

self.pressed = False

if key[pygame.K_z]:
    self.pressed = True

if self.pressed:
    robsHouse.printText(screen)

然后,当您希望文本消失时,将 self.pressed 设置为 False,它将停止被 blitted

像这样:

def printText(self, surface):
    if self.counter < 20:
        text = pygame.font.SysFont("Pixelated Regular", 30)
        label = text.render("Hello", 0, (0,0,0,))
        surface.blit(label, (100,100))
        self.counter += 1
    else:
        self.pressed = False

这样,一旦计数器结束,文本就会消失

希望有帮助!

【讨论】:

  • 由于文本没有变化,可以渲染一次,对缓存的Surface进行blit。如果您想要一个自动执行此操作的类,请参阅:stackoverflow.com/a/15516132/341744
  • 是的,将渲染从循环中取出,这样您就不会多次渲染它,而是将 blitting 保持在循环中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-17
  • 1970-01-01
  • 2022-06-27
  • 1970-01-01
  • 2021-08-22
  • 1970-01-01
  • 2021-04-02
相关资源
最近更新 更多