嘿,我正在尝试为我的游戏创建一个使用箭头键并实现重力的 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