all repos — Legends-RPG @ e32036476094d15926f22b90346e2fd34f0107b5

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