在皮格姆跳跃

2024-06-05 22:41:07 发布

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

我正在尝试在pygame中制作一个类似于geometry dash的游戏。除了跳跃部分,我什么都做了。我需要它,这样当角色在一个方块上时,他可以跳起来,但不能在半空中双跳。现在我有了这样的角色可以在地上跳跃,但是一旦角色接触到一组方块上的跳跃,他就会开始上下弹跳,在方块上滑行时无法跳跃。谁能帮忙吗?在

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

Tags: therecteventfalse角色ifgravity方块
1条回答
网友
1楼 · 发布于 2024-06-05 22:41:07

我不知道你的缩进是在这篇文章中还是在你的游戏中是错的,但是根据这篇文章,onblock在每次循环之后总是错误的。这可能就是问题所在。在

另一个可能的问题是,当发生碰撞时,您将角色放置在平台上方1个像素的位置。这意味着下次检查碰撞时,角色将不会停留在块上,因此onblock将为False。在

第一个问题可以通过删除最后一行onblocks = False轻松解决。第二个问题可以用不带+1的player_rect.bottom = i.top来解决。在

请看这个talk获取更多信息,尤其是this时间戳。在

相关问题 更多 >