Skip to content Skip to sidebar Skip to footer

Python Pygame Camera Movement

So, I'm making a 2d topviewed game in Python with Pygame. I've been trying to create a camera movement that would keep the player in the center of the screen. How would I do this?

Solution 1:

Well, you didn't show how you draw your map or your player, but you can do something like this:

camera = [0,0]
...
defupdate(self, dx=0, dy=0):
    newpos = (self.pos[0] + dx, self.pos[1] + dy)  # Calculates a new position
    entityrect = pygame.Rect(newpos, self.surface.get_size())
    camera[0] += dx
    camera[1] += dy
    ...

Then you draw your map like this

screen.blit(map1, (0,0), 
            (camera[0], camera[1], screen.get_width(), screen.get_height())
           )

This way the map will scroll in the opposite direction of the camera, leaving the player still.

If you wan't the player to move in your world, but not to move in the screen, you can do something like this:

screen.blit(player, (player.pos[0]-camera[0], player.pos[1]-camera[1]))

Post a Comment for "Python Pygame Camera Movement"