且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在pygame中进行双跳?

更新时间:2023-01-18 10:55:50

为了你想要的你需要改变一些跳转算法.根据加速度和速度计算跳跃.

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

PLAYER_ACC = 15PLAYER_GRAV = 1

使用键盘事件进行跳转,而不是pygame.key.get_pressed().键盘事件(参见

导入pygamepygame.init()赢 = pygame.display.set_mode((500, 500))pygame.display.set_caption(第一场比赛")时钟 = pygame.time.Clock()地_y = 400x,y = 200,ground_y宽度,高度 = 40, 60vel_x, vel_y = 5, 0acc_y = 0PLAYER_ACC = 15PLAYER_GRAV = 1运行 = 真运行时:acc_y = PLAYER_GRAV对于 pygame.event.get() 中的事件:如果 event.type == pygame.QUIT:运行 = 错误如果 event.type == pygame.KEYDOWN:如果 event.key == pygame.K_SPACE:acc_y = -PLAYER_ACCvel_y = 0键 = pygame.key.get_pressed()x += (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * vel_xx = max(0, min(500 - width, x))vel_y += acc_yy += vel_y如果 y + 高度 >地_y:y = ground_y - 高度vel_y = 0acc_y = 0win.fill((0, 0, 0))pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))pygame.display.update()时钟滴答(60)pygame.quit()

Can someone please help me debug my code because I don't get why I cannot make my character do a double jump with multiple spacebars. When I ran the script, I could move up,down,left,right but once I press spacebar one time, the object flies out of the window.

The problem comes with this if statement so I'm guessing that this if statement keeps running and incrementing my jumpCount, which I can't comprehend because after pressing space one time shouldn't the keys[pygame.K_SPACE] evaluate to true and then back to false again so this if statement shouldn't run unless I press another spacebar?

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

Here is my script:

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()

To what you want you need to change some the jump algorithm. Calculate the jump based on an acceleration and a velocity.

Define constants for the gravity and jump acceleration:

PLAYER_ACC = 15
PLAYER_GRAV = 1

Use the keyboard events for the jump, instead of pygame.key.get_pressed(). The keyboard events (see pygame.event module) occur only once when the state of a key changes. The KEYDOWN event occurs once every time a key is pressed. KEYUP occurs once every time a key is released. Use the keyboard events for a single action.
Set the jump acceleration when the space is pressed:

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

Change the velocity depending on the acceleration and the y coordinate depending on the velocity in every frame:

vel_y += acc_y
y += vel_y

Limit the y-coordinate by the ground:

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


Minimal example:

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()