def __init__(self, g, anim_file): self.g = g #store given parameters images = {} #dictionary of images loaded self.part_images = {} #dictionary of part images defined self.parts = {} #dictionary of different parts defined self.layout = None #the part layout texnum = 0 #load data from given file anim_dom = data.load_xml(anim_file).documentElement #load all given images for image in anim_dom.getElementsByTagName("image"): f = image.getAttribute("from") #get source file so we can load it id = image.getAttribute("id") #and image id surf = data.load_image(f) #load given image surf.convert_alpha() #convert it for faster rendering images[id] = surf #store it #define all the part images for part_image in anim_dom.getElementsByTagName("part_image"): image = images[part_image.getAttribute("from")] #get image used #get source coordinate coord = [ int(x.strip()) for x in part_image.getAttribute("coord").split(",") ] surf = pygame.Surface((coord[2], coord[3]), SRCALPHA) surf.blit(image, (0, 0), coord) surfdata = pygame.image.tostring(surf, "RGBA", True) glBindTexture(GL_TEXTURE_2D, texnum) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, coord[2], coord[3], 0, GL_RGBA, GL_UNSIGNED_BYTE, surfdata) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) if part_image.getAttribute( "origin") != "": #if an origin was defined #get origin coord origin = [ int(x.strip()) for x in part_image.getAttribute("origin").split(",") ] else: origin = [0, 0] center = (surf.get_width() / 2, surf.get_height() / 2 ) #calculate new center self.part_images[part_image.getAttribute("id")] = ( texnum, center, origin, (coord[2], coord[3])) #store created image texnum += 1 self.layout = PartAnimationGroup( self, g, anim_dom.getElementsByTagName("layout")[0]) #create layout self.curr_animation = None #clear list of animations self.animations = {} for anim in anim_dom.getElementsByTagName( "anim"): #load list of animations #initialize and store an animation self.animations[anim.getAttribute("id")] = PartAnimation( self, anim)
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 load_data(): #load all pokemon data global pokemon_data, nature_data pokemon_data = {} #ensure data list is cleared pokemen = data.load_xml("pokemon_data.xml").documentElement # get list of pokemon for pokemon in pokemen.getElementsByTagName("pokemon"): #loop through all pokemon name = pokemon.getAttribute("name") #get name of pokemon poke_data = data.load_xml(data.get_node_text(pokemon)).documentElement #load data file pokemon_data[name] = PokemonData(poke_data) #load and parse data nature_data = {} #clear out nature data natures = data.load_xml("nature.xml").documentElement #get nature data for nature in natures.getElementsByTagName("nature"): #loop through nature data t = Container() t.name = nature.getAttribute("name") #set name of nature num = int(nature.getAttribute("num")) #get number of nature t.help = int(nature.getAttribute("help")) #get which stat this nature helps t.hinder = int(nature.getAttribute("hinder")) #get which stat this nature hinders t.num = num nature_data[num] = t #store nature data
def load_data(): #load the type data global poke_types types = {} #ensure type data is cleared dom = data.load_xml("types.xml", peltdir=False).documentElement #load type data for type in dom.getElementsByTagName("type"): #loop through different types type_data = {} #dictionary to hold data for this type type_data["name"] = type.getAttribute("name") #store name of type type_data["modifiers"] = {} #dictionary to hold modifier data for modifier in type.getElementsByTagName("modifier"): #loop through different modifiers #store modifier data type_data["modifiers"][modifier.getAttribute("defender")] = \ Fraction(modifier.getAttribute("amount")) poke_types[type_data["name"]] = PokemonType(type_data) #store a type class for this type
def __init__(self, g, anim_file): self.g = g #store given parameters images = {} #dictionary of images loaded self.part_images = {} #dictionary of part images defined self.parts = {} #dictionary of different parts defined self.layout = None #the part layout #load data from given file anim_dom = data.load_xml(anim_file).documentElement #load all given images for image in anim_dom.getElementsByTagName("image"): f = image.getAttribute("from") #get source file so we can load it id = image.getAttribute("id") #and image id surf = data.load_image(f) #load given image surf.convert_alpha() #convert it for faster rendering images[id] = surf #store it #define all the part images for part_image in anim_dom.getElementsByTagName("part_image"): image = images[part_image.getAttribute("from")] #get image used #get source coordinate coord = [int(x.strip()) for x in part_image.getAttribute("coord").split(",")] if part_image.getAttribute("center") == "": #if no center was defined surf = pygame.Surface((coord[2], coord[3]), SRCALPHA) #create a surface for the image surf.blit(image, (0, 0), coord) #draw image onto new surface normally pos = [0, 0] else: #if one was defined, we need to shift the image around #get center coordinate center = [int(x.strip()) for x in part_image.getAttribute("center").split(",")] #calculate the difference in current center and wanted center center_diff = ((coord[2]/2)-center[0], (coord[3]/2)-center[1]) #calculate new size of surface size = (coord[2]+abs(center_diff[0]), coord[3]+abs(center_diff[1])) #calculate blitting position pos = [max(0, center_diff[0]), max(0, center_diff[1])] surf = pygame.Surface(size, SRCALPHA) #create new surface surf.blit(image, pos, coord) #blit proper section of image if part_image.getAttribute("origin") != "": #if an origin was defined #get origin coord origin = [int(x.strip()) for x in part_image.getAttribute("origin").split(",")] pos[0] += origin[0] #combine origin into offset pos[1] += origin[1] center = (surf.get_width()/2, surf.get_height()/2) #calculate new center self.part_images[part_image.getAttribute("id")] = (surf, center, pos) #store created image self.layout = PartAnimationGroup(self, g, anim_dom.getElementsByTagName("layout")[0]) #create layout self.curr_animation = None #clear list of animations self.animations = {} for anim in anim_dom.getElementsByTagName("anim"): #load list of animations #initialize and store an animation self.animations[anim.getAttribute("id")] = PartAnimation(self, anim)
def load_data(): #load all pokemon data global pokemon_data, nature_data pokemon_data = {} #ensure data list is cleared pokemen = data.load_xml( "pokemon_data.xml").documentElement # get list of pokemon for pokemon in pokemen.getElementsByTagName( "pokemon"): #loop through all pokemon name = pokemon.getAttribute("name") #get name of pokemon poke_data = data.load_xml( data.get_node_text(pokemon)).documentElement #load data file pokemon_data[name] = PokemonData(poke_data) #load and parse data nature_data = {} #clear out nature data natures = data.load_xml("nature.xml").documentElement #get nature data for nature in natures.getElementsByTagName( "nature"): #loop through nature data t = Container() t.name = nature.getAttribute("name") #set name of nature num = int(nature.getAttribute("num")) #get number of nature t.help = int( nature.getAttribute("help")) #get which stat this nature helps t.hinder = int( nature.getAttribute("hinder")) #get which stat this nature hinders t.num = num nature_data[num] = t #store nature data
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 load_data(): #load the type data global poke_types poke_types = {} #ensure type data is cleared dom = data.load_xml("types.xml").documentElement #load type data for type in dom.getElementsByTagName( "type"): #loop through different types type_data = {} #dictionary to hold data for this type type_data["name"] = type.getAttribute("name") #store name of type type_data["modifiers"] = {} #dictionary to hold modifier data for modifier in type.getElementsByTagName( "modifier"): #loop through different modifiers #store modifier data type_data["modifiers"][modifier.getAttribute("defender")] = \ Fraction(modifier.getAttribute("amount")) poke_types[type_data["name"]] = PokemonType( type_data) #store a type class for this type
def __init__(self, g, anim_file, peltdir=True): self.g = g images = {} self.part_images = {} self.parts = {} self.layout = None anim_dom = data.load_xml(anim_file, peltdir).documentElement for image in anim_dom.getElementsByTagName('image'): f = image.getAttribute('from') id = image.getAttribute('id') surf = data.load_image(f, peltdir) surf.convert_alpha() images[id] = surf for part_image in anim_dom.getElementsByTagName('part_image'): image = images[part_image.getAttribute('from')] coord = [int(x.strip()) for x in part_image.getAttribute('coord').split(',')] if part_image.getAttribute('center') == "": surf = pygame.Surface((coord[2], coord[3]), pygame.SRCALPHA) surf.blit(image, (0, 0), coord) pos = [0, 0] else: center = [int(x.strip()) for x in part_image.getAttribute('center').split(',')] center_diff = ((coord[2] / 2) - center[0], (coord[3] / 2) - center[1]) size = (coord[2] + abs(center_diff[0]), coord[3] + abs(center_diff[1])) pos = [max(0, center_diff[0]), max(0, center_diff[1])] surf = pygame.Surface(size, SRCALPHA) surf.blit(image, pos, coord) if part_image.getAttribute('origin') != "": origin = [int(x.strip()) for x in part_image.getAttribute('origin').split(',')] pos[0] += origin[0] pos[1] += origin[1] center = (surf.get_width() / 2, surf.get_height() / 2) self.part_images[part_image.getAttribute('id')] = (surf, center, pos) self.layout = PartAnimationGroup(self, g, anim_dom.getElementsByTagName('layout')[0]) self.curr_animation = None self.animations = {} for anim in anim_dom.getElementsByTagName('anim'): self.animations[anim.getAttribute('id')] = PartAnimation(self, anim) for anim in anim_dom.getElementsByTagName('anim'): self.animations[anim.getAttribute('id')] = PartAnimation(self, anim)
def load_data(): #load tileset animation data global tileset_anims tileset_anims = {} dom = data.load_xml("tilesets/tileset_anim.xml").documentElement #load dom data for tileset in dom.getElementsByTagName("tileset"): #loop through tilesets anims = {} for cycle in tileset.getElementsByTagName("cycle"): anim_list = [] node = cycle.firstChild #loop through nodes of the cycle to get tiles while node != None: if node.localName == "tile": #get position pos = tuple(int(x.strip()) for x in node.getAttribute("pos").split(",")) anim_list.append([pos, int(node.getAttribute("time"))]) node = node.nextSibling #go to next sibling anims[anim_list[0][0]] = anim_list #set animation tileset_anims[tileset.getAttribute("file")] = anims #set animations
def __init__(self, g, anim_file): self.g = g #store given parameters images = {} #dictionary of images loaded self.part_images = {} #dictionary of part images defined self.parts = {} #dictionary of different parts defined self.layout = None #the part layout texnum = 0 #load data from given file anim_dom = data.load_xml(anim_file).documentElement #load all given images for image in anim_dom.getElementsByTagName("image"): f = image.getAttribute("from") #get source file so we can load it id = image.getAttribute("id") #and image id surf = data.load_image(f) #load given image surf.convert_alpha() #convert it for faster rendering images[id] = surf #store it #define all the part images for part_image in anim_dom.getElementsByTagName("part_image"): image = images[part_image.getAttribute("from")] #get image used #get source coordinate coord = [int(x.strip()) for x in part_image.getAttribute("coord").split(",")] surf = pygame.Surface((coord[2], coord[3]), SRCALPHA) surf.blit(image, (0, 0), coord) surfdata = pygame.image.tostring(surf, "RGBA", True) glBindTexture(GL_TEXTURE_2D, texnum) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, coord[2], coord[3], 0, GL_RGBA, GL_UNSIGNED_BYTE, surfdata) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) if part_image.getAttribute("origin") != "": #if an origin was defined #get origin coord origin = [int(x.strip()) for x in part_image.getAttribute("origin").split(",")] else: origin = [0, 0] center = (surf.get_width()/2, surf.get_height()/2) #calculate new center self.part_images[part_image.getAttribute("id")] = (texnum, center, origin, (coord[2], coord[3])) #store created image texnum += 1 self.layout = PartAnimationGroup(self, g, anim_dom.getElementsByTagName("layout")[0]) #create layout self.curr_animation = None #clear list of animations self.animations = {} for anim in anim_dom.getElementsByTagName("anim"): #load list of animations #initialize and store an animation self.animations[anim.getAttribute("id")] = PartAnimation(self, anim)
def __init__(self, file): self.letters = {} # dictionary of letters font_dom = data.load_xml(file).documentElement # load and parse font image_file = font_dom.getAttribute("image") # get font bitmap file name self.image = data.load_image(image_file) # load font bitmap self.image.convert_alpha() # convert it to draw faster self.height = int(font_dom.getAttribute("height")) # get height of characters self.id = font_dom.getAttribute("id") # get font ID # load all the letters for letter in font_dom.getElementsByTagName("letter"): name = letter.getAttribute("name") # get name of letter width = int(letter.getAttribute("size")) # get width of letter coords = letter.getAttribute("coord").split(",") # get coordinate of letter # make a rect of the letter bitmap rect = pygame.Rect(int(coords[0].strip()), int(coords[1].strip()), width, self.height) self.letters[name] = rect # store the letter
def from_xml(path): task_dir = os.path.dirname(path) self = Task() data = load_xml('ojoj.task', path, handlers={'default': _attrs_to_dict, 'test': _attrs_to_dict}) self.id = data['id'] self.name = data.get('name', self.id) self.task_dir = data.get('task_dir', task_dir) self.tests = [] default = data.get('default', {}) for test in data.get_list('test'): tmp = {} tmp.update(default) tmp.update(test) _make_test(tmp) self.tests.append(tmp) return self
def load_data(): #load tileset animation data global tileset_anims tileset_anims = {} dom = data.load_xml( "tilesets/tileset_anim.xml").documentElement #load dom data for tileset in dom.getElementsByTagName("tileset"): #loop through tilesets anims = {} for cycle in tileset.getElementsByTagName("cycle"): anim_list = [] node = cycle.firstChild #loop through nodes of the cycle to get tiles while node != None: if node.localName == "tile": #get position pos = tuple( int(x.strip()) for x in node.getAttribute("pos").split(",")) anim_list.append([pos, int(node.getAttribute("time"))]) node = node.nextSibling #go to next sibling anims[anim_list[0][0]] = anim_list #set animation tileset_anims[tileset.getAttribute("file")] = anims #set animations
def load_map(self, map_file): #load a map map_dom = data.load_xml(map_file).documentElement #load map xml data tile_file = map_dom.getAttribute("tiles") #get tile map file self.map = map.Map(self.g, tile_file) #load the map self.map_file = map_file #save map file self.wild_pokemon = {} #clear wild pokemon data too child = map_dom.firstChild #get first object data element while child is not None: #loop through object data if child.localName == "object": #if it's an object element obj_id = child.getAttribute("id") #get its id obj_type = child.getAttribute("type") #and type #initialize new object obj = objects.obj_types[obj_type](self, child) self.objects[obj_id] = obj #store it self.map.add_object(obj) #add it to map elif child.localName == "wild": #if it's wild pokemon data self.parse_wild(child) #handle it child = child.nextSibling #go to next element self.objects["player"] = self.player #store player object self.map.add_object(self.player) #and add it to map if self.debug : print repr(self.objects)
def __init__(self, file): self.letters = {} #dictionary of letters font_dom = data.load_xml(file).documentElement #load and parse font image_file = font_dom.getAttribute("image") #get font bitmap file name self.image = data.load_image(image_file) #load font bitmap self.image.convert_alpha() #convert it to draw faster self.height = int( font_dom.getAttribute("height")) #get height of characters self.id = font_dom.getAttribute("id") #get font ID #load all the letters for letter in font_dom.getElementsByTagName("letter"): name = letter.getAttribute("name") #get name of letter width = int(letter.getAttribute("size")) #get width of letter coords = letter.getAttribute("coord").split( ",") #get coordinate of letter #make a rect of the letter bitmap rect = pygame.Rect(int(coords[0].strip()), int(coords[1].strip()), \ width, self.height) self.letters[name] = rect #store the letter
def __init__(self, g, sprite, anim_file): self.g = g self.sprite = sprite self.afile = anim_file self.animations = {} self.sheets = {} self.curranim = None self.oldanim = None anim_dom = data.load_xml(anim_file).documentElement for sheet in anim_dom.getElementsByTagName('sheet'): width = int(sheet.getAttribute('tilewidth')) height = int(sheet.getAttribute('tileheight')) image = sheet.getAttribute('from') id = sheet.getAttribute('id') self.sheets[id] = tileset.Tileset(image, width, height) for anim in anim_dom.getElementsByTagName('animation'): id = anim.getAttribute('id') self.animations[id] = Animation(self, anim)
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 load_map(self, map_file, spawn=False): #load a map map_dom = data.load_xml(map_file).documentElement #load map xml data tile_file = map_dom.getAttribute("tiles") #get tile map file self.map = map.Map(self.g, tile_file) #load the map self.map_file = map_file #save map file self.wild_pokemon = {} #clear wild pokemon data too child = map_dom.firstChild #get first object data element while child is not None: #loop through object data if child.localName == "object": #if it's an object element obj_id = child.getAttribute("id") #get its id obj_type = child.getAttribute("type") #and type #initialize new object obj = objects.obj_types[obj_type](self, child) self.objects[obj_id] = obj #store it self.map.add_object(obj) #add it to map elif child.localName == "wild": #if it's wild pokemon data self.parse_wild(child) #handle it child = child.nextSibling #go to next element if spawn: try: self.player.warp(self.objects["spawn_point"].tile_pos) except KeyError as e: raise error.NoSpawnPoint("The introduction level to this game has no spawn point defined!\n%s" %e) self.objects["player"] = self.player #store player object self.map.add_object(self.player) #and add it to map if self.debug: print repr(self.objects)
import re, os, time, sys from collections import Counter from api import Api BASEDIR, filename = os.path.split(os.path.abspath(__file__)) TESTER_PATH = os.path.abspath(BASEDIR+'/../tester') sys.path.append(TESTER_PATH) class Paths: user_source = '{tmp_dir}/ojoj-user-source{ext}' import tester, data, events from task import Task config = data.load_xml('ojoj.daemon.config', sys.argv[1]) if config.get('web.password') is None: import getpass config['web.password'] = getpass.getpass() tasks = [] for tasks_directory in config.get_list('tasks'): for directory, _, files in os.walk(tasks_directory): if 'task.xml' in files: tasks.append((Task.from_xml, os.path.join(directory, 'task.xml'))) if 'info' in files: tasks.append((Task.from_info, os.path.join(directory, 'info')))
def __init__(self, g, anim_file): self.g = g #store given parameters images = {} #dictionary of images loaded self.part_images = {} #dictionary of part images defined self.parts = {} #dictionary of different parts defined self.layout = None #the part layout #load data from given file anim_dom = data.load_xml(anim_file).documentElement #load all given images for image in anim_dom.getElementsByTagName("image"): f = image.getAttribute("from") #get source file so we can load it id = image.getAttribute("id") #and image id surf = data.load_image(f) #load given image surf.convert_alpha() #convert it for faster rendering images[id] = surf #store it #define all the part images for part_image in anim_dom.getElementsByTagName("part_image"): image = images[part_image.getAttribute("from")] #get image used #get source coordinate coord = [ int(x.strip()) for x in part_image.getAttribute("coord").split(",") ] if part_image.getAttribute( "center") == "": #if no center was defined surf = pygame.Surface( (coord[2], coord[3]), SRCALPHA) #create a surface for the image surf.blit(image, (0, 0), coord) #draw image onto new surface normally pos = [0, 0] else: #if one was defined, we need to shift the image around #get center coordinate center = [ int(x.strip()) for x in part_image.getAttribute("center").split(",") ] #calculate the difference in current center and wanted center center_diff = ((coord[2] / 2) - center[0], (coord[3] / 2) - center[1]) #calculate new size of surface size = (coord[2] + abs(center_diff[0]), coord[3] + abs(center_diff[1])) #calculate blitting position pos = [max(0, center_diff[0]), max(0, center_diff[1])] surf = pygame.Surface(size, SRCALPHA) #create new surface surf.blit(image, pos, coord) #blit proper section of image if part_image.getAttribute( "origin") != "": #if an origin was defined #get origin coord origin = [ int(x.strip()) for x in part_image.getAttribute("origin").split(",") ] pos[0] += origin[0] #combine origin into offset pos[1] += origin[1] center = (surf.get_width() / 2, surf.get_height() / 2 ) #calculate new center self.part_images[part_image.getAttribute("id")] = ( surf, center, pos) #store created image self.layout = PartAnimationGroup( self, g, anim_dom.getElementsByTagName("layout")[0]) #create layout self.curr_animation = None #clear list of animations self.animations = {} for anim in anim_dom.getElementsByTagName( "anim"): #load list of animations #initialize and store an animation self.animations[anim.getAttribute("id")] = PartAnimation( self, anim)