0

所以我试图在“场景”类中创建一个函数,以便在您按下“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”之后也能被传送几帧。

提前致谢。

4

1 回答 1

1

What i would do is create a boolean to define wheter the button was pressed or not

Here is an example:

self.pressed = False

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

if self.pressed:
    robsHouse.printText(screen)

then at then when you want the text to go away set self.pressed to False and it will stop being blitted

like this:

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

That way once the counter ends the text will dissapear

Hope that helps!

于 2013-08-27T03:20:24.983 回答