How do I prevent the player from moving through the walls in a maze?

Use mask collision. Draw the maze of a transparent pygame.Surface: cell_size = 40 maze = Maze() maze_surf = pygame.Surface((maze.size[0]*cell_size, maze.size[1]*cell_size), pygame.SRCALPHA) draw_maze(maze_surf, maze, 0, 0, cell_size, (196, 196, 196), 3) Crate a pygame.Mask from the Surface with pygame.mask.from_surface: maze_mask = pygame.mask.from_surface(maze_surf) Create a mask form the player: player_rect = pygame.Rect(190, 190, 20, 20) player_surf = … Read more

How to implement barriers to stop the player moving through walls

Compute the bounding rectangle of the player and compute the grid indices of the corner points and and center point: player_rect = pygame.Rect(player.x, player.y, wr-3, hr-3) xC, yC = int(player_rect.centerx / wr), int(player_rect.centery / hr) x0, y0 = int(player_rect.left / wr), int(player_rect.top / hr) x1, y1 = int(player_rect.right / wr), int(player_rect.bottom / hr) Restrict the … Read more

Adding collision to maze walls

Ensure that the Player object is positioned in the center of cell of the grid. e.g. calculate a random position for the player: def load_player(background): pimg = pygame.Surface((10, 10)) pimg.fill((200, 20, 20)) px = random.randint(0, rows-1) * width + width//2 py = random.randint(0, cols-1) * width + width//2 return Player(pimg, (px, py), background) To track … Read more

What’s a good algorithm to generate a maze? [closed]

It turns out there are 11 classic algorithms to generate “perfect” mazes. A maze is perfect if it has one, and only one, solution. Here are some links to each algorithm, in rough order of my preference. Kruskal’s Prim’s Recursive Backtracker Aldous-Broder Growing Tree Hunt-and-Kill Wilson’s Eller’s Recursive Division (Predictable) Sidewinder (Predictable) Binary Tree (Flawed) … Read more

Representing and solving a maze given an image

Here is a solution. Convert image to grayscale (not yet binary), adjusting weights for the colors so that final grayscale image is approximately uniform. You can do it simply by controlling sliders in Photoshop in Image -> Adjustments -> Black & White. Convert image to binary by setting appropriate threshold in Photoshop in Image -> … Read more