[英]How to make a text blink in PyGame?
Like this, but it doesn't have to fade out or any other advanced way.像这样,但它不必淡出或任何其他高级方式。
font_fade = pygame.USEREVENT + 1
pygame.time.set_timer(font_fade, 1)
if event.type == pygame.KEYDOWN:
click_to_start()
if event.type == font_fade:
text('press any button to start', (0, 0, 0), screen, screen.get_width() / 2, screen.get_height() - 70, 20)
i have tried with USEREVENT
, but it blinks so fast and does not works sometimes我试过USEREVENT
,但它闪烁得很快,有时不起作用
The unit of time ofpygame.time.set_timer
is milliseconds. pygame.time.set_timer
的时间单位是毫秒。 IF you want to define an interval of 1 second you have specify a time of 1000 milliseconds:如果你想定义 1 秒的间隔,你已经指定了 1000 毫秒的时间:
pygame.time.set_timer(font_fade, 1)
pygame.time.set_timer(font_fade, 1000)
You need to draw the text in the application loop.您需要在应用程序循环中绘制文本。 If you want the a blinking text you have to draw it once and not draw it once.如果你想要一个闪烁的文本,你必须画一次而不是画一次。
Add a Boolean variable show_text
.添加一个 Boolean 变量show_text
。 Toggle the variable when the event occurse.事件发生时切换变量。 Draw the text depending on the variable:根据变量绘制文本:
while True:
for event in pygame.event.get():
# [...]
if event.type == font_fade:
show_text = not show_text
# [...]
if show_text:
text('press any button to start', (0, 0, 0), screen, screen.get_width() / 2, screen.get_height() - 70, 20)
Minimal example:最小的例子:
import pygame
pygame.init()
window = pygame.display.set_mode((500, 100))
clock = pygame.time.Clock()
font_fade = pygame.USEREVENT + 1
pygame.time.set_timer(font_fade, 200)
font = pygame.font.SysFont(None, 40)
text_surf = font.render('press any button to start', True, (255, 255, 0))
show_text = True
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == font_fade:
show_text = not show_text
window.fill(0)
if show_text:
window.blit(text_surf, text_surf.get_rect(center = window.get_rect().center))
pygame.display.flip()
clock.tick(60)
pygame.quit()
exit()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.