From fe8e0249c1916c74eed97d9750b16b03e87953e7 Mon Sep 17 00:00:00 2001 From: raylu Date: Sat, 12 Oct 2013 03:48:28 -0700 Subject: collision detection! --- tileset.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'tileset.py') diff --git a/tileset.py b/tileset.py index 017f995..f24efd6 100644 --- a/tileset.py +++ b/tileset.py @@ -5,6 +5,7 @@ import pygame class Tileset(object): TILESIZE = 16 + MAPSIZE = 50 RIGHT = 0 DOWN = 1 LEFT = 2 @@ -56,7 +57,14 @@ class Tileset(object): # read as array of 4-byte ints array = list(struct.unpack('<{}i'.format(len(data)/4), data)) loaded[name] = array + self.level_array = loaded['level'] + collidables = frozenset([6, 7, 8, 22, 24, 38, 39, 40]) + collisions = set() + for i, tile in enumerate(loaded['level']): + if tile in collidables: + y, x = divmod(i, self.MAPSIZE) + collisions.add((x, y)) units = { self.ENEMY1: [], @@ -64,15 +72,15 @@ class Tileset(object): } for i, tile in enumerate(loaded['units']): if tile in units.iterkeys(): - y, x = divmod(i, 50) + y, x = divmod(i, self.MAPSIZE) units[tile].append((x, y)) if len(units[self.PLAYER]) != 1: raise Exception('expected exactly 1 player tile in units layer, found ' + str(len(units[self.PLAYER]))) - return units + return collisions, units def render_level(self): for i, tile in enumerate(self.level_array): - y, x = divmod(i, 50) + y, x = divmod(i, self.MAPSIZE) x *= self.TILESIZE y *= self.TILESIZE tile_y, tile_x = divmod(tile - 1, self.TILESIZE) # those 1-indexing fuckers -- cgit v1.2.3