Im trying to add collision detection on a rock sprite using the following code;
import pygame
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
class Player(pygame.sprite.Sprite):
def __init__(self, filename, x, y):
super().__init__()
self.image = pygame.image.load(filename).convert()
self.image.set_colorkey(BLACK)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.change_x = 0
self.change_y = 0
def changespeed(self, x, y):
self.change_x += x
self.change_y += y
def update(self):
self.rect.x += self.change_x
self.rect.y += self.change_y
def rockCollision(self, rock):
block_hit_list = pygame.sprite.spritecollide(self, rock, False)
for block in block_hit_list:
if self.change_x > 0:
self.rect.right = block.rect.left
else:
self.rect.left = block.rect.right
block_hit_list = pygame.sprite.spritecollide(self, rock, False)
for block in block_hit_list:
if self.change_y > 0:
self.rect.bottom = block.rect.top
else:
self.rect.top = block.rect.bottom
class Rock(pygame.sprite.Sprite):
def __init__(self, filename, x, y):
super().__init__()
self.image = pygame.image.load(filename).convert()
self.image.set_colorkey(BLACK)
self.rect = self.image.get_rect().inflate(1, 1)
self.rect.y = y
self.rect.x = x
pygame.init()
screen_width = 1080
screen_height = 607
screen = pygame.display.set_mode([screen_width, screen_height])
pygame.display.set_caption('Labyrinth')
block_list = pygame.sprite.Group()
all_sprites_list = pygame.sprite.Group()
rock_list = pygame.sprite.Group()
rock = Rock("Rock.png", 380, 280)
rock_list.add(rock)
player = Player("Isaac.png", 420, 150)
all_sprites_list.add(player)
clock = pygame.time.Clock()
background_position = [0, 0]
background_image = pygame.image.load("Floor.png").convert()
done = False
# -- MAIN PROGRAM LOOP -- #
# -- Event processing --
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
# Player controls
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player.changespeed(-7, 0)
elif event.key == pygame.K_RIGHT:
player.changespeed(7, 0)
elif event.key == pygame.K_UP:
player.changespeed(0, -7)
elif event.key == pygame.K_DOWN:
player.changespeed(0, 7)
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
player.changespeed(7, 0)
elif event.key == pygame.K_RIGHT:
player.changespeed(-7, 0)
elif event.key == pygame.K_UP:
player.changespeed(0, 7)
elif event.key == pygame.K_DOWN:
player.changespeed(0, -7)
# -- Game Logic --
all_sprites_list.update()
# Hit detection
blocks_hit_list = pygame.sprite.spritecollide(player, block_list, True)
player.rockCollision(rock_list)
screen.blit(background_image, background_position)
all_sprites_list.draw(screen)
rock_list.draw(screen)
pygame.display.flip()
clock.tick(60)
pygame.quit()
Moving left or right into the rock, the collision works perfectly. However, moving up or down into the rock clips my player to the right of the rock (I can never actually walk up into the rock and get stuck there i get immediately clipped to the right).
My question is why does this happen? I can see no reason as to why it only happens when i walk up or down into the rock, not left or right as the code for each is identical.
I want to be able to walk into the rock while walking up or down and be able to actually get stuck on the rock instead of clipping around it