已解决AttributeError: module 'pygame.time' has no attribute 'sleep'问题
一、出现报错
AttributeError: module 'pygame.time' has no attribute 'sleep'
翻译为:
属性错误:模块“pygame.time”没有属性“sleep”
二、报错原因
这个报错提示是因为Pygame的time模块中没有sleep()函数。这可能是因为您使用的Pygame版本与代码编写的版本不一致导致的。
三、那么,如何解决呢?
原来的代码:
import time
import pygame
while True:
time.sleep(0.01) #休眠0.01秒再触发
解决办法:如果您使用的是Pygame 2.x版本,可以使用Pygame内置的Clock类来替代time.sleep()函数。具体方法如下:
import pygame
pygame.init()
clock = pygame.time.Clock()
# 在游戏循环中使用clock.tick()方法来替代time.sleep()函数
while True:
clock.tick(60) # 每秒最多循环60次