gpt4 book ai didi

python - pygame.time.wait() 使程序崩溃

转载 作者:太空宇宙 更新时间:2023-11-03 15:43:25 25 4
gpt4 key购买 nike

在 pygame 代码中,我想做一个改变颜色的标题。我试着做一个简单的标题来改变颜色,但它甚至没有把颜色变成蓝色(或做一秒钟),程序崩溃了。代码:

title_font = pygame.font.SysFont("monospace", TITLE_SIZE)

while True:
title = title_font.render("game", 5, RED)
game_display.blit(title, TITLE_POS)
pygame.display.update()
pygame.time.wait(2000)

title = title_font.render("game", 5, BLUE)
game_display.blit(title, TITLE_POS)
pygame.display.update()
pygame.time.wait(3000)

title = title_font.render("game", 5, RED)
game_display.blit(title, TITLE_POS)
pygame.display.update()
pygame.time.wait(2000)

pygame.time.delay()也会出现,不知道是哪里出了问题...

最佳答案

不要使用 pygame.time.waitdelay,因为这些函数会使您的程序在给定时间内休眠,并且窗口变得无响应。您还需要在每一帧处理事件(使用 pygame.event 函数之一)以避免这种情况。

这里有一些不阻塞的计时器示例:Countdown timer in Pygame

要切换颜色,您只需将下一种颜色分配给变量并使用它来呈现文本。

import pygame


pygame.init()
screen = pygame.display.set_mode((640, 480))
clock = pygame.time.Clock()
title_font = pygame.font.SysFont('monospace', 50)
BACKGROUND_COLOR = pygame.Color('gray12')
BLUE = pygame.Color('blue')
RED = pygame.Color('red')
# Assign the current color to the color variable.
color = RED
timer = 2
dt = 0

done = False
while not done:
# Handle the events.
for event in pygame.event.get():
# This allows the user to quit by pressing the X button.
if event.type == pygame.QUIT:
done = True

timer -= dt # Decrement the timer by the delta time.
if timer <= 0: # When the time is up ...
# Swap the colors.
if color == RED:
color = BLUE
timer = 3
else:
color = RED
timer = 2

screen.fill(BACKGROUND_COLOR)
title = title_font.render('game', 5, color)
screen.blit(title, (200, 50))

pygame.display.flip()
# dt is the passed time since the last clock.tick call.
dt = clock.tick(60) / 1000 # / 1000 to convert milliseconds to seconds.

pygame.quit()

关于python - pygame.time.wait() 使程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51411713/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com