all repos — Legends-RPG @ f2dca0d393a15a440a0a97993509345ca515d575

A fantasy mini-RPG built with Python and Pygame.

data/tilerender.py (view raw)

 1"""
 2This is a test of using the pytmx library with Tiled.
 3"""
 4import sys
 5import pygame as pg
 6import pytmx
 7
 8class Renderer(object):
 9    """
10    This object renders tile maps from Tiled
11    """
12    def __init__(self, filename):
13        tm = pytmx.load_pygame(filename, pixelalpha=True)
14        self.size = tm.width * tm.tilewidth, tm.height * tm.tileheight
15        self.tmx_data = tm
16
17    def render(self, surface):
18
19        tw = self.tmx_data.tilewidth
20        th = self.tmx_data.tileheight
21        gt = self.tmx_data.getTileImageByGid
22
23        if self.tmx_data.background_color:
24            surface.fill(self.tmx_data.background_color)
25
26        for layer in self.tmx_data.visibleLayers:
27            if isinstance(layer, pytmx.TiledLayer):
28                for x, y, gid in layer:
29                    tile = gt(gid)
30                    if tile:
31                        surface.blit(tile, (x * tw, y * th))
32
33            elif isinstance(layer, pytmx.TiledObjectGroup):
34                pass
35
36            elif isinstance(layer, pytmx.TiledImageLayer):
37                image = gt(layer.gid)
38                if image:
39                    surface.blit(image, (0, 0))
40
41    def make_2x_map(self):
42        temp_surface = pg.Surface(self.size)
43        self.render(temp_surface)
44        temp_surface = pg.transform.scale2x(temp_surface)
45        return temp_surface