我怎样才能在pygame中进行双跳?

2024-03-29 08:29:25 发布

您现在位置:Python中文网/ 问答频道 /正文

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

这个if语句出现了问题,所以我猜这个if语句一直在运行并增加我的跳转计数,这我无法理解,因为在按空格键一次之后,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, 0))
    pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))
    pygame.display.update()

pygame.quit()

Tags: andtonumberifspace语句keyspygame
1条回答
网友
1楼 · 发布于 2024-03-29 08:29:25

为了达到你想要的效果,你需要改变一些跳跃算法。根据加速度和速度计算跳跃

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

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, 0))
    pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))
    pygame.display.update()
    clock.tick(60)

pygame.quit()

相关问题 更多 >