为什么我不能在 Pygame 中进行双跳/跳跃? [英] Why can't I do a double jump/hop in Pygame?

查看:102
本文介绍了为什么我不能在 Pygame 中进行双跳/跳跃?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

其他:如果键[pygame.K_SPACE]:跳跃计数 += 5number_to_compensate += 1

这是我的脚本:

导入pygamepygame.init()赢 = pygame.display.set_mode((500, 500))pygame.display.set_caption(第一场比赛")x = 50y = 50宽度 = 40高度 = 60vel = 5isJump = 假jumpCount = 5 #计算我必须上升的高度number_to_compensate = 0 #所以我按空格的次数越多(我跳得越高)我就必须摔倒运行 = 真运行时:pygame.time.delay(20)对于 pygame.event.get() 中的事件:如果 event.type == pygame.QUIT:运行 = 错误键 = pygame.key.get_pressed()如果键 [pygame.K_LEFT] 和 x >vel:x -= vel如果键 [pygame.K_RIGHT] 和 x <500 - vel - 宽度:x += vel如果不是(isJump):如果键 [pygame.K_UP] 和 y >vel:y -= vel如果键 [pygame.K_DOWN] 和 y <500 - 高度 - vel:y += vel如果键[pygame.K_SPACE]:isJump = 真number_to_compensate += 1别的:如果键[pygame.K_SPACE]:跳跃计数 += 5number_to_compensate += 1如果 jumpCount >= -5 *number_to_compensate:y -= (jumpCount * abs(jumpCount)) * 0.5跳转计数 -= 1别的:跳跃计数 = 5isJump = 假number_to_compensate = 0win.fill((0, 0, 0))pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))pygame.display.update()pygame.quit()

解决方案

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

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

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

这篇关于为什么我不能在 Pygame 中进行双跳/跳跃?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆