Pygame双跳

时间:2013-03-17 16:51:15

标签: python pygame

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

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

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

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

3 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:1)

两个例子:

你可以强制双跳之间的最短时间,但我把它留下来简化示例。询问您是否还有其他问题。

答案 2 :(得分:-2)

使用柜台会不会更容易?只需在你的播放器中设置一个计数器变量= 0,然后定义你的跳转并设置计数器+ = 1.然后说如果计数器&lt; 2然后你就可以跳了。不是吗?