| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- 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))
|