diff options
author | raylu <ray.lu@getclever.com> | 2013-10-12 00:06:21 -0700 |
---|---|---|
committer | raylu <ray.lu@getclever.com> | 2013-10-12 00:21:51 -0700 |
commit | 948aec96350b7db0cd689f7ef997d14e9f27253c (patch) | |
tree | 286bfbb2a8d0ca0975d8524f093a8852bc0b6e5e /tileset.py | |
parent | 421129459f1ece0eca5dbcb14aa05112696482d8 (diff) | |
download | troll-948aec96350b7db0cd689f7ef997d14e9f27253c.tar.xz |
refactor tileset
Diffstat (limited to 'tileset.py')
-rw-r--r-- | tileset.py | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/tileset.py b/tileset.py new file mode 100644 index 0000000..fecebb1 --- /dev/null +++ b/tileset.py @@ -0,0 +1,56 @@ +import struct +from xml.etree import ElementTree + +import pygame + +class Tileset(object): + tilesize = 16 + RIGHT = 0 + DOWN = 1 + LEFT = 2 + UP = 3 + + def __init__(self, screen): + self.screen = screen + image = pygame.image.load('tileset.png').convert() + self.tileset = { + self.RIGHT: image, + self.UP: pygame.transform.rotate(image, 90), + self.LEFT: pygame.transform.rotate(image, 180), + self.DOWN: pygame.transform.rotate(image, 270), + } + self.tile_width, self.tile_height = image.get_size() + self.tile_width /= self.tilesize + self.tile_height /= self.tilesize + + def _render(self, tile_x, tile_y, pos, direction=RIGHT): + tileset = self.tileset[direction] + if direction == self.RIGHT: + x, y = tile_x, tile_y + elif direction == self.UP: + x, y = tile_y, self.tile_width - tile_x - 1 + elif direction == self.LEFT: + x, y = self.tile_width - tile_x - 1, self.tile_height - tile_y - 1 + elif direction == self.DOWN: + x, y = self.tile_height -tile_y - 1, tile_x + x *= self.tilesize + y *= self.tilesize + self.screen.blit(tileset, pos, (x, y, self.tilesize, self.tilesize)) + + def render_person(self, pos, direction): + self._render(0, 13, pos, direction) + + def load_level(self): # https://bitbucket.org/r1chardj0n3s/pygame-tutorial/src/a383dd24790d/tmx.py#cl-241 + level_data = ElementTree.parse('level1.tmx').getroot() + raw_data = level_data.findall('layer')[0].findall('data')[0].text.strip() + data = raw_data.decode('base64').decode('zlib') + # read as array of 4-byte ints + self.level_array = struct.unpack('<{}i'.format(len(data)/4), data) + + def render_level(self): + for i, tile in enumerate(self.level_array): + x, y = divmod(i, 50) + x *= self.tilesize + y *= self.tilesize + tile_y, tile_x = divmod(tile - 1, self.tilesize) # those 1-indexing fuckers + self._render(tile_x, tile_y, (y, x)) |