这是我的代码,它在Pygame窗口中绘制一个网格,当我在学校的PC上运行它时工作正常。现在我想继续在家里出现一个错误,我自己找不到解决方案:
import sys, pygame
pygame.init()
white = (255, 255, 255)
black = (0,0,0)
orange = (255, 165,0)
green = (0,128, 0)
radius = 25
screen = pygame.display.set_mode((715,715))
screen.fill(white)
pygame.display.update()
def grid():
pygame.draw.line(screen, (black), (60, 0), (60,715))
pygame.draw.line(screen, (black), (360, 0), (360,715))
pygame.draw.line(screen, (black), (0, 65), (715, 65))
pygame.draw.line(screen, (black), (0, 130), (715, 130))
pygame.draw.line(screen, (black), (0, 195), (715, 195))
pygame.draw.line(screen, (black), (0, 260), (715, 260))
pygame.draw.line(screen, (black), (0, 325), (715, 325))
pygame.draw.line(screen, (black), (0, 390), (715, 390))
pygame.draw.line(screen, (black), (0, 455), (715, 455))
pygame.draw.line(screen, (black), (0, 520), (715, 520))
pygame.draw.line(screen, (black), (0, 585), (715, 585))
pygame.draw.line(screen, (black), (0, 650), (715, 650))
pygame.draw.line(screen, (black), (0,715), (715, 715))
pygame.display.update()
grid()
grid()
当我运行这段代码时,我收到一个错误:
Traceback (most recent call last):
File "C:\Users\Jack\Documents\Jack's Stuff\School\Year 10\Computer Science\Programs\Jack's Python programs\Useful programs\pygame_test.py", line 1, in <module>
import sys, pygame
File "C:\Users\Jack\Documents\Jack's Stuff\School\Year 10\Computer Science\Programs\Jack's Python programs\Useful programs\pygame.py", line 11, in <module>
screen = pygame.display.set_mode((715,715))
AttributeError: 'module' object has no attribute 'display'
我的代码出了什么问题,因为我尝试了几种不同的东西?
答案 0 :(得分:1)
你的cwd中的模块pygame
被拿起了。那是你需要的吗?
答案 1 :(得分:0)
你在代码中做了一些非常不寻常的事情。您没有实例化pygame类型的新对象。而是在模块上手动运行init,然后直接引用模块。大概这会导致你的错误。
试试这个
# rather than pygame.init()
game = pygame
# replace all future instances of "pygame" with "game"
game.display.update()