#๐Ÿ”’ Player sprite stops moving

4 messages ยท Page 1 of 1 (latest)

dusk violet
#

Hi, its my first time creating a game in python using pygame. My player sprite would move fine horizontally across the whole map however when adding collisions it will only move a few pixels right or left and then it will stop even though I have no collisions in the places where they are being stopped. I'm also using a software called tiled to create collisions and the tilemap. Here is my code:
def input(self):

    key = pygame.key.get_pressed()
    input_vector = vector(0,0)
    if key[pygame.K_LEFT]:
        input_vector.x -= 1
    if key[pygame.K_RIGHT]:
        input_vector.x += 1
    self.direction = input_vector.normalize() if input_vector else input_vector


    

def move(self, dt):
    
    self.rect.x += self.direction.x * self.speed * dt
    self.collision('horizontal')

    # self.direction.y += self.gravity * dt
    # self.rect.y += self.direction.y 
    # self.collision('vertical')

def collision(self, axis):
    for sprite in self.collision_sprites:
        if sprite.rect.colliderect(self.rect):
            if axis == 'horizontal':
                #left
                if self.rect.left <= sprite.rect.right and self.old_rect.left >= sprite.old_rect.right:
                    self.rect.left = sprite.rect.right

                #right
                if self.rect.right >= sprite.rect.left and self.old_rect.right <= sprite.old_rect.left:
                    self.rect.right = sprite.rect.left

            else:
                pass
bronze roseBOT
#

@dusk violet

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

bronze roseBOT
#

@dusk violet

Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.