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