简体   繁体   中英

Python/Pygame errors in class inheritance

So I've been working with classes and inheritance and from my limited understanding, I've gathered that attributes should be inherited. However, the first set of code below throws this error: 'AttributeError: type object 'Characters' has no attribute 'posX' with the below code:

class Characters(object):
def __init__(self,health,posX,posY,width,height,dead,moveSpeed,yMove,xMove):
    self.health = health
    self.posX = posX
    self.posY = posY
    self.width = width
    self.height = height
    self.dead = dead
    #self.image = image
    self.yMove = yMove
    self.xMove = xMove


class Player(Characters):
def __init__(self,images,gameOver):
    self.images = (config.get('player'))
    self.gameOver = gameOver


def move(self):

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            self.gameOver = True

    keysPressed = pygame.key.get_pressed()

    if keysPressed[pygame.K_w]:
        Characters.yMove = -Characters.moveSpeed
        Characters.image = self.images[1]
    if keysPressed[pygame.K_s]:
        Characters.yMove = Characters.moveSpeed
        Characters.image = self.images[0]
    if keysPressed[pygame.K_a]:
        Characters.xMove = -Characters.moveSpeed
        Characters.image = self.images[2]
    if keysPressed[pygame.K_d]:
        Characters.xMove = Characters.moveSpeed
        Characters.image = pygame.transform.flip(self.images[2],True,False)

    Characters.posX += Characters.xMove
    Characters.posY += Characters.yMove

And with the below code, it throws the error: AttributeError: 'pygame.Surface' object has no attribute 'posX'

class Player(Characters):
def __init__(self,images,gameOver,posX,posY):
    self.images = (config.get('player'))
    self.gameOver = gameOver
    self.posX = posX
    self.posY = posY

def move(self):

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            self.gameOver = True

    keysPressed = pygame.key.get_pressed()

    if keysPressed[pygame.K_w]:
        Characters.yMove = -Characters.moveSpeed
        Characters.image = self.images[1]
    if keysPressed[pygame.K_s]:
        Characters.yMove = Characters.moveSpeed
        Characters.image = self.images[0]
    if keysPressed[pygame.K_a]:
        Characters.xMove = -Characters.moveSpeed
        Characters.image = self.images[2]
    if keysPressed[pygame.K_d]:
        Characters.xMove = Characters.moveSpeed
        Characters.image = pygame.transform.flip(self.images[2],True,False)

    self.posX += Characters.xMove
    self.posY += Characters.yMove

I have tried using *args and **kwargs , the super method, and Parent.__init__(self, attr1, attr2,...) , but it still throws the same errors. Is there something I'm doing wrong?

Maybe I'm wrong (I'm a newbie...!), but: shouldn't you indent the constructor and the other class methods after the class statement? Is that just a transcription error? I mainly refer to Pyhton 2.7, maybe this has changed in the 3.x versions...

The instance variables are inherited so they're accessible simply by referencing them through self , don't use Characters . As a basic example:

class A(object):
    def __init__(self):
        self.foo = 3;

class B(A):
    def thefoo(self):
        print self.foo

b = B()
b.thefoo()

Here, B inherits the instance variable foo from A and as you can see, self.foo in an instance of B returns the inherited value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM