0

So I'm trying to make a function inside a "scene" class to have text pop up whenever you press "z" and continue to be blitted for a while.

If I use the pygame.key.get_pressed() it only blitz while Z is pressed. I want it to pop up when Z is pressed and continue to stay on screen for a while.

##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)

Just in case I didnt make it clear before: I basically want it the text to be blitted for a couple of frames even after I let go of "z".

Thanks in advance.

Ronnie Sena
  • 7
  • 1
  • 2

1 Answers1

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!

Serial
  • 7,925
  • 13
  • 52
  • 71
  • Since the text doesn't change, you can render it once, and blit the cached Surface. If you'd like a class that does this automatically, see: http://stackoverflow.com/a/15516132/341744 – ninMonkey Aug 27 '13 at 03:51
  • yeah take the rendering out of the loop so you dont render it multiple times but keep the blitting in the loop – Serial Aug 27 '13 at 03:53