def make_minimap(self, x, y): background = pygame.sprite.Sprite() background.image = pygame.Surface((42, 34)) background.rect = (x, y, 42, 34) background.image.fill(game.BGCOLOR) self.popup_sprites.append(background) ts = tilemap.Tilemap( 2, 20, 16, pygame.image.load(resource_path('assets/minitiles.png'))) ts.load( resource_path('levels/%s_tiles.csv' % LEVELS[self.selected_level][0])) ts.rect = (x + 1, y + 1, ts.rect[2], ts.rect[3]) self.popup_sprites.append(ts) ts = tilemap.Tilemap(2, 20, 16, pygame.image.load( resource_path('assets/miniobjects.png')), use_zero=True) ts.load( resource_path('levels/%s_objects.csv' % LEVELS[self.selected_level][0])) ts.rect = (x + 1, y + 1, ts.rect[2], ts.rect[3]) self.popup_sprites.append(ts)
def parse(tmx_file): tmx_file = bacon.get_resource_path(tmx_file) base_dir = os.path.dirname(tmx_file) tree = ET.parse(tmx_file) elem = tree.getroot() orientation = elem.get('orientation') cols = int(elem.get('width')) rows = int(elem.get('height')) tile_width = int(elem.get('tilewidth')) tile_height = int(elem.get('tileheight')) tm = tilemap.Tilemap(tile_width, tile_height, cols, rows) tm.tilesets = [] layers = [] object_layers = [] for child in elem: if child.tag == 'tileset': tm.tilesets.append(parse_tileset(child, base_dir)) elif child.tag == 'layer': parse_layer(tm, child, tm.tilesets) elif child.tag == 'objectgroup': parse_object_group(tm, child) return tm
def newGame(self, fn, root): """Start a new game""" #create the new save file self.savegame = save.Save(fn) #get the new game node from the game xml file newgameNode = data.getChild(root, "newgame") #create the map, loading connections since it's the active one self.map = tilemap.Tilemap( os.path.join(settings.path, "data", data.getAttr(newgameNode, "map", data.D_STRING))) self.map.loadConnections() #create the player playerNode = data.getChild(root, "player") self.player = player.Player( playerNode, self.map, data.getAttr(newgameNode, "position", data.D_INT2LIST), data.getAttr(newgameNode, "level", data.D_INT)) #if there's a new game script, run it script = data.getOptionalChild(newgameNode, "script") if script is not None: s = script_engine.Script(script) self.scriptEngine.run(s)
def openSave(self, fn, root): """ Open a save file. fn - the path to the save file. root - the root <game> node. """ #open the file for reading, load the save game, and close try: f = open(fn, "r") self.savegame = pickle.load(f) f.close() except pickle.PickleError: raise error.DittoInvalidResourceException("Ditto main", fn) except IOError: raise error.DittoIOException("Ditto main", fn) #create the current map and load its connections self.map = tilemap.Tilemap(self.savegame.currentMap) self.map.loadConnections() #create the player playerNode = data.getChild(root, "player") self.player = player.Player(playerNode, self.map, self.savegame.currentPosition, self.savegame.currentLevel) self.player.direction = self.savegame.currentDirection self.player.party = self.savegame.party
def load(self): gx, gy = game.RES[0] // game.TILESIZE, game.RES[1] // game.TILESIZE self.tilemap = tilemap.Tilemap( game.TILESIZE, gx, gy, pygame.image.load( resource_path("assets/tiles.png")).convert_alpha()) self.background_group = pygame.sprite.Group() self.background_group.add(self.tilemap) self.game_group = pygame.sprite.LayeredUpdates() self.ui_group = pygame.sprite.Group() self.tutorial_group = pygame.sprite.Group() self.tanks = [] self.overlay = framesprite.FrameSprite("assets/gameoverlay.png", 240) self.ui_group.add(self.overlay) self.worker_queue = tilemap.Tilemap( 12, 20, 1, pygame.image.load( resource_path("assets/workericon.png")).convert_alpha()) self.worker_queue.rect = (2, 1, 240, 12) self.ui_group.add(self.worker_queue) self.animatedsprites = [] self.road_grid = [] self.object_grid = [] self.factories = [] self.cityhalls = [] for row in range(gy): self.object_grid.append([]) self.road_grid.append([]) for col in range(gx): self.object_grid[-1].append(None) self.road_grid[-1].append(False) try: self.tilemap.load("levels/%s_tiles.csv" % self.level_file) self.load_objects("levels/%s_objects.csv" % self.level_file) except Exception as e: traceback.print_exc() self.tilemap.load("levels/empty_tiles.csv") self.load_objects("levels/empty_objects.csv") self.level_file = "empty" self.queued_workers = 0 self.start_tile = None
def __init__(self): #Game control classes self.game_map = tilemap.Tilemap() tilemap.Tilemap.load_overworld(self.game_map) self.display_window = display.DisplayWindow() self.display_map = display.DisplayTilemap(self.display_window, self.game_map) ##Loading Colligo for user manipulation self.main_char = self.game_map.gameObjInstances[0] self.char_moving = [False, False, False, False] #Up Down Left Right
import player # The player needs to move. import menu import story import npc sys.path.insert(0, 'src/objects/' ) # This line tells the importer where to look for the module. import pokemon # Globals. __builtin__.g_game_stopped = False __builtin__.g_current_scene = OUTSIDE # Create & load the different scenes. __builtin__.g_outside_tilemap = tilemap.Tilemap("outside.map", [0, 0], 0) __builtin__.g_lab_tilemap = tilemap.Tilemap( "lab.map", [0, 0], pygame.image.load("resc/images/lab.png").convert()) __builtin__.g_player_house_down_tilemap = tilemap.Tilemap( "player_house_down.map", [0, 0], pygame.image.load("resc/images/player_house_down.png").convert()) __builtin__.g_player_house_up_tilemap = tilemap.Tilemap( "player_house_up.map", [0, 0], pygame.image.load("resc/images/player_house_up.png").convert()) __builtin__.g_route_tilemap = tilemap.Tilemap("route.map", [0, -64 * 64], 0) # Create the player object. __builtin__.g_player = player.Player( [240 * 4 / 2 - 8 * 4, 160 * 4 / 2 - 4 * 4])
def create_map(cls): cls.map = tilemap.Tilemap()
# graphics def load_images(manifest): images = {} for k, v in manifest.items(): images[k] = (pygame.image.load(prefix + v)).convert_alpha() return images images = load_images(manifest) # INITIALIZATION & MAIN LOOP # generate map map = rl.map.make_map(MAP_WIDTH, MAP_HEIGHT, MAX_ROOMS, ROOM_MIN_SIZE, ROOM_MAX_SIZE) tilemap = tilemap.Tilemap(map, TILE_SIZE, images['world'], 127) tile_wall = pygame.Rect(6 * TILE_SIZE, 0 * TILE_SIZE, TILE_SIZE, TILE_SIZE) tile_floor = pygame.Rect(0 * TILE_SIZE, 0 * TILE_SIZE, TILE_SIZE, TILE_SIZE) data = map['data'] for row in data: for tile in row: if tile.block_sight: tile.rect = tile_wall else: tile.rect = tile_floor # generate player creatures_anim_sheet = sprite.AnimSheet(images['creatures'], 24, 24)
def tick(self): if self.waiting: #if waiting if self.waitingFor is not None: #if we're waiting for an object if not self.waitingFor.busy: #if what we're waiting for is no longer busy self.waiting = False #stop waiting self.waitingFor = None #no longer waiting for anything else: #else self.countdown -= 1 #decrease the countdown if self.countdown <= 0: #if we've finished counting down self.waiting = False #stop waiting while (not self.waiting and self.running ): #while we're not waiting and still have commands to process cmd = self.script.commands[ self.currentCommand] #get the current command if cmd[0] == CMD_LOCK: #if it's LOCK print "LOCK" self.game.player.lock() #lock the player if self.caller.__class__.__name__ == "NPC": #if the script has a caller self.caller.lock() #lock it elif cmd[0] == CMD_UNLOCK: #if it's UNLOCK print "UNLOCK" self.game.player.unlock() #unlock the player if self.caller.__class__.__name__ == "NPC": #if the script has a caller self.caller.unlock() #unlock it elif cmd[0] == CMD_FACEPLAYER: #if it's FACEPLAYER print "FACEPLAYER" difference = self.game.player.position[ 0] - self.caller.position[0], self.game.player.position[ 1] - self.caller.position[ 1] #calculate offset between player and caller if difference == (0, -1): #if player is above caller self.caller.direction = sprite.DIR_UP #face UP elif difference == (0, 1): #if player is below caller self.caller.direction = sprite.DIR_DOWN #face DOWN elif difference == (-1, 0): #if player is to left of caller self.caller.direction = sprite.DIR_LEFT #face LEFT elif difference == (1, 0): #if player is to right of caller self.caller.direction = sprite.DIR_RIGHT #face RIGHT elif cmd[0] == CMD_WAIT: #if it's WAIT print "WAIT" self.waiting = True #we're waiting self.waitingFor = None #not waiting for an object self.countdown = cmd[1] #set countdown as needed elif cmd[0] == CMD_MOVEME: #if it's MOVEME print "MOVEME" self.caller.walkUp(cmd[1], True) #walk, force move self.waiting = True #we're waiting self.waitingFor = self.caller #waiting for caller elif cmd[0] == CMD_DIALOG: #if it's DIALOG print "DIALOG" text = map(self.resolveString, cmd[1]) #resolve the text strings for variables d = dialog.Dialog(text, self.font, self.game.screen, cmd[2]) #create the dialog self.game.foregroundObject = d #set it as the game's active object self.waiting = True #we're waiting self.waitingFor = d #waiting for the dialog elif cmd[0] == CMD_SETVAR: print "SETVAR" if cmd[1][0] == "$": self.variables[cmd[1][1:-1]] = cmd[2] elif cmd[1][0] == "*": self.game.savegame.variables[cmd[1][1:-1]] = cmd[2] elif cmd[0] == CMD_GETVAR: print "GETVAR" self.result = self.variables[cmd[1]] elif cmd[0] == CMD_COMPARE: #if it's COMPARE print "COMPARE" self.result = (self.resolveString( cmd[1]) == self.resolveString(cmd[2]) ) #store whether the two are equal elif cmd[0] == CMD_GOTO: #if it's GOTO print "GOTO" self.currentCommand = cmd[ 1] - 1 #go to the line before the one required, which will then be advanced to the correct line elif cmd[0] == CMD_IFFALSEGOTO: #if it's IFFALSEGOTO print "IFFALSEGOTO" if not self.result: #if comparison result is False self.currentCommand = cmd[ 1] - 1 #goto to the line before the one required, which will then be advanced to the correct line elif cmd[0] == CMD_MOVEPLAYER: #if it's MOVEPLAYER print "MOVEPLAYER" self.game.player.walk(cmd[1], True) #move the player self.waiting = True #we're waiting self.waitingFor = self.game.player #we're waiting for the player elif cmd[0] == CMD_CHOICEDIALOG: #if it's CHOICEDIALOG print "CHOICEDIALOG" text = map(self.resolveString, cmd[1]) #resolve the text for variables d = dialog.ChoiceDialog(text, self.font, self.game.screen, self, cmd[2]) #create the choice dialog self.game.foregroundObject = d #set it as the game's active object self.waiting = True #we're waiting self.waitingFor = d #waiting for the choice dialog elif cmd[0] == CMD_SHOWSPRITE: #if it's SHOWSPRITE print "SHOWSPRITE" if self.caller is not None: #if there's a caller if self.caller.__class__.__name__ == "Tilemap": #if it was a map self.caller.getSpriteById( cmd[1]).visible = True #set the sprite visible elif self.caller.__class__.__name__ == "NPC": #if it was an NPC self.caller.map.getSpriteById( cmd[1]).visible = True #set the sprite visible else: #else self.game.player.map.getSpriteById( cmd[1]).visible = True #set the sprite visible elif cmd[0] == CMD_HIDESPRITE: #if it's HIDESPRITE print "HIDESPRITE" if self.caller is not None: #if there's a caller if self.caller.__class__.__name__ == "Tilemap": #if it was a map self.caller.getSpriteById( cmd[1]).visible = False #set the sprite invisible elif self.caller.__class__.__name__ == "NPC": #if it was an NPC self.caller.map.getSpriteById( cmd[1]).visible = False #set the sprite invisible else: #else self.game.player.map.getSpriteById( cmd[1]).visible = False #set the sprite invisible elif cmd[0] == CMD_WARP: print "WARP" if False: p = self.game.player del (p.map.sprites["PLAYER"]) p.map = tilemap.Tilemap(cmd[1], self) p.map.loadConnections() p.position = cmd[2] p.destination = cmd[2] p.level = cmd[3] p.map.sprites["PLAYER"] = p sound.playMusic(p.map.music) else: p = self.game.player mMap = tilemap.Tilemap(cmd[1], self) p.transferTo(mMap, cmd[2]) p.destination = p.getPositionInFront() p.level = cmd[3] elif cmd[0] == CMD_FADEOUTANDIN: print "FADEOUTANDIN" self.game.foregroundObject = foreground_object.FadeOutAndIn( self.game.screen, cmd[1]) elif cmd[0] == CMD_GIVEPOKEMON: print "GIVEPOKEMON" poke = pokemon.Pokemon(cmd[1], cmd[2]) self.game.player.party.add(poke) elif cmd[0] == CMD_SURF: print "SURF" self.game.player.surf() self.currentCommand += 1 #advance to next command if self.currentCommand >= len( self.script.commands ): #if we've reached the end of the script if len(self.queue) == 0: self.running = False #no longer running else: print "Script from queue" s, caller = self.queue.pop() self.running = True #running a script self.script = s #store the script self.caller = caller #store the caller of the script self.currentCommand = 0 #reset command counter to start self.waiting = False #not yet waiting self.waitingFor = None #not waiting for anything break #stop processing