python,pygame-跳得太快?

Ast*_*ent 2 python pygame python-3.x

我搞不清pygame,并试图创建一个简单的跳跃功能(尚无物理学)。

由于某种原因,即使我使用的值已打印出来并且似乎按预期工作,我的“跳转”在显示中也不可见。我可能做错了什么?

isJump = False
jumpCount = 10
fallCount = 10
Run Code Online (Sandbox Code Playgroud)
if keys[pygame.K_SPACE]:
    isJump = True
if isJump:
    while jumpCount > 0:
        y -= (jumpCount**1.5) / 3
        jumpCount -= 1
        print(jumpCount)
    while fallCount > 0:
        y += (fallCount**1.5) / 3
        fallCount -= 1
        print(fallCount)
    else:
        isJump = False
        jumpCount = 10
        fallCount = 10
        print(jumpCount, fallCount)

win.fill((53, 81, 92))
pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))
pygame.display.update()
Run Code Online (Sandbox Code Playgroud)

我缩短了代码量,但我认为这就是与问题有关的全部。

Rab*_*d76 5

您必须将while循环转换为if条件。您不想在一帧中进行完整的跳转。
您必须每帧执行一个“跳跃”步骤。使用主应用程序循环执行跳转。

参见示例:

import pygame

pygame.init()
win = pygame.display.set_mode((400, 400))
clock = pygame.time.Clock()

isJump = False
jumpCount, fallCount = 10, 10
x, y, width, height = 200, 300, 20, 20

run = True
while run:
    clock.tick(20)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
    keys = pygame.key.get_pressed()

    if keys[pygame.K_SPACE]:
        isJump = True
    if isJump:
        if jumpCount > 0:
            y -= (jumpCount**1.5) / 3
            jumpCount -= 1
            print(jumpCount)
        elif fallCount > 0:
            y += (fallCount**1.5) / 3
            fallCount -= 1
            print(fallCount)
        else:
            isJump = False
            jumpCount, fallCount = 10, 10
            print(jumpCount, fallCount)

    win.fill((53, 81, 92))
    pygame.draw.rect(win, (255, 0, 0), (x, y, width, height)) 
    pygame.display.flip()
Run Code Online (Sandbox Code Playgroud)