You need to apply an offset to the position of your entities when drawing them. Let's call that offset a camera , since this is the effect we want to achieve with this.

First of all, we can't use the draw function of the sprite group, since the sprites don't need to know that their position ( rect ) is not the position they are going to be drawn on the screen (At the end, we'll subclass the Group class and reimplement the it's draw to be aware of the camera, but let's start slow).

Let's start by creating a Camera class to hold the state of the offset we want to apply to the position of our entities:

class Camera(object): def __init__(self, camera_func, width, height): self.camera_func = camera_func self.state = Rect(0, 0, width, height) def apply(self, target): return target.rect.move(self.state.topleft) def update(self, target): self.state = self.camera_func(self.state, target.rect)

some things to note here:

We need to store the position of the camera, and the width and height of the level in pixels (since we want to stop scrolling at the edges of the level). I used a Rect to store all these informations, but you could easily just use some fields.

Using Rect comes in handy in the apply function. This is where we re-calculate the position of an entity on the screen to apply the scrolling.

Once per iteration of the main loop, we need to update the position of the camera, hence there's the update function. It just alters the state by calling the camera_func function, which will do all the hard work for us. We implement it later.

Let's create an instace of the camera:

for row in level: ... total_level_width = len(level[0])*32 # calculate size of level in pixels total_level_height = len(level)*32 # maybe make 32 an constant camera = Camera(*to_be_implemented*, total_level_width, total_level_height) entities.add(player) ...

and alter our main loop:

# draw background for y in range(32): ... camera.update(player) # camera follows player. Note that we could also follow any other sprite # update player, draw everything else player.update(up, down, left, right, running, platforms) for e in entities: # apply the offset to each entity. # call this for everything that should scroll, # which is basically everything other than GUI/HUD/UI screen.blit(e.image, camera.apply(e)) pygame.display.update()

Our camera class is already very flexible and yet dead simple. It can use different kinds of scrolling (by providing different camera_func functions), and it can follow any arbitary sprite, not just the player. You even can change this at runtime.

Now for the implementation of camera_func . A simple approach is to just center the player (or whichever entity we want to follow) at the screen, and the implementation is straight forward:

def simple_camera(camera, target_rect): l, t, _, _ = target_rect # l = left, t = top _, _, w, h = camera # w = width, h = height return Rect(-l+HALF_WIDTH, -t+HALF_HEIGHT, w, h)

We just take the position of our target , and add the half total screen size. You can try it by creating your camera like this:

camera = Camera(simple_camera, total_level_width, total_level_height)

So far, so good. But maybe we don't want to see the black background outside the level? How about:

def complex_camera(camera, target_rect): # we want to center target_rect x = -target_rect.center[0] + WIN_WIDTH/2 y = -target_rect.center[1] + WIN_HEIGHT/2 # move the camera. Let's use some vectors so we can easily substract/multiply camera.topleft += (pygame.Vector2((x, y)) - pygame.Vector2(camera.topleft)) * 0.06 # add some smoothness coolnes # set max/min x/y so we don't see stuff outside the world camera.x = max(-(camera.width-WIN_WIDTH), min(0, camera.x)) camera.y = max(-(camera.height-WIN_HEIGHT), min(0, camera.y)) return camera

Here we simply use the min / max functions to ensure we don't scroll outside out level.

Try it by creating your camera like this:

camera = Camera(complex_camera, total_level_width, total_level_height)

There's a little animation of our final scrolling in action:

Here's the complete code again. Note I changed some things:

the level is bigger and to have some more platforms

use python 3

use a sprite group to handle the camera

refactored some duplicate code

since Vector2/3 is now stable, use them for easier math

get rid of that ugly event handling code and use pygame.key.get_pressed instead