Python 3.5 AttributeError:'Player'对象没有属性'posX'

时间:2015-12-05 12:24:31

标签: python inheritance pygame attributeerror python-3.5

所以我找到了类似问题的答案,但它们似乎对我的情况不起作用。下面是我试图使用的类文件:

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.yMove = yMove
    self.xMove = xMove


class Player(Characters):
def __init__(self,images,gameOver):
    self.images = images
    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]:
        self.yMove = -self.moveSpeed
        self.image = self.images[1]
    if keysPressed[pygame.K_s]:
        self.yMove = self.moveSpeed
        self.image = self.images[0]
    if keysPressed[pygame.K_a]:
        self.xMove = -self.moveSpeed
        self.image = self.images[2]
    if keysPressed[pygame.K_d]:
        self.xMove = self.moveSpeed
        self.image = pygame.transform.flip(self.images[2],True,False)

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

    return (self.image,self.posX,self.posY,self.gameOver)

下面是我试图用来调用模块Player.move()的主要功能的一部分:

gameOver = False
playerPos = [None,(surfaceDim[0]/2,surfaceDim[1]/2),gameOver]

while not playerPos[2]:

    player = Characters.Player(playerImgs,gameOver)
    playerPos = player.move()

当我这样做时,它会告诉我'Player' object has no attribute 'posX'。根据我的理解,Player应该继承来自Characters的属性,但是在这里它不是。我也尝试使用super()方法,但它告诉我我缺少变量。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

你需要像这样初始化Character类:

class Player(Characters):
def __init__(self,images,gameOver):
    Characters.__init__(self)