user969416
user969416

Reputation:

NameError when using "self" in class?

I have the following Python (3.2) code:

from pygame import *

class Application:

    def __init__(self):
        self.running = True
        self.display_surface = None
        self.size = self.width, self.height = 640, 480

        self.old_ticks = pygame.time.get_ticks
        self.new_ticks = None

        pygame.init()
            self.display_surface = pygame.display.set_mode(self.size, pygame.HWSURFACE | pygame.DOUBLEBUF)

    def on_event(self, event):
        if event.type == pygame.QUIT:
            self.running = False

    def on_loop(self):
        pass

    def on_render(self):
        pass

    def on_cleanup(self):
        pygame.quit()

    def regulate_time(self):
        self.new_ticks = pygame.time.get_ticks

        while (self.new_ticks < self.old_ticks + 1000):
            pass

        self.old_ticks = self.new_ticks

    def load_images(self):
        pass

    while(self.running == True):
        for event in pygame.event.get():
            self.on_event(event)

        self.regulate_time
        self.on_loop()
        self.on_render()

    self.on_cleanup()

    test = Application

I'm having a problem with the following line:

while(self.running == True):

which throws me the error : NameError: Name "self" is not defined.

I am pretty much a python newbie and was hoping to use this time to get started on learning python and pygame which I could then use for my college project (two birds with one stone) and I cannot figure out why this error is being thrown at me.

Upvotes: 0

Views: 170

Answers (1)

user166390
user166390

Reputation:

Well, the while(self.running == True) is not in any method (def) so there is no such variable called self in scope (which is what the NameError says)...

...perhaps there is some missing indentation? Although the previous pass makes it look like more than this is missing: maybe def run(self):?

Remember, self is just the conventional (and proper) name given to the first parameter for a method by which the "current instance" is implicitly passed.

Happy coding.

Upvotes: 6

Related Questions