def load_map(self, file): lines = file.readlines() # Load the tile map for idx in xrange(len(lines)): line = lines[idx] if line == " \n": self.tiles_high = idx self.height = self.tiles_high * self.tile_size for y in xrange(0, self.tiles_high): line = lines[y] parsed_comments = line.split('#') line_tiles = parsed_comments[0].replace(' ', '').replace('\n', '').split(',') self.tiles_wide = len(line_tiles) self.width = self.tiles_wide * self.tile_size self.num_tiles = self.tiles_wide * self.tiles_high x = 0 for key in line_tiles: tile = TileFactory.generateTile(key, x, y, self.game) self.tiles.append(tile) if not tile.walkable: self.unwalkable_tiles.add(tile) if not tile.visible: self.unjumpable_tiles.add(tile) if not tile.swimmable and not tile.walkable: self.unswimmable_and_unwalkable_tiles.add(tile) x += 1 index = self.tiles_high while lines[index] is "\n": index += 1 index += 1 # Load the sprite map for y in xrange(index, self.tiles_high + index): line = lines[y] parsed_comments = line.split('#') line_tiles = parsed_comments[0].replace(' ', '').replace('\n', '').split(',') x = 0 for key in line_tiles: if key is not None and key is not '': object = TileFactory.generateSprite( key, x, y - index, self.game) if not isinstance(object, Player): self.not_player.add(object) self.game_objects.add(object) x += 1 # special commands index = self.tiles_high + index while index < len(lines): if lines[index] is "\n": index += 1 continue command = lines[index].split(' ') if command[0] == 'link': start_coords = command[1].split(',') end_map = command[2] end_coords = command[3].replace('\n', '').split(',') self.events[int(start_coords[1]) * self.tiles_wide + int(start_coords[0])] = LinkEvent( start_coords, end_coords, end_map, self.game) elif command[0] == 'dialog': start_coords = command[1].split(',') text = " ".join(command[2:]) self.events[int(start_coords[1]) * self.tiles_wide + int(start_coords[0])] = DialogEvent( text, self.game) elif command[0] == 'cutscene': slides = [] for image in [ os.path.join("cutscenes", image).replace('\n', '') for image in command[3:] ]: if (self.loaded_cutscenes.has_key(image)): slides.append(self.loaded_cutscenes.get(image)) else: loaded_image = image_util.load_image(image) slides.append(loaded_image) self.loaded_cutscenes[image] = loaded_image cutscene = Cutscene(self.game, command[2], slides, self.press_enter) self.played_cutscenes[cutscene.name] = False if command[1] == "start": self.start_cutscenes.append(cutscene) else: coords = command[1].split(',') self.events[int(coords[1]) * self.tiles_wide + int(coords[0])] = cutscene elif command[0] == 'koaladoor': coords = command[1].split(',') self.events[int(coords[1]) * self.tiles_wide + int(coords[0])] = KoalaDoorEvent( int(coords[0]), int(coords[1]), self.game) elif command[0] == 'dingodoor': coords = command[1].split(',') self.events[int(coords[1]) * self.tiles_wide + int(coords[0])] = DingoDoorEvent( int(coords[0]), int(coords[1]), self.game) elif command[0] == 'snakedoor': start_coords = command[1].split(',') end_map = command[2] end_coords = command[3].replace('\n', '').split(',') self.events[int(start_coords[1]) * self.tiles_wide + int(start_coords[0])] = SnakeLinkEvent( start_coords, end_coords, end_map, self.game) elif command[0] == 'pushbacklink': start_coords = command[1].split(',') end_map = command[2] end_coords = command[3].split(',') self.events[int(start_coords[1]) * self.tiles_wide + int(start_coords[0])] = PushbackLinkEvent( start_coords, end_coords, end_map, self.game) elif command[0] == 'visitors\n': self.shouldCreateVisitors = True elif command[0] == 'zookeepers\n': self.shouldCreateZookeepers = True elif command[0] == 'animals\n': self.shouldCreateAnimals = True elif command[0] == 'gameover': start_coords = command[1].split(',') self.events[int(start_coords[1]) * self.tiles_wide + int(start_coords[0])] = GameOverEvent(self.game) index += 1
def load_map(self, file): lines = file.readlines() # Load the tile map for idx in xrange(len(lines)): line = lines[idx] if line == " \n": self.tiles_high = idx self.height = self.tiles_high * self.tile_size for y in xrange(0, self.tiles_high): line = lines[y] parsed_comments = line.split('#'); line_tiles = parsed_comments[0].replace(' ','').replace('\n','').split(',') self.tiles_wide = len(line_tiles) self.width = self.tiles_wide * self.tile_size self.num_tiles = self.tiles_wide * self.tiles_high x = 0 for key in line_tiles: tile = TileFactory.generateTile(key,x,y,self.game) self.tiles.append(tile) if not tile.walkable: self.unwalkable_tiles.add(tile) if not tile.visible: self.unjumpable_tiles.add(tile) if not tile.swimmable and not tile.walkable: self.unswimmable_and_unwalkable_tiles.add(tile) x += 1 index = self.tiles_high while lines[index] is "\n": index += 1 index += 1 # Load the sprite map for y in xrange(index, self.tiles_high + index): line = lines[y] parsed_comments = line.split('#'); line_tiles = parsed_comments[0].replace(' ','').replace('\n','').split(',') x = 0 for key in line_tiles: if key is not None and key is not '': object = TileFactory.generateSprite(key,x,y - index,self.game) if not isinstance(object, Player): self.not_player.add( object ) self.game_objects.add( object ) x += 1 # special commands index = self.tiles_high + index while index < len(lines): if lines[index] is "\n": index += 1 continue command = lines[index].split(' ') if command[0] == 'link': start_coords = command[1].split(',') end_map = command[2] end_coords = command[3].replace('\n','').split(',') self.events[int(start_coords[1])*self.tiles_wide+int(start_coords[0])] = LinkEvent(start_coords, end_coords, end_map, self.game) elif command[0] == 'dialog': start_coords = command[1].split(',') text = " ".join(command[2:]) self.events[int(start_coords[1])*self.tiles_wide+int(start_coords[0])] = DialogEvent(text, self.game) elif command[0] == 'cutscene': slides = [] for image in [os.path.join("cutscenes",image).replace('\n','') for image in command[3:]]: if(self.loaded_cutscenes.has_key(image)): slides.append(self.loaded_cutscenes.get(image)) else: loaded_image = image_util.load_image(image) slides.append(loaded_image) self.loaded_cutscenes[image] = loaded_image cutscene = Cutscene(self.game, command[2], slides, self.press_enter) self.played_cutscenes[cutscene.name] = False if command[1] == "start": self.start_cutscenes.append(cutscene) else: coords = command[1].split(',') self.events[int(coords[1])*self.tiles_wide+int(coords[0])] = cutscene elif command[0] == 'koaladoor': coords = command[1].split(',') self.events[int(coords[1])*self.tiles_wide+int(coords[0])] = KoalaDoorEvent(int(coords[0]), int(coords[1]), self.game) elif command[0] == 'dingodoor': coords = command[1].split(',') self.events[int(coords[1])*self.tiles_wide+int(coords[0])] = DingoDoorEvent(int(coords[0]), int(coords[1]), self.game) elif command[0] == 'snakedoor': start_coords = command[1].split(',') end_map = command[2] end_coords = command[3].replace('\n','').split(',') self.events[int(start_coords[1])*self.tiles_wide+int(start_coords[0])] = SnakeLinkEvent(start_coords, end_coords, end_map, self.game) elif command[0] == 'pushbacklink': start_coords = command[1].split(',') end_map = command[2] end_coords = command[3].split(',') self.events[int(start_coords[1])*self.tiles_wide+int(start_coords[0])] = PushbackLinkEvent(start_coords, end_coords, end_map, self.game) elif command[0] == 'visitors\n': self.shouldCreateVisitors = True elif command[0] == 'zookeepers\n': self.shouldCreateZookeepers = True elif command[0] == 'animals\n': self.shouldCreateAnimals = True elif command[0] == 'gameover': start_coords = command[1].split(',') self.events[int(start_coords[1])*self.tiles_wide+int(start_coords[0])] = GameOverEvent(self.game) index += 1