0

when i run my program i want it to run start_game(self). For that, the constructor has to be run. Without that, the objects don't exist and therefore their methods can't be run. So far, so clear. Basically, i struggle to correctly "start" that process.

from Surface import Surface
from Pellet import Pellet
from Pacman import Pacman
from Ghost import Ghost
from Timer import Timer


class Controls:
    def __init__(self):
        global the_surface
        the_surface = Surface(self)

        the_pellets = []

        for y in range(0, 8):
            for x in range(0, 14):
                pellet = Pellet(x, y)
                the_pellets.append(pellet)

        global the_pacman
        the_pacman = Pacman(the_pellets)

        the_ghosts = []
        for ghost in range(0, 3):
            the_ghosts.append(Ghost())

        global the_timer
        the_timer = Timer(self, 200)

    # [...]

    def start_game(self):
        self.__init_game_objects()
        Timer.start(the_timer)
        return

    def tick_timer(self):
        Pacman.move(the_pacman)
        return

    # http://stackoverflow.com/a/419185
    if __name__ == '__main__':
        # need to run start_game()

What I've tried (all of the following is after the if __name__ [...] line, every bullet point represents one trial.)

first attempt:

the_controls = Controls()
the_controls.start_game(the_controls)

Traceback (most recent call last):
  File "Controls.py", line 8, in <module>
    class Controls:
  File "Controls.py", line 53, in Controls
    the_controls = Controls()
NameError: name 'Controls' is not defined

second attempt:

__init__('Controls')
self.start_game(self)

Traceback (most recent call last):
  File "Controls.py", line 8, in <module>
    class Controls:
  File "Controls.py", line 54, in Controls
    self.start_game(self)
NameError: name 'self' is not defined

third attempt(as suggested by @TigerhawkT3)

the_controls = Controls()
the_controls.start_game()

Traceback (most recent call last):
  File "Controls.py", line 8, in <module>
    class Controls:
  File "Controls.py", line 53, in Controls
    the_controls = Controls()
NameError: name 'Controls' is not defined
toogley
  • 551
  • 1
  • 4
  • 11
  • 1
    Is your indentation wrong or is the ``if __name__``... defined in your class? – MSeifert Feb 11 '16 at 23:38
  • @MSeifert according to http://stackoverflow.com/a/419185/5124117 i don't need to define `__name__`, as that's automatically set up when the program is run. I'll check my indentation – toogley Feb 11 '16 at 23:47
  • 2
    Sorry, I meant the ``if __name__ == '__main__':`` has the same indentation as the inside of the class in your first code block. (the last few lines) – MSeifert Feb 11 '16 at 23:48
  • This is absolutely an indentation error. – g.d.d.c Feb 11 '16 at 23:49
  • @MSeifert ah, yeah. it seems that this was the problem. I'm getting other (unrelated to this topic) errors though. Would you mind posting your comment as answer, then i'll accept it. – toogley Feb 11 '16 at 23:53

2 Answers2

3

The instance itself is automatically passed. Use the following:

the_controls = Controls()
the_controls.start_game()

This is similar to other object methods you may be familiar with:

'hello world'.upper()

You could also write it like this:

the_controls = Controls()
Controls.start_game(the_controls)

Or this:

str.upper('hello world')

But the former style is preferred over the latter.

TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
  • I've adressed that in my post method in my post (third attempt), but it hasn't worked. – toogley Feb 11 '16 at 23:47
  • Your posted code can't be all your code, then, because if it was accurate and complete then nothing would happen when you ran the program. – TigerhawkT3 Feb 12 '16 at 06:52
3

It seems like you have an indentation error in your code: The if __name__ == '__main__': is defined in your class. You need to define it outside the class in the global namespace of this file/module (meaning: without indentation) if you want it to be executed while importing the file/module.

MSeifert
  • 145,886
  • 38
  • 333
  • 352