Pygame - Blitting Text even after the Key is Pressed

279 views Asked by At

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.

1

There are 1 answers

2
Serial On BEST ANSWER

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!