python - Pygame:谁能帮我实现双跳?

标签 python pygame

我知道已经有其他关于此的帖子,但我的运动系统与我发现的那些有一点不同,所以后来我问了这个问题。

我的移动系统基于名为 Move(up,left,right,down) 的命名元组 然后是这个:

def update(self, move, blocks):
    # check if we can jump 
    if move.up and self.on_ground:
        self.yvel -= self.jump_speed
    # simple left/right movement
    if move.left:
            self.xvel = -self.move_speed
    if move.right:
            self.xvel = self.move_speed

    # if in the air, fall down
    if not self.on_ground:
        self.yvel += 0.3
        # but not too fast
        if self.yvel > max_gravity: self.yvel = max_gravity

    # if no left/right movement, x speed is 0, of course
    if not (move.left or move.right):
        self.xvel = 0

    # move horizontal, and check for horizontal collisions
    self.rect.left += self.xvel
    self.collide(self.xvel, 0, blocks)

    # move vertically, and check for vertical collisions
    self.rect.top += self.yvel
    self.on_ground = False
    self.collide(0, self.yvel, blocks)

def collide(self, xvel, yvel, blocks):
    # all blocks that we collide with
    for block in [blocks[i] for i in self.rect.collidelistall(blocks)]:

        # if xvel is > 0, we know our right side bumped 
        # into the left side of a block etc.
        if xvel > 0:
                self.rect.right = block.rect.left;self.xvel=0
        if xvel < 0:
                self.rect.left = block.rect.right;self.xvel=0

        # if yvel > 0, we are falling, so if a collision happpens 
        # we know we hit the ground (remember, we seperated checking for
        # horizontal and vertical collision, so if yvel != 0, xvel is 0)
        if yvel > 0:
            self.rect.bottom = block.rect.top
            self.on_ground = True
            self.yvel = 0
        # if yvel < 0 and a collision occurs, we bumped our head
        # on a block above us
        if yvel < 0: self.rect.top = block.rect.bottom;self.yvel=0

我尝试将第五个变量添加到名为 upUp 的元组中,当调用它时它会触发另一个跳转,无论 on_ground 是否为真。

为了触发它,我在事件循环中使用了这个:

if e.type==KEYUP:
    if dj==0:
        dj=-1
    if dj=-1:
        dj='true'
Move(K_w,K_a,K_d,K_s,dj)

但这根本行不通! 有人有什么建议吗?

最佳答案

您必须保持某种状态以跟踪您所处的跳跃“阶段”。

这些“阶段”是:

  • 在地面上
  • 做跳跃(按下跳跃按钮)
  • 跳跃并在半空中(释放跳跃按钮)
  • 双跳(再次按下跳跃按钮)

所以你应该能够做这样的事情:

def update(self, move, blocks):
    if self.on_ground:
        self.jump_state = 'on_ground'

    if move.up and self.on_ground:
        # jump!
        self.yvel -= self.jump_speed
        self.jump_state = 'jumped'

    if self.jump_state = 'jumped' and not move.up and not self.on_ground:
        self.jump_state = 'ready_for_double_jump'

    if self.jump_state = 'ready_for_double_jump' and move.up:
        # jump!
        self.yvel -= self.jump_speed
        self.jump_state = 'double_jumped'

    ...

你会明白的。

关于python - Pygame:谁能帮我实现双跳?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20610845/

相关文章:

python - PyGame 正在卡住

python - numpy.i 不见了。推荐的安装方式是什么?

python - 在 Python 中将数字字符串的键拆分为单个数字键

python - 多处理池不适用于嵌套函数

python - 不返回 YAML 文件的更新并在字符串更新时返回 TypeError

python - 如何在 Python 上安装 PyGame?

python - 添加每个元素后 Pygame 屏幕不更新

python - pandas 中的 groupby 是创建数据的副本还是只是一个 View ?

python - 如何在Python中存储声音?

python - 为什么 pygame 在退出之前需要 time.sleep 来播放声音?