如何在 pygame 中进行双跳?

问题描述

有人可以帮我调试我的代码,因为我不明白为什么我不能让我的角色用多个空格键进行双跳。当我运行脚本时,我可以向上、向下、向左、向右移动,但是一旦我按一次空格键,对象就会飞出窗口。

问题来自这个 if 语句,所以我猜这个 if 语句会继续运行并增加我的 jumpCount,我无法理解,因为按一次空格后 keys[pygame.K_SPACE] 不应该评估为 true然后再次返回 false,因此除非我按另一个空格键,否则不应运行此 if 语句?

else:
    if keys[pygame.K_SPACE]:
        jumpCount += 5
        number_to_compensate += 1

这是我的脚本:

import pygame

pygame.init()

win = pygame.display.set_mode((500,500))
pygame.display.set_caption("First Game")

x = 50
y = 50
width = 40
height = 60
vel = 5

isJump = False

jumpCount = 5 #To calculate the height I have to rise by
number_to_compensate = 0 #So the more times I press space(higher I jump) the more I have to fall by

run = True

while run:
    pygame.time.delay(20)

    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 - vel - width:
        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
            number_to_compensate += 1
    else:
        if keys[pygame.K_SPACE]:
            jumpCount += 5
            number_to_compensate += 1
        if jumpCount >= -5 *number_to_compensate:
            y -= (jumpCount * abs(jumpCount)) * 0.5
            jumpCount -= 1
        else:
            jumpCount = 5
            isJump = False
            number_to_compensate = 0



    win.fill((0,0))
    pygame.draw.rect(win,(255,0),(x,y,width,height))
    pygame.display.update()

pygame.quit()

解决方法

根据您的需要,您需要更改一些跳转算法。根据加速度和速度计算跳跃。

定义重力和跳跃加速度的常数:

PLAYER_ACC = 15
PLAYER_GRAV = 1

使用键盘事件进行跳转,而不是 pygame.key.get_pressed()。键盘事件(请参阅 pygame.event 模块)仅在键状态更改时发生一次。 KEYDOWN 事件在每次按下键时发生一次。 KEYUP 每次释放键时出现一次。将键盘事件用于单个操作。
设置按下空格时的跳跃加速度:

acc_y = PLAYER_GRAV
for event in pygame.event.get():
    if event.type == pygame.QUIT:
        run = False
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_SPACE:
            acc_y = -PLAYER_ACC
            vel_y = 0

根据加速度改变速度,根据每一帧的速度改变 y 坐标:

vel_y += acc_y
y += vel_y

通过地面限制 y 坐标:

if y + height > ground_y:
    y = ground_y - height
    vel_y = 0
    acc_y = 0

最小示例:

import pygame

pygame.init()
win = pygame.display.set_mode((500,500))
pygame.display.set_caption("First Game")
clock = pygame.time.Clock()

ground_y = 400
x,y = 200,ground_y
width,height = 40,60
vel_x,vel_y = 5,0
acc_y = 0

PLAYER_ACC = 15
PLAYER_GRAV = 1

run = True
while run:
    acc_y = PLAYER_GRAV
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                acc_y = -PLAYER_ACC
                vel_y = 0

    keys = pygame.key.get_pressed()
    x += (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * vel_x
    x = max(0,min(500 - width,x))

    vel_y += acc_y
    y += vel_y

    if y + height > ground_y:
        y = ground_y - height
        vel_y = 0
        acc_y = 0

    win.fill((0,0))
    pygame.draw.rect(win,(255,0),(x,y,width,height))
    pygame.display.update()
    clock.tick(60)

pygame.quit()