[Python] Multiple key inputs at a time? [duplicate]

Discussão em 'Python' iniciado por Stack, Março 26, 2025.

  1. Stack

    Stack Membro Participativo

    So I have a slight problem with my collision detection. Everything with one direction is great; however, when I input more than one at a time, my player phases through the walls or teleports around them. I was looking up code on it on GitHub and YouTube; however, none of them could seem to fix it. Eventually I settled on the code below, but I doesn't work. I have looked at other StackOverflow questions from years ago and none of them applied to this or helped in any way. Maybe I overlooked something. The entire code is at: Game.

    Any help would be grateful and appreciated. Thankyou for your time.

    def collision(self):
    self.collision_sprites = pygame.sprite.spritecollide(self, entityManager.groups["Obstacles"], False)
    self.padding = 5

    for sprite in self.collision_sprites:
    # Horizontal collision
    if self.attributes["dx"] > 0: # Moving right
    self.rect.right = sprite.rect.left - self.padding
    elif self.attributes["dx"] < 0: # Moving left
    self.rect.left = sprite.rect.right + self.padding

    # Vertical collision
    if self.attributes["dy"] > 0: # Moving down
    self.rect.bottom = sprite.rect.top - self.padding
    elif self.attributes["dy"] < 0: # Moving up
    self.rect.top = sprite.rect.bottom + self.padding

    Continue reading...

Compartilhe esta Página