当我运行我的代码时,它可以工作,但是当我想输入跳转命令时会出现问题.计算机跳跃,但随后不断从我的屏幕上掉下来



嗨,我一直在关注Tim的pygame的技术。但是,我已经达到了编码跳转的地步。我理解代码,但我不明白为什么一旦我达到 jumpCount = -10,我的计算机就不会考虑 else 语句 这是用于理解的完整代码,但它是从"if not isJump:"开始的部分。

x = 50
y = 50
width = 40
height = 60
vel = 5
isJump = False
jumpCount = 10
run = True
while run:
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and x > vel:
x -= vel
if keys[pygame.K_RIGHT] and x < 500 - (width + vel):
x += vel
if not isJump:
if keys[pygame.K_UP] and y > vel:
y -= vel
if keys[pygame.K_DOWN] and y < 500 - (height + vel):
y += vel
if keys[pygame.K_SPACE]:
isJump = True
# So my problem is really here.
else:
if isJump >= -10:
neg = 1
if jumpCount < 0:
neg = -1
y -= ((jumpCount ** 2) * neg) * 0.5
jumpCount -= 1
else:
isJump = False
jumpCount = 10
window.fill((0, 0, 0))
pygame.draw.rect(window, (255, 0, 0), (x, round(y), width, height))
pygame.display.update()
pygame.quit()

条件错误。它必须是if jumpCount >= -10:而不是if isJump >= -10:

while run:
# [...]
if not isJump:
# [...]
else:
if jumpCount >= -10:
neg = 1
if jumpCount < 0:
neg = -1
y -= ((jumpCount ** 2) * neg) * 0.5
jumpCount -= 1
else:
isJump = False
jumpCount = 10

最新更新