2

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

0

1 Answer 1

1

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
    # [...]
Sign up to request clarification or add additional context in comments.

2 Comments

but when i do this, i get the warning that surface.blit(*bonus_text) "expected an iretable, but got none"
@marta Then you did something wrong. If you evalute if bonus_text, then bonus_text cant be None in surface.blit(*bonus_text)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.