Reputation: 41
How do I make flashing text in PyGame? Here's the piece of text I'm trying to flash once every second or so:
text = pygame.font.Font("c:/windows/fonts/visitor1.ttf", 50)
textsurf, textrect = text_objects("Press Any Key To Start", text)
textrect.center = ((res_w / 2), (res_h / 2))
screen.blit(textsurf, textrect)
Upvotes: 4
Views: 6563
Reputation: 1
I did the same thing as @BlackJack https://stackoverflow.com/users/3815611/blackjack but I made the off_text_surface transparent:
from __future__ import absolute_import, division, print_function
from itertools import cycle
import pygame
VISITOR_TTF_FILENAME = 'pixelFont.ttf'
BLINK_EVENT = pygame.USEREVENT + 0
empty = (255,255,255,0)
def main():
pygame.init()
try:
screen = pygame.display.set_mode((800, 600))
screen.fill((255,255,255))
screen_rect = screen.get_rect()
clock = pygame.time.Clock()
font = pygame.font.Font(VISITOR_TTF_FILENAME, 50)
on_text_surface = font.render(
'Press Any Key To Start', True, pygame.Color('green3')
)
blink_rect = on_text_surface.get_rect()
blink_rect.center = screen_rect.center
off_text_surface = pygame.Surface(blink_rect.size)
off_text_surface.fill(empty)
blink_surfaces = cycle([on_text_surface, off_text_surface])
blink_surface = next(blink_surfaces)
pygame.time.set_timer(BLINK_EVENT, 1000)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
if event.type == BLINK_EVENT:
blink_surface = next(blink_surfaces)
screen.blit(blink_surface, blink_rect)
pygame.display.update()
clock.tick(60)
finally:
pygame.quit()
if __name__ == '__main__':
main()
Upvotes: 0
Reputation: 4689
In order to have it flashing you'll have to draw it for a second then delete it for a second repeat this over and over again. For timing the blinking a custom timer event can be used.
#!/usr/bin/env python
# coding: utf8
from __future__ import absolute_import, division, print_function
from itertools import cycle
import pygame
VISITOR_TTF_FILENAME = '/usr/share/fonts/truetype/aenigma/visitor1.ttf'
BLINK_EVENT = pygame.USEREVENT + 0
def main():
pygame.init()
try:
screen = pygame.display.set_mode((800, 600))
screen_rect = screen.get_rect()
clock = pygame.time.Clock()
font = pygame.font.Font(VISITOR_TTF_FILENAME, 50)
on_text_surface = font.render(
'Press Any Key To Start', True, pygame.Color('green3')
)
blink_rect = on_text_surface.get_rect()
blink_rect.center = screen_rect.center
off_text_surface = pygame.Surface(blink_rect.size)
blink_surfaces = cycle([on_text_surface, off_text_surface])
blink_surface = next(blink_surfaces)
pygame.time.set_timer(BLINK_EVENT, 1000)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
if event.type == BLINK_EVENT:
blink_surface = next(blink_surfaces)
screen.blit(blink_surface, blink_rect)
pygame.display.update()
clock.tick(60)
finally:
pygame.quit()
if __name__ == '__main__':
main()
Instead of going completetly black in the off phase one could also blit the text in a darker color.
Encapsulating the blinking text in a type derived from Pygame's Sprite
might help to keep the code clear if there is going on much more in the initialisation and the main loop.
Upvotes: 4