def __init__(self, level, map_tiles_filename, character_tiles_filename, fps=FRAMES_PER_SECOND): game.Game.__init__(self, fps) self.map_tiles = tileset.Tileset(map_tiles_filename, MAP_TILE_SIZE) self.character_tiles = tileset.Tileset(character_tiles_filename, CHARACTER_TILE_SIZE) if level < 1 or level > NUM_LEVELS: level = 1 self.current_level = level mixer.init() self.load_level(self.current_level) mouse.set_visible(False)
def __init__(self, g, map_file): global tileset_anims self.g = g #store globals self.map_file = map_file #and the file we were passed in map_dom = data.load_xml(map_file) #load and parse the map XML map_dom = map_dom.documentElement #get the document element of the map self.map_width = int(map_dom.getAttribute("width")) #load dimensions self.map_height = int(map_dom.getAttribute("height")) self.pix_width = self.map_width * 16 #calculate pixel dimensions self.pix_height = self.map_height * 16 self.properties = {} #dictionary to store map properties self.obj_layer = None #the object layer in the map self.tilesets = [] #list of tilesets in the map self.layers = [] #list of layers in the map child = map_dom.firstChild #get the first child of the map so we can process them while child is not None: #loop through the children if child.localName == "tileset": #if it's a tileset image_tag = child.getElementsByTagName("image")[ 0] #get image associated with it image_path = image_tag.getAttribute( "source") #get path of image image_path = image_path.replace("../", "") #fix it up trans = image_tag.getAttribute("trans") #get transparent color if trans is not "": #if one actually exists trans = (int(trans[:2], 16), int(trans[2:4], 16), int(trans[4:], 16)) #parse it else: #if it doesn't trans = None #set it to None firstgid = int( child.getAttribute("firstgid")) #get id of tileset start t = tileset.Tileset(image_path, 16, 16, trans) #load the tileset try: anim = tileset_anims[ image_path] #attempt to get tileset animations for this image except: anim = {} self.tilesets.append([firstgid, t, anim]) #and save it to the list elif child.localName == "layer": #if it's a layer self.layers.append(MapTileLayer(self.g, self, child)) #process it elif child.localName == "objectgroup": #if it's an object layer self.layers.append(MapObjectLayer(self.g, self, child)) #process it elif child.localName == "properties": #if it's the properties list curr_prop = child.firstChild #get the first property while curr_prop is not None: #loop through properties if curr_prop.localName == "property": #if it's a property self.properties[curr_prop.getAttribute("name")] = \ curr_prop.getAttribute("value") #load a property curr_prop = curr_prop.nextSibling #go to next property child = child.nextSibling #get the next child to process it self.image = pygame.Surface( (self.map_width * 16, self.map_height * 16)) #create a new surface to render on self.image.convert() #convert it to blit faster
def __init__(self, name, map_filename, tiles_filename, tile_size_x, tile_size_y, view_size_x, view_size_y, frames_per_second): self.map_filename = map_filename self.tiles_filename = tiles_filename self.tile_size_x = tile_size_x self.tile_size_y = tile_size_y self.view_size_x = view_size_x self.view_size_y = view_size_y game.Game.__init__(self, name, view_size_x * tile_size_x, view_size_y * tile_size_y, frames_per_second) # initialize the set of tiles self.tiles = tileset.Tileset(self.tiles_filename, self.tile_size_x, self.tile_size_y) # initialize the map self.themap = map.Map(self.map_filename, self.tiles, self.screen, self.view_size_x, self.view_size_y) # initialize the player (size_x, size_y) = self.themap.getSize() start_x = self.tile_size_x start_y = self.tile_size_y * (size_y - 2) self.theguy = guy.Guy(self.tiles, self.themap, self.screen, start_x, start_y)
def __init__(self, node, mMap, position, level): """ Set up the player. node - the <player> node. mMap - the map to start on. position - the position to start at. level - the level to start on. """ #init the sprite sprite.Sprite.__init__(self, node, mMap, position, level) #create status tilesets self.statusTilesets = {} self.statusTilesets[sprite.S_WALK] = self.tileset for statusNode in data.getChildren(node, "status"): try: status = STATUSNAMES[data.getAttr(statusNode, "name", data.D_STRING)] except KeyError: raise data.DInvalidAttributeError(statusNode, "name") tsPath = os.path.join(settings.path, "data", data.getAttr(statusNode, "tileset", data.D_STRING)) ts = tileset.Tileset(tsPath) self.statusTilesets[status] = ts #create an empty party #overridden with unpickled party if restoring from a save self.party = pokemon.Party() #set up scripting self.scriptCommands["addToParty"] = self.command_addToParty
def __init__(self, name, map_filename, tiles_filename, tile_size_x, tile_size_y, view_size_x, view_size_y, frames_per_second): self.map_filename = map_filename self.tiles_filename = tiles_filename self.tile_size_x = tile_size_x self.tile_size_y = tile_size_y self.view_size_x = view_size_x self.view_size_y = view_size_y game.Game.__init__(self, name, view_size_x * tile_size_x, view_size_y * tile_size_y, frames_per_second) self.currentmap = -1 self.changemap = True # initialize the set of tiles self.tiles = tileset.Tileset(self.tiles_filename, self.tile_size_x, self.tile_size_y)
def __init__(self, g, type, dlog=None): self.g = g #store parameters if dlog is not None: #if a dialog has been provided #copy its parameters self.type = dlog.type self.choice_tiles = dlog.choice_tiles self.dlog_font = dlog.dlog_font self.dlog = dialogs[self.type] else: #otherwise, if none was given dlog = dialogs[type] #get type self.choice_tiles = tileset.Tileset(dlog["choice_file"], 8, 8) #load choice tileset self.dlog_font = font.Font(dlog["font"]) #load font self.dlog = dlog self.type = type self.choices = [] #list of choices self.drawing = False #whether we're currently showing choices self.curr_choice = None #index of the currently selected choice self.cursor_tile = pygame.Surface( (8, 8), SRCALPHA) #surface to hold cursor tile
def __init__(self, g, type): global dialogs self.type = type #store parameters self.g = g dlog = dialogs[type] #get attributes of dialog self.dlog = dlog #store it self.image = data.load_image(dlog["file"]) #load image file self.image.convert() #convert it so it will draw faster self.choice_tiles = tileset.Tileset(dlog["choice_file"], 8, 8) #load choice tileset self.dlog_rect = dlog["text_rect"] #get text rect self.dlog_font = font.Font( dlog["font"]) #load font we're going to use for drawing self.waiting = False #whether we're waiting for the player to press action self.text = [] #list of characters to draw self.text_surf = pygame.Surface( self.dlog_rect.size) #create a surface to draw text on self.text_surf.set_colorkey((127, 182, 203)) #set a colorkey for it self.text_surf.fill((127, 182, 203)) #fill it with the colorkey value self.text_surf.convert() #convert it to blit faster self.drawing = False #whether we're currently drawing the dialog box self.choice_dialog = None #store current choice dialog, if any self.fill_allowed = False #whether the user can draw the whole dialog at once
def __init__(self, g, sprite, anim_file): self.g = g #store parameters self.sprite = sprite self.animations = {} #dictionary of the different animations self.sheets = {} #dictionary of the different sheets self.curr_animation = None #pointer to the current animation self.old_animation = None #pointer to the old animation, used for looping #load the animations from the file provided anim_dom = data.load_xml(anim_file).documentElement #load all of the animation sheets for sheet in anim_dom.getElementsByTagName("sheet"): width = int(sheet.getAttribute("tilewidth")) #get size of tiles height = int(sheet.getAttribute("tileheight")) image = sheet.getAttribute("from") #get path to image id = sheet.getAttribute("id") #and image ID self.sheets[id] = tileset.Tileset(image, width, height) #create the tileset #load all of the animations for anim in anim_dom.getElementsByTagName("animation"): id = anim.getAttribute("id") #get id of the animation self.animations[id] = Animation(self, anim) #create the animation
def __init__(self, fn): """ Open the map data file, set border tiles and connections, and add NPCs and other events. fn - the filename of the map XML file. """ #for the scripting engine script_engine.ScriptableObject.__init__(self) #store variables we'll need later self.fn = fn #get a script engine (singleton) self.scriptEngine = script_engine.ScriptEngine() #parse the XML file root = data.getTreeRoot(fn) self.music = os.path.join(settings.path, "data", data.getAttr(root, "music", data.D_STRING)) #open the actual map data file to create the map tile data mapPath = os.path.join(settings.path, "data", data.getAttr(root, "file", data.D_STRING)) self.openMap(mapPath) #create the tileset tilesetPath = os.path.join( settings.path, "data", data.getAttr(root, "tileset", data.D_STRING)) self.tileset = tileset.Tileset(tilesetPath) #set the border tiles self.borderTiles = {} borderNode = data.getChild(root, "border") #set each border node with the correct tile indexes, subtracting 1 because the tileset starts at 1 not 0 self.borderTiles[BD_NW] = data.getAttr(borderNode, "nw", data.D_INT) - 1 self.borderTiles[BD_NE] = data.getAttr(borderNode, "ne", data.D_INT) - 1 self.borderTiles[BD_SW] = data.getAttr(borderNode, "sw", data.D_INT) - 1 self.borderTiles[BD_SE] = data.getAttr(borderNode, "se", data.D_INT) - 1 #create any connections from the map #connected maps will not be loaded until the map becomes the main game map #connections are stored as {direction: (filename, offset)} self.connections = {} self.connectedMaps = {} for c in data.getChildren(root, "connection"): side = data.getAttr(c, "side", data.D_STRING) fp = os.path.join(settings.path, "data", data.getAttr(c, "map", data.D_STRING)) offset = data.getAttr(c, "offset", data.D_INT) if side == "left": self.connections[sprite.DIR_LEFT] = (fp, offset) elif side == "right": self.connections[sprite.DIR_RIGHT] = (fp, offset) elif side == "up": self.connections[sprite.DIR_UP] = (fp, offset) elif side == "down": self.connections[sprite.DIR_DOWN] = (fp, offset) #create any NPCs, adding them to the sprite dictionary self.sprites = {} for n in data.getChildren(root, "npc"): spr = npc.NPC(n, self) self.sprites[spr.id] = spr #create a dictionary to hold positions reserved by moving sprites self.reservedPositions = {} #create script and warp events, adding them to the events dictionary #if a load script is defined, create it self.events = {} loadScript = None for s in data.getChildren(root, "script"): trigger = data.getAttr(s, "trigger", data.D_STRING) if trigger == "load": loadScript = script_engine.Script(s) else: position = tuple(data.getAttr(s, "position", data.D_INT2LIST)) self.events[position] = events.ScriptEvent(s, self) for w in root.findall("warp"): position = tuple(data.getAttr(w, "position", data.D_INT2LIST)) self.events[position] = events.Warp(w, self) #if there is a load script, run it if loadScript is not None: self.scriptEngine.run(loadScript, self)