嘿我正在尝试为我的游戏制作一个使用箭头键并实现重力的Player类,但我不能在我的类方法中使用我的 init 参数来更新位置等等,可以有人告诉我为什么会这样?我的错误是
File "playerClass.py", line 83, in <module>
CircleMan.update()
文件“playerClass.py”,第28行,更新中 self.yPos + = self.yVel AttributeError:Player实例没有属性'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
答案 0 :(得分:2)
你可能在 init ()函数中输了一个错字。您正在设置self.xPos两次,但未设置self.yPos。
def init (自我,图片,xPos,yPos,xVel,yVel): self.xPos = xPos self.xPos = yPos self.xVel = xVel self.yVel = yVel self.Image = image
答案 1 :(得分:1)
你在构造函数中犯了一个错误:
self.xPos = yPos
应该是:
self.yPos = yPos
答案 2 :(得分:0)
除非在问题中输入错误,否则您已定义self.xPos
两次
self.xPos = xPos
self.xPos = yPos
并且永远不要定义self.yPos