martae
martae

Reputation: 69

Pygame, how to display text on transparent layer?

I'm trying to display text on a separated transparent layer after hitting a bonus. The screen blits for a milisecond and the game continues. Where did i make a mistake?

WIDTH = 500
HEIGHT = 600

screen = pygame.display.set_mode((WIDTH, HEIGHT))
surface = pygame.surface.Surface((WIDTH, HEIGHT))

def hit():
    screen.blit(surface, (0, 0))
    bonus = BONUSFONT.render("+3 points!", 1, (0, 0, 0))
    bonus_text = (bonus, (200, 150))
    bonus_end = pygame.time.get_ticks() + 3000
    if bonus_text and pygame.time.get_ticks() < bonus_end:
        surface.blit(*bonus_text)

I checked almost all the questions about surfaces and layers, but nothing helped me

Upvotes: 2

Views: 100

Answers (1)

Rabbid76
Rabbid76

Reputation: 210890

Create 2 variables in global name space:

bonus_text = None
bonus_end = 0

hit sets bonus_end and hast be called once, when the "hit" happens:

def hit():
    global bonus_end, bonus_text  
    bonus = BONUSFONT.render("+3 points!", 1, (0, 0, 0))
    bonus_text = (bonus, (200, 150))
    bonus_end = pygame.time.get_ticks() + 3000

Create a function which shows the bonus text_

def show_bonus():
    if bonus_text and pygame.time.get_ticks() < bonus_end:
        surface.blit(*bonus_text)

Call the function continuously in the main application loop:

while True:

    # [...]

    # show bonus text
    show_bonus()

    # update display
    # [...]

Upvotes: 1

Related Questions