我的播放器设置为在按下空格键时跳跃,
并在释放空格键时将 Y 速度 (yVel) 设置为 0。
这应该会导致我正在寻找的平稳跳跃和跌落,
但是如果我没有在整个跳跃过程中按住空格键(即当他在 yPos = 469 处撞击地面时),出于某种奇怪的原因,
我必须按两次空格键才能让他再次跳跃。
有人可以向我解释为什么会这样吗?
我的代码:
class Player:
def __init__(self,screen,image,xPos,yPos,xVel,yVel):
self.xPos = xPos
self.yPos = yPos
self.xVel = xVel
self.yVel = yVel
self.image = image
self.screen = screen
def draw(self):
screen.blit(self.image,(self.xPos,self.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_SPACE:
if self.yPos == 469:
self.Jump()
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_SPACE:
self.yVel = 0
elif event.key == K_DOWN:
pass
self.playerGravity()
def playerGravity(self):
if self.xPos > 469:
self.xPos = 469
if self.xPos < 0:
self.xPos = 0
if self.yPos > 469:
self.yPos = 469
if self.yPos < 0:
self.yPos = 0
if self.yPos < 469:
self.yVel += 1
print(self.xPos)
print(self.yPos)
def Jump(self):
##for i in range(10):
self.yVel = -20
##break