【问题标题】:Pygame double jumpPygame二段跳
【发布时间】:2015-06-24 06:38:33
【问题描述】:

我几乎放弃了,所以我需要帮助。 我正在尝试制作一个具有双跳的简单平台游戏... 我一直在兜圈子,试图找到有用的东西 到目前为止,我最好的想法是比较滴答声的数量,但是每次我得到一些想法时,我都设法把它搞砸了,我不知道如何...... 有什么简单的方法吗?

请忽略任何不必要的变量,这只是一个示例

Clock=pygame.time.Clock()

t=0
a=0
b=0
f=0
m=0

while True:
    Clock.tick(180)
    for event in pygame.event.get():
        if event.type == QUIT:       
            pygame.quit()
            sys.exit()
        if event.type == KEYDOWN:
            if event.key==K_SPACE and b==0:
                movey=-1
                a=1
                t=pygame.time.get_ticks()
            if event.key==K_SPACE and b==1:
                f=pygame.time.get_ticks()
                if f<=t+238:
                    a=1
                else:
                    pass
        if event.type == KEYUP:
            if event.key==K_SPACE and b==0 and a==1:
                a=0
                b=1
            if event.key==K_SPACE and b==1 and a==1:
                m=1
                b=3
                s=y
    if m==1:
        y+=movey
        if y==s-32:
            m==0

    elif y<=312 and movey==-1:
        movey=+1
    elif y==344 and movey==+1:
        movey=0
        a=0
        b=0
    else:
        y+=movey

现在这是我正在尝试的......

【问题讨论】:

  • 你实现了单跳吗?您添加到 y 速度。之后,如果再次点击跳转,您可以切换布尔“can_double_jump”。当你触地时,它会重置。
  • ...让我们假装我不知道您的意思...例如,您能给出更长的解释吗?
  • 请帮助,任何人......
  • @user2154113 他基本上在做的是创建一个变量jump 并赋予它三种状态。当它在第一个时,你在地上并且可以跳跃。当它在第二个时,你在空中,还没有完成二段跳,所以仍然可以跳。第三,你已经二段跳了,还不能着地才能跳。

标签: python pygame


【解决方案1】:
g = -1  # gravity
floor = 0  # where frog stands


class Frog():  # say you have a frog
    def __init__(self):
        self.y = 0  # distance from ground
        self.y_speed = 0  # speed
        self.jumping = 0  # jumping status

    def jump(self):
        if self.jumping == 0:
            self.y_speed = 9  # a big jump
            self.jumping = 1  # change jumping status
        # I want the small jump available only when falling
        elif self.jumping == 1 and self.y_speed <= 0:
            self.y_speed = 5  # a small one
            self.jumping = 2  # change jumping status

    def update(self):  # this is called by mainloop
        self.y_speed += g  # change the acceleration
        self.y = max(self.y + self.y_speed, floor)  # don't want fall off
        if self.y == 0:  # on the ground again!
            self.jumping = 0  # reset jump
            self.y_speed = 0  # reset speed

【讨论】:

  • 和我差不多,但我一个人跑。
  • 非常好的例子,你可以在开头定义状态,这样在没有 cmets 的情况下更容易阅读。
【解决方案2】:

两个例子:

您可以强制在两次跳远之间设置最短时间,但为了简化示例,我将其省略了。询问您是否还有其他问题。

【讨论】:

  • 我认为这让我朝着正确的方向前进,但仍然可以解决
【解决方案3】:

使用计数器不是更容易吗?只需在您的播放器中设置一个计数器变量 = 0,然后定义您的跳跃并设置计数器 += 1。然后说如果计数器

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多