[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Fwd: [Tutor] A more Pythonic way to do this



> The symptom is that they are all painting on top of each other in the same
> position.  Correct?

You got it. 

> The confusion here is between *class* attributes and *object* attributes
> (or more pythonically speaking, *instance* attributes).
....*snip*...
> class Enemy:
>  someVar = 90
>  def __init__(self):
>    self.pants = 44
> 
> We usually just call "pants" an attribute, but we usually give special
> significance to "someVar" and call it a "class attribute" -- it is common
> across all instances of that class
...*snip*...
> Anyway enough lecturing.  The basic rule is stay away from class
> attributes unless you need them.  Just use instance attributes by defining
> attributes in your __init__() function, like "pants" above.

Ok, so I set up, within my Enemy class, the following:

class Enemy(pygame.sprite.Sprite):
    def __init__(self, startx, starty, level):
        pygame.sprite.Sprite.__init__(self)
*snip*
        self.rect.centerx = startx
        self.rect.centery = starty
        self.initialx = 0
        self.initialy = 0

so.... didn't I set it up as an instance attribute and NOT as a class
attribute? each new "self" should have its own initialx, initialy,
right? Or am I misunderstanding you?

~Denise