简体   繁体   English

在Pygame中跳跃

[英]Jumping in Pygame

I am trying to make a game like geometry dash in pygame. 我试图在pygame中制作像几何破折号的游戏。 I have everything done except the jumping part. 除了跳跃部分,我已完成所有工作。 I need it so that when the character is on a square block he can jump up, but cannot double jump in mid air. 我需要它,以便当角色在方块上时,他可以跳起来,但不能在空中半跳。 Right now I have it so that the character is able to jump on the ground but as soon as the character touches jumps on a set of blocks, he starts to bounce up and down and isn't able to jump while gliding on the blocks. 现在我有了它,以便角色可以在地面上跳跃,但是一旦角色触摸在一组积木上跳跃,他就会开始上下反弹,并且在滑到积木上时不能跳跃。 Can anyone help please? 有人可以帮忙吗?

onblock = False
for i in squares_list:
        if player_rect.bottom <= 560 and player_rect.colliderect(i):
            onblock = True
            player_rect.bottom = i.top + 1
        if player_rect.collidepoint((i.topleft[0], i.topleft[1]+1)):
            print ('Game Over')
    if event.type == KEYDOWN:     # if space is pressed the character jumps
            if event.key == K_SPACE:
                print(onblock)
                if onblock or player_rect.bottom == screen.get_rect().bottom  :  # prevents double jumps
                    vel_y = -20   # Makes the character jump up
                    player_rect.y -= 1
    if onblock:
        gravity = 0
        vel_y = 0
        current_angle = 0
    else:
        gravity = 1
        vel_y += gravity

    onblock = False

I don't know if you're indention is wrong just in this post or in your game, but according to this post, onblock will always be False after every loop. 我不知道您的缩进是否只是在这篇文章或您的游戏中是错误的,但是根据这篇文章,onblock在每个循环之后始终为False。 This might be the problem. 这可能是问题所在。

The other possible problem might be that you are placing your character 1 pixel above the platform when a collision happens. 另一个可能的问题是,发生碰撞时,您将角色放置在平台上方1个像素处。 This means that the next time you check for collision the character will not be resting on the block and therefore the onblock will be False. 这意味着下次您检查碰撞时,角色将不会停留在块上,因此onblock将为False。

The first problem is easily fixed by removing the last line onblocks = False . 通过删除最后一行onblocks = False可以轻松解决第一个问题。 The second problem can be fixed with player_rect.bottom = i.top without the +1. 第二个问题可以使用player_rect.bottom = i.top而不用+1来解决。

Watch this talk for more information, especially around this timestamp. 观看此演讲以获取更多信息,尤其是在时间戳附近。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM