0

嘿,我正在尝试为我的游戏创建一个使用箭头键并实现重力的 Player 类,但我不能在我的类方法中使用我的init参数来更新位置等,有人能告诉我为什么请吗?我的错误是

File "playerClass.py", line 83, in <module>
CircleMan.update()

文件“playerClass.py”,第 28 行,更新 self.yPos += self.yVel AttributeError: Player instance has no attribute 'yPos'

class Player:
def __init__(self,image,xPos,yPos,xVel,yVel):
    self.xPos = xPos
    self.xPos = yPos
    self.xVel = xVel
    self.yVel = yVel
    self.Image = image

def draw(self):
    screen.blit(Image,(xPos,yPos))

def update(self):
    self.xPos += self.xVel
    self.yPos += self.yVel

    for event in pygame.event.get():
            if event.type == QUIT:
                    pygame.quit()
                    sys.exit()
            if event.type == KEYDOWN:
                    if event.key == K_LEFT:
                        self.xVel = -2
                    elif event.key == K_RIGHT:
                        self.xVel = 2
                    elif event.key == K_UP:
                        if self.yPos == 469:  ## if player presses down up and player is on the ground, subtract y value making him jump
                            self.yVel = -3
                    elif event.key == K_DOWN:
                         pass


            if event.type == KEYUP:
                    if event.key == K_LEFT:
                        self.xVel = 0
                    elif event.key == K_RIGHT:
                        self.xVel = 0
                    elif event.key == K_UP:
                        self.yVel = 0
                    elif event.key == K_DOWN:  
                        self.yVel = 0

    playerGravity()
    draw()

def playerGravity(self):
    if self.yPos < 469:
        self.yVel = self.yVel + 2
    elif yPos == 469:
        self.yVel = 0
    else:
        pass
4

3 回答 3

2

您可能在init () 函数中打错了字。您设置 self.xPos 两次,但 self.yPos 未设置。

def init (self,image,xPos,yPos,xVel,yVel): self.xPos = xPos self.xPos = yPos self.xVel = xVel self.yVel = yVel self.Image = image

于 2013-01-30T14:07:08.170 回答
1

您在构造函数中犯了一个错误:

self.xPos = yPos

应该 :

self.yPos = yPos
于 2013-01-30T13:40:13.723 回答
0

除非这是您定义self.xPos两次的问题中的错字

  self.xPos = xPos
  self.xPos = yPos

并且从不定义self.yPos

于 2013-01-30T13:43:18.317 回答