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