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? I'd like to have the "map" in a single surface, that will be blit to the screen surface. If doing so I could just build the map once and then just somehow adjust it's position so that the player will always stay in the center of the screen. My player updates it's position like this:
def update(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()) # Creates a rect for the player
collided = False
for o in self.objects: # Loops for solid objects in the map
if o.colliderect(entityrect):
collided = True
break
if not collided:
# If the player didn't collide, update the position
self.pos = newpos
return collided
I found this, but that was for a sideviewed platformer. So my map would look something like this:
map1 = pygame.Surface((3000, 3000))
img = pygame.image.load("floor.png")
for x in range(0, 3000, img.get_width()):
for y in range(0, 3000, img.get_height()):
map1.blit(img, (x, y))
So how would I do the camera movement? Any help would be appreciated.
PS. I hope you can understand what I'm asking here, english is not my native language. =)