Skip to main content

Pygame Scrolling - Python 2.7

Project description


for Python 2.7 and Pygame 1.9

A simple, fast module for adding scrolling maps to your new or existing game.

Compatible with pytmx (use the python2 branch):

What the heck is it?

pyscroll is a generic module for making a fast scrolling image with PyGame. It uses a lot of magic to get reasonable
framerates out of PyGame. It only exists to draw a map. It doesn't load images or data, so you can use your own custom
data structures, tile storage, ect.

The included class, BufferedRenderer, gives great framerates, supports layered rendering and can draw itself. It uses
more memory than a typical map would, but gives much better performance.


- Fast and small footprint
- Layered drawing
- Dirty screen updates


Basically, you need an object that conforms to a simple protocol defined in pyscroll.TiledMapData. And as luck would
have it, I've included an object that works with my Tiled TMX library.

# Using a pyscroll map layer

pyscroll isn't a replacement for any PyGame objects. You can it it along with your Sprites and SpriteGroups.

# Load TMX data (optional)
tmx_data = pytmx.load_pygame("desert.tmx")

# Make data source for the map
map_data = pyscroll.TiledMapData(tmx_data)

# Make the scrolling layer
size = (400, 400)
map_layer = pyscroll.BufferedRenderer(map_data, size)

# Center the layer on a pixel, 200))

# Draw the layer
rect = pygame.Rect(0,0,200,200)
map_layer.draw(screen, rect)

# Draw the layer in the background (optional)

See the demo in tests for code.

Adapting Existing Games / Map Data

pyscroll can be used with existing map data, but you will have to create a class to interact with pyscroll
or adapt your data handler to have these functions / attributes:

class MyData:
def __init__(self, tmx):
self.default_image = generate_default_image((tmx.tilewidth, tmx.tileheight))

def tilewidth(self):
Return pixel width of map tiles

def tileheight(self):
Return pixel height of map tiles

def width(self):
Return number of tiles on X axis

def height(self):
Return number of tiles on Y axis

def visible_layers(self):
Return a list of layer numbers that are visible.
If using a single layer map, just return [0]

def get_tile_image(self, position):
Return a surface for this position.
Return self.default_image if there is not map data for the position.

position is x, y, layer tuple

def convert(self, surface=None, depth=None, flags=0):
Optional. Convert the surfaces to match the display.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date (12.8 kB) Copy SHA256 hash SHA256 Source None

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page