ARTICLE AD BOX
I'm working on a game where my character has to navigate through a cave. If the character collides with the cave walls, its position should be reset. I made a mask of the cave layout, with the main path left transparent. I'll include an image. When I check to see if the character mask and map mask are colliding, it says that they are, even when my character is within the proper pathway. This could definitely be an issue with offset, which I'm having some trouble grasping. How can I check for collisions correctly?
class Character: def __init__(self, x, y): self.image = pygame.image.load("Player.gif").convert_alpha() self.rect = self.image.get_rect() self.topleft = (x, y) self.mask = pygame.mask.from_surface(self.image) def draw(self, screen): screen.blit(self.image, self.rect) def main(): pygame.init() screen_size = width, height = 1200, 800 screen = pygame.display.set_mode(screen_size) map = pygame.image.load("background.png").convert_alpha() map_mask = pygame.mask.from_surface(map) mask_image = map_mask.to_surface() character = Character(50, 50) character_mask = character.mask.to_surface() clock = pygame.time.Clock() is_playing = True while is_playing: for event in pygame.event.get(): if event.type == pygame.QUIT: is_playing = False keys = pygame.key.get_pressed() if keys[pygame.K_w]: character.rect.move_ip(0, -7) if map_mask.overlap(character.mask, character.topleft): print("colliding") screen.fill((255,255,255)) screen.blit(mask_image, (0,0)) screen.blit(character_mask, (100, 200)) character.draw(screen) pygame.display.update() clock.tick(50) pygame.quit() sys.exit() if __name__=="__main__": main()
