def load(self, fileName): self.tmxMap = tmxloader.load_pygame(fileName) num_tile_y = self.tmxMap.height num_tile_x = self.tmxMap.width tileWidth = self.tmxMap.tilewidth tileHeight = self.tmxMap.tileheight self.size = (num_tile_x*tileWidth, num_tile_y*tileHeight) #search for collision layer collisionLayerIndex = 2 for layerIndex in xrange(len(self.tmxMap.layers)): if self.tmxMap.layers[layerIndex].name == "collision": ## print("found") collisionLayerIndex = layerIndex break #generate position of every tile image in the world map and collidable rect for tile_y in range(num_tile_y): self.posList.append([]) for tile_x in range(num_tile_x): self.posList[tile_y].append((tile_x*tileWidth,tile_y*tileHeight)) #generate collidable rect if self.tmxMap.get_tile_image(tile_x, tile_y, collisionLayerIndex): tempRect = pygame.Rect(tile_x*tileWidth, tile_y*tileHeight, self.tmxMap.tilewidth, self.tmxMap.tileheight) oldbottom = tempRect.midbottom tempRect = tempRect.inflate(-10, -10) tempRect.midbottom = oldbottom self.unwalkable.append(tempRect)
def __init__(self, path, num): self.save_path = None self.map_path = None self.height = 0 self.width = 0 self.crates = [] self.buttons = [] self.gates = [] self.bosses = [] self.shooters = [] self.enemies = [] self.background = [] self.foreground = [] self.top = [] # If path an num are provided, find the corresponding .json and .tmx # files and load them if path and num: self.save_path = '{0}{1}'.format(path, num) self.map_path = '{0}{1}'.format(MAPS_DIR, num) tmxdata = tmxloader.load_pygame('{0}.tmx'.format(self.map_path), pixelalpha=True) self.height = tmxdata.height self.width = tmxdata.width data = json.loads(open('{0}.json'.format(self.save_path)).read()) self._initialize_entities(data) self._initialize_map(tmxdata)
def __init__(self, path, size, **kwargs): import tmxloader import quadtree data = tmxloader.load_pygame(path, **kwargs) self.colorkey = kwargs.get("force_colorkey", False) self.default_image = generateDefaultImage((data.tilewidth, data.tileheight)) left, self.xoffset = divmod(size[0] / 2, data.tilewidth) top, self.yoffset = divmod(size[1] / 2, data.tileheight) self.view = Rect(left,top, (size[0] / data.tilewidth), (size[1] / data.tileheight)) self.oldX = self.xoffset + (left * data.tilewidth) self.oldY = self.yoffset + (top * data.tileheight) self.bufferWidth = size[0] + data.tilewidth * 2 self.bufferHeight = size[1] + data.tileheight * 2 self.size = size self.blitPerUpdate = 4 self.background_update = 1 self.data = data self.queue = [] self.buffer = Surface((self.bufferWidth, self.bufferHeight)) if self.colorkey: self.buffer.fill(self.colorkey) # the layer quadtree is used to correctly draw tiles that are over # sprites rects = [] p = product(range(self.view.width+2), range(self.view.height+2)) for x, y in p: rect = Rect((x*self.data.tilewidth,y*self.data.tileheight), (self.data.tilewidth, self.data.tileheight)) rects.append(rect) self.layerQuadtree = quadtree.QuadTree(rects, 4) self.blank = True
def load(self, fileName, name): self.name = name self.tmxMap = tmxloader.load_pygame(fileName) num_tile_y = self.tmxMap.height num_tile_x = self.tmxMap.width tileWidth = self.tmxMap.tilewidth tileHeight = self.tmxMap.tileheight self.size = (num_tile_x*tileWidth, num_tile_y*tileHeight) #search for collision layer collisionLayerIndex = 2 for layerIndex in xrange(len(self.tmxMap.layers)): if self.tmxMap.layers[layerIndex].name == "collision": ## print("found") collisionLayerIndex = layerIndex break firstgid = [tileset.firstgid for tileset in self.tmxMap.tilesets if tileset.name == "collision"][0] collisionMap = { firstgid + 1:[pygame.Rect(0,0,tileWidth,tileHeight)], # full firstgid + 2:[pygame.Rect(0,0,tileWidth/2,tileHeight/2)], # left-top firstgid + 3:[pygame.Rect(tileWidth/2,0,tileWidth/2,tileHeight/2)], # right-top firstgid + 4:[pygame.Rect(0,tileHeight/2,tileWidth/2,tileHeight/2)], # left-bottom firstgid + 5:[pygame.Rect(tileWidth/2,tileHeight/2,tileWidth/2,tileHeight/2)], # right-bottom firstgid + 6:[pygame.Rect(0,0,tileWidth,tileHeight/2)], # top firstgid + 7:[pygame.Rect(0,tileHeight/2,tileWidth,tileHeight/2)], # bottom firstgid + 8:[pygame.Rect(tileWidth/2,0,tileWidth/2,tileHeight)], # right firstgid + 9:[pygame.Rect(0,0,tileWidth/2,tileHeight)], # left firstgid + 10:[pygame.Rect(0,tileHeight/2,tileWidth/2,tileHeight/2),pygame.Rect(tileWidth/2,0,tileWidth/2,tileHeight)], # !left-top firstgid + 11:[pygame.Rect(0,0,tileWidth/2,tileHeight),pygame.Rect(tileWidth/2,tileHeight/2,tileWidth/2,tileHeight/2)], # !right-top, firstgid + 12:[pygame.Rect(0,0,tileWidth,tileHeight/2),pygame.Rect(tileWidth/2,tileHeight/2,tileWidth/2,tileHeight/2)], # !left-bottom firstgid + 13:[pygame.Rect(0,0,tileWidth,tileHeight/2),pygame.Rect(0,tileHeight/2,tileWidth/2,tileHeight/2)], # !right-bottom } #generate position of every tile image in the world map and collidable rect for tile_y in range(num_tile_y): self.posList.append([]) for tile_x in range(num_tile_x): self.posList[tile_y].append((tile_x*tileWidth,tile_y*tileHeight)) #generate collidable rect # no, full, left-top, right-top, left-bottom, right-bottom, top, bottom, right, left, !left-top, !right-top, !left-bottom, !right-bottm tileId = self.tmxMap.getTileGID(tile_x, tile_y, collisionLayerIndex) if tileId in collisionMap: for rectTemplate in collisionMap[tileId]: tempRect = rectTemplate.copy() tempRect.left = tempRect.left + tile_x*tileWidth tempRect.top = tempRect.top + tile_y*tileHeight self.unwalkable.append(tempRect)
def __init__(self, filename): self.filename = filename self.tiledmap = tmxloader.load_pygame(self.filename) super(TiledEnvironment, self).__init__()