Create 2 variables in global name space (or class attributes if you use classes). The first on states the bonus text and position (bonus_text
). The other one states the time when the text has to disappear:
bonus_text = None
bonus_end = 0
Set the variables in hit
. Use pygame.time.get_ticks()
to get the current time. Add the timespan to the current time (e.g. 3000 milliseconds).
Don't forget the global
statement, since the function writes to the variables in global namespace.
def hit():
global bonus_text, bonus_end
cz = pygame.font.SysFont("Times New Roman", size)
rend = cz.render(txt, 1, (0, 0, 0))
x = (WIDTH - rend.get_rect().width) / 2
y = (HEIGHT - rend.get_rect().height) / 4
bonus_text = (rend, (x, y))
bonus_end = pygame.time.get_ticks() + 3000 # 3000 milliseconds = 3 seconds
Continuously call draw_bonus
in the main application loop. The function draws the bonus text, if bonus_text
is set and the current time (pygame.time.get_ticks()
) is less the time point a t which text has to disappear.
def draw_bonus(txt, x, y, size):
if bonus_text and pygame.time.get_ticks() < bonus_end:
screen.blit(*bonus_text)