A very basic way of getting the camera centered on the player would be to just offset everything you draw so that the player is always in the center of the camera. In my own game, I use a function to translate coordinates:
def to_pygame_coords(coords):
# move the coordinates so that 0, 0 is the player's position
# then move the origin to the center of the window
return coords - player.position.center + window.position.center
To expand on this so that it's not absolutely positioned on the player, you can instead center the window upon a box. Then you update the center of the box such that if the player leaves the box, the box will move along with him (thus moving the camera).
Pseudo code (not tested for negative coordinates):
BOX_WIDTH = 320
BOX_HEIGHT = 240
box_origin = player.position.center
def update_box(player_coords):
if player_coords.x - box_origin.x > BOX_WIDTH:
box_origin.x = player_coords.x - BOX_WIDTH
elif box_origin.x - player_coords.x > BOX_WIDTH:
box_origin.x = player_coords.x + BOX_WIDTH
if player_coords.y - box_origin.y > BOX_HEIGHT:
box_origin.y = player_coords.y - BOX_HEIGHT
elif box_origin.y - player_coords.y > BOX_HEIGHT:
box_origin.y = player_coords.y + BOX_HEIGHT
def to_pygame_coords(coords):
# move the coordinates so that 0, 0 is the box's position
# then move the origin to the center of the window
return coords - box_origin + window.position.center