0

I am having trouble having a rectangle jumping only when it touches a platform. I have tried putting an if-statement within the jump method stating if the rectangle is colliding with any of the blocks within the block list for the rectangle to b able to jump but still has not worked.

def keydown_events(self, event):
    """This method handles keydown events."""
    if event.key == pygame.K_ESCAPE:
        exit()
    if event.key == pygame.K_SPACE:
            self.jump()

def jump(self):
    """This method controls the player's jump."""
    jump = pygame.key.get_pressed()
    if jump[pygame.K_SPACE]:
        self.player.vel.y = -self.settings.jump_height

def collide(self):
    for each in self.map.blocks:
        if self.player.rect.colliderect(each):
            self.player.pos.y = each.top + 1
            self.player.vel.y = 0
            if self.player.vel.x < 0:
                self.player.rect.left = each.right
            elif self.player.vel.x > 0:
                self.player.rect.right = each.right
  • why are you checking in the `jump` function if `SPACE` has been pressed? the function gets called only if the spacebar is pressed already. Also you haven't shown what you attempted. usually you would just have some flag like `can_jump` and set it to True when a collision happens and to False when there is no collision – Matiiss Nov 15 '21 at 19:46
  • Related: [How to make a character jump in Pygame?](https://stackoverflow.com/questions/65873880/how-to-make-a-character-jump-in-pygame/65874132#65874132) --- [How can I do a double jump in pygame?](https://stackoverflow.com/questions/67667103/how-can-i-do-a-double-jump-in-pygame/67667585#67667585) --- [python, pygame - jumping too fast?](https://stackoverflow.com/questions/58474204/python-pygame-jumping-too-fast/58474280#58474280) – Rabbid76 Nov 15 '21 at 20:03

0 Answers0