0

I'm (attempting) to recreate Mario level 1-1 in Pygame with Python 2.7.12. these are the two relevant files:

main.py: http://pastebin.com/HXmBdJ2a

mario.py: http://pastebin.com/29xu1tMM
My problem is: when I run main.py, the interpreter gives me this error message:

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


I'm confused, as mario.py does the class Mario. If I try to run mario.py, I get this error:

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


Could anyone explain this to me?
EDIT: I fixed it by adding:
import sys sys.path.insert(0, "scripts")
import mario

Oliver W.
  • 11
  • 2
  • Try importing mario.Mario – Nikhil Raghavendra Feb 11 '17 at 07:25
  • And also, try doing this `screen = pygame.display.set_mode((num, num))` to avoid the No video mode error – Nikhil Raghavendra Feb 11 '17 at 07:28
  • See this: http://stackoverflow.com/questions/18905989/pygame-error-no-video-mode-has-been-set – Nikhil Raghavendra Feb 11 '17 at 07:29
  • I tried import mario.Mario, and got: ImportError: No module named Mario – Oliver W. Feb 11 '17 at 07:30
  • The problem is that the Game class doesn't have an attribute called Mario, as you do not define it in the `__init__` of your Game class. Either you have `self.Mario = mario.Mario()` in the `__init__` of Game class or call just `Mario = mario.Mario()`. Plus, why do you call `mario = Mario()` at the end of your `mario.py`? You should try to remove that. – fedepad Feb 11 '17 at 07:47
  • Could it be that since there are errors in Mario, that main won't even acknowledge the class? – TallChuck Feb 11 '17 at 07:48
  • @ragledwhy remove `mario = Mario()` in the mario.py at the end and just call `Mario = mario.Mario()` in main.py and not `self.Mario = mario.Mario()`. – fedepad Feb 11 '17 at 07:58
  • Plus why do you need to `import main` in mario.py ? I think you can remove that. – fedepad Feb 11 '17 at 08:05
  • You have a [circular import](http://stackoverflow.com/questions/22187279/python-circular-importing). – user2357112 Feb 11 '17 at 23:10

1 Answers1

0

The AttributeError: 'module' object has no attribute 'Mario' in this case come from the fact that the Game class doesn't have an attribute called Mario, as you do not define it in the init of your Game class. To make it clear, you do not have:

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!!!
        .......
        .......

Either you have self.Mario = mario.Mario() in the __init__ of Game class or call just Mario = mario.Mario() in the code you have.
This means that keeping the code you have already you should do:

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)   

Side note: why do you call mario = Mario() at the end of your mario.py? Try to remove that.
And if you do not use any methods or classes defined in main.py you can remove import main in mario.py.

fedepad
  • 4,509
  • 1
  • 13
  • 27
  • Thanks. Now when I run main I get a different error: sheet = pygame.image.load("../assets/images/MarioLuigiSprites.png").convert() pygame.error: No video mode has been set – Oliver W. Feb 11 '17 at 08:29
  • Did you check already one of the comment from @Nikhil Raghavendra on that video issue? Again here if you didn't look: http://inventwithpython.com/pygamecheatsheet.png and http://stackoverflow.com/questions/18905989/pygame-error-no-video-mode-has-been-set – fedepad Feb 11 '17 at 08:37
  • I still get the no video mode has been set error even when I don't use a tuple in pygame.display.set_mode(). I tried importing mario.Mario after I set the display, and get: ImportError: No module named Mario – Oliver W. Feb 11 '17 at 09:17