pygame 不会显示 window

pygame won't bring up a window

我正在 raspberry pi 3 上学习 python,所以我想我会尝试添加一些图形。我试图打开一个 window 来添加我的图形,但它显示了以下错误消息:

Traceback (most recent call last):
  File "/home/pi/python_programmes/pygame1.py", line 1, in <module>
    import pygame
  File "/home/pi/python programmes/pygame.py", line 6, in <module>
AttributeError: 'module' object has no attribute 'init'

我输入的代码是:

import pygame
pygame.init()
size = [700, 500]
screen = pygame.display.set_mode(size)
pygame.display.set_caption("My game")
done = False
clock = pygame.time.Clock()
while done == False:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
    clock.tick(20)
pygame.quit()

我删除了所有名为 pygame.py 的文件,但仍有一个名为 pygame.pyc

您有一个名为 File "/home/pi/python programmes/pygame.py" 的本地文件, 所以会发生什么情况是您的脚本没有导入 pygame 库,而是从您的本地文件夹 File "/home/pi/python programmes/" 导入 pygame.py 。您只需将此文件从 pygame.py 重命名为其他名称即可解决您的问题。