在 Pygame 中制作流体跳跃运动

Making Fluid Jump Motion in Pygame

我为我的角色编写了一个跳跃动作,但是,每次我按下向上箭头键时都会发生变化,而不是一个适当的流畅动作。我曾尝试使用 while 循环制作代码,但屏幕随后冻结。我尝试将代码设为事件然后调用它,但没有成功。我什至感到绝望,尝试观看 YouTube 视频并查看 Stack Overflow 上已解决的问题,但我无法解决我的问题。这是代码:

#Variables
player_img = pygame.image.load('car.png')
player_x = 40
player_y = 390

vel = 5

isJump = False
jumpCount = 10
player_pass_range = range(10,21)

class Player():
    def draw_player():
        screen.blit(player_img, (player_x, player_y))

#Main Loop
while playing:
    screen.fill((0, 0, 255))         

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running, playing, credits_menu, options_menu = False, False, False, False

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                if jumpCount >= -10:
                    player_y -= (jumpCount * abs(jumpCount)) * 0.5
                    jumpCount -= 1
                else: # This will execute if our jump is finished
                   jumpCount = 10
                   isJump = False

    #Draw Objects
    Player.draw_player()

    #Update Screen
    pygame.display.update()
    clock.tick(60)

跳跃动画必须在应用程序循环而不是事件循环中完成。按下该键时将 isJump 设置为 True。根据 isJump:

在应用程序循环中为播放器设置动画
while playing:
    screen.fill((0, 0, 255))         

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running, playing, credits_menu, options_menu = False, False, False, False

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                isJump = True

    if isJump:
        if jumpCount >= -10:
            player_y -= (jumpCount * abs(jumpCount)) * 0.5
            jumpCount -= 1
        else: # This will execute if our jump is finished
            jumpCount = 10
            isJump = False

    # [...]