嗨,我一直在用Tim的pygame关注Tech。然而,我已经达到了一个点,我们正在编写跳跃。我理解代码,但我不明白为什么我的计算机在达到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()
发布于 2020-05-17 20:42:55
条件是错误的。它必须是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
https://stackoverflow.com/questions/61851639
复制相似问题