我(试图)用Python 2.7.12在Pygame中重建马里奥级别1-1。这些是两个相关的文件:
main.py: http://pastebin.com/HXmBdJ2a
mario.py:
http://pastebin.com/29xu1tMM
我的问题是:当我运行main.py时,解释器给我这个错误信息:
Traceback (most recent call last):
File "C:/path/to/main.py", line 6, in <module>
import mario
File "C:\path\to\mario.py", line 7, in <module>
import main
File "C:\path\to\main.py", line 47, in <module>
game = Game()
File "C:\path\to\main.py", line 26, in __init__
self.main_loop()
File "C:\path\to\main.py", line 39, in main_loop
self.Mario = mario.Mario()
AttributeError: 'module' object has no attribute 'Mario'
Process finished with exit code 1
我很困惑,因为mario.py是马里奥班。如果我尝试运行mario.py,我会收到此错误:
Traceback (most recent call last):
File "C:/path/to/mario.py", line 7, in <module>
import main
File "C:\path\to\main.py", line 6, in <module>
import mario
File "C:\path\to\mario.py", line 12, in <module>
sheet = pygame.image.load("../assets/images/MarioLuigiSprites.png").convert()
pygame.error: No video mode has been set
Process finished with exit code 1
有人可以向我解释一下吗?
编辑:我修改了它:
import sys
sys.path.insert(0, "scripts")
import mario
答案 0 :(得分:0)
在这种情况下,AttributeError: 'module' object has no attribute 'Mario'
来自Game
类没有名为Mario
的属性这一事实,因为您没有在中定义它您的Game类的init 。为清楚起见,你没有:
class Game(object):
def __init__(self):
self.fps = 60
self.showfps = True
self.clock = pygame.time.Clock()
# Set FPS
self.clock.tick(self.fps)
self.Mario = mario.Mario() # notice this line!!!
.......
.......
您可以在游戏课程的self.Mario = mario.Mario()
中使用__init__
,也可以在您拥有的代码中只调用Mario = mario.Mario()
。
这意味着保留您已经拥有的代码应该这样做:
def main_loop(self):
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.closegame()
Mario = mario.Mario() # notice: it's not self.Mario....
DISPLAYSURF.fill(const.BLACK)
旁注:为什么在mario.py结束时拨打mario = Mario()
?尽量删除它。
如果您不使用main.py
中定义的任何方法或类,则可以删除import main
中的mario.py
。