鱼C论坛

 找回密码
 立即注册
查看: 531|回复: 6

[已解决]pygame里如何用time.sleep()

[复制链接]
发表于 2025-4-6 19:57:54 | 显示全部楼层
在pygame主循环中使用time.sleep会导致循环堵塞。因此应将time.sleep(秒数)pygame.time.delay(毫秒数)。
  1. import pygame
  2. RED = (255, 0, 0)
  3. WIDTH, HEIGHT = 600, 500
  4. screen=pygame.display.set_mode((WIDTH, HEIGHT))
  5. pygame.init()
  6. font = pygame.font.SysFont('simhei', 60)
  7. winner = 'black'

  8. while True:
  9.     screen.fill((0,255,255))
  10.     # 处理事件
  11.     for event in pygame.event.get():
  12.         if event.type == pygame.QUIT:
  13.             pygame.quit()
  14.     if winner == "black":
  15.         text = font.render("小爱赢了!",True, RED)
  16.         screen.blit(text, (100, 10))
  17.     elif winner == "red":
  18.         text = font.render("恭喜你赢了!", True, RED)
  19.         screen.blit(text, (50, 10))
  20.     pygame.time.delay(5000)
  21.     winner = ''
  22.     pygame.display.flip()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-9-23 03:04

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表