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

Bounding ellipse

You’re looking for the Minimum Volume Enclosing Ellipsoid, or in your 2D case, the minimum area. This optimization problem is convex and can be solved efficiently. Check out the MATLAB code in the link I’ve included – the implementation is trivial and doesn’t require anything more complex than a matrix inversion. Anyone interested in the … Read more

Detecting collision in Python turtle game

This code seems to be more wishful thinking than actual programming: def is_collided_with(self, run): return self.rect.colliderect(run.rect) runner = run(10, 10, ‘my_run’) follower = follow(20, 10) if follow.is_collided_with(run): print ‘collision!’ Turtles don’t have a .rect() method. You can’t simply add a is_collided_with() method to an existing class with this def statement. There are no run() and … 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