tileset.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import struct
  2. from xml.etree import ElementTree
  3. import pygame
  4. class Tileset(object):
  5. TILESIZE = 16
  6. MAPSIZE = 50
  7. RIGHT = 0
  8. DOWN = 1
  9. LEFT = 2
  10. UP = 3
  11. PLAYER = 209
  12. ENEMY1 = 161
  13. def __init__(self, screen):
  14. self.screen = screen
  15. image = pygame.image.load('tileset.png').convert()
  16. self.tileset = {
  17. self.RIGHT: image,
  18. self.UP: pygame.transform.rotate(image, 90),
  19. self.LEFT: pygame.transform.rotate(image, 180),
  20. self.DOWN: pygame.transform.rotate(image, 270),
  21. }
  22. self.tile_width, self.tile_height = image.get_size()
  23. self.tile_width /= self.TILESIZE
  24. self.tile_height /= self.TILESIZE
  25. def _render(self, tile_x, tile_y, pos, direction=RIGHT):
  26. tileset = self.tileset[direction]
  27. if direction == self.RIGHT:
  28. x, y = tile_x, tile_y
  29. elif direction == self.UP:
  30. x, y = tile_y, self.tile_width - tile_x - 1
  31. elif direction == self.LEFT:
  32. x, y = self.tile_width - tile_x - 1, self.tile_height - tile_y - 1
  33. elif direction == self.DOWN:
  34. x, y = self.tile_height -tile_y - 1, tile_x
  35. x *= self.TILESIZE
  36. y *= self.TILESIZE
  37. self.screen.blit(tileset, pos, (x, y, self.TILESIZE, self.TILESIZE))
  38. def render_person(self, pos, direction):
  39. self._render(0, 13, pos, direction)
  40. def load_level(self): # https://bitbucket.org/r1chardj0n3s/pygame-tutorial/src/a383dd24790d/tmx.py#cl-241
  41. level_data = ElementTree.parse('level1.tmx').getroot()
  42. layers = level_data.findall('layer')
  43. if len(layers) != 2:
  44. raise Exception('expected 2 layers: level and units')
  45. loaded = {}
  46. for layer in layers:
  47. name = layer.attrib['name']
  48. raw_data = layer.findall('data')[0].text.strip()
  49. data = raw_data.decode('base64').decode('zlib')
  50. # read as array of 4-byte ints
  51. array = list(struct.unpack('<{}i'.format(len(data)/4), data))
  52. loaded[name] = array
  53. self.level_array = loaded['level']
  54. collidables = frozenset([6, 7, 8, 22, 24, 38, 39, 40])
  55. collisions = set()
  56. for i, tile in enumerate(loaded['level']):
  57. if tile in collidables:
  58. y, x = divmod(i, self.MAPSIZE)
  59. collisions.add((x, y))
  60. units = {
  61. self.ENEMY1: [],
  62. self.PLAYER: [],
  63. }
  64. for i, tile in enumerate(loaded['units']):
  65. if tile in units.iterkeys():
  66. y, x = divmod(i, self.MAPSIZE)
  67. units[tile].append((x, y))
  68. if len(units[self.PLAYER]) != 1:
  69. raise Exception('expected exactly 1 player tile in units layer, found ' + str(len(units[self.PLAYER])))
  70. return collisions, units
  71. def render_level(self):
  72. for i, tile in enumerate(self.level_array):
  73. y, x = divmod(i, self.MAPSIZE)
  74. x *= self.TILESIZE
  75. y *= self.TILESIZE
  76. tile_y, tile_x = divmod(tile - 1, self.TILESIZE) # those 1-indexing fuckers
  77. self._render(tile_x, tile_y, (x, y))