def loadLevel(levelnum): filename = "level" + str(levelnum) + ".xml" if os.path.isfile(filename): dom = xml.dom.minidom.parse(filename) monsterelems = dom.getElementsByTagName("monster") instantiated = [] count = 0 for me in monsterelems: thetype = me.attributes["type"].value x = int(me.attributes["x"].value) y = int(me.attributes["y"].value) e = classfactory.produce(thetype) assert(e) e.setPosition(x,y) instantiated.append(e) count = count + 1 print("instanting no: " + str(count)) return instantiated else: print("Could not find file " + filename) return False
def parseLevel(levelnum): filename = "level" + str(levelnum) + ".xml" if os.path.isfile(filename): dom = xml.dom.minidom.parse(filename) print("Testing XML parsing") rows = dom.getElementsByTagName("row") mlist = [] numrows = 0 for row in rows: mlist.append(handleRow(row)) numrows = numrows + 1 print(mlist) instantiated = [] for row in mlist: thisrow = [] for m in row: e = classfactory.produce(m) thisrow.append(e) instantiated.append(thisrow) return instantiated else: print("NO SUCH FILE!!") exit()
def loadLevel(levelnum): filename = "level" + str(levelnum) + ".xml" if os.path.isfile(filename): dom = xml.dom.minidom.parse(filename) levelinfo = dom.getElementsByTagName("level")[0] w = int(levelinfo.attributes["w"].value) h = int(levelinfo.attributes["h"].value) thelevel = dict() thelevel["dimensions"] = pygame.rect.Rect(0,0,w,h) thelevel["gameobjects"] = [] thelevel["drawables"] = pygame.sprite.LayeredUpdates() layers = dom.getElementsByTagName("layer") count = 0 for layer in layers: count = count + 1 print("instanting layer no: " + str(count)) z = layer.attributes["z"].value print("I found a layer with z-index " + z) instances = layer.getElementsByTagName("instance") for instance in instances: name = instance.attributes["name"].value x = float(instance.attributes["x"].value) y = float(instance.attributes["y"].value) i = classfactory.produce(name) print("produced + " + name) i.setPosition(x,y) i._layer = int(z) print("added an obj to layer " + str(i._layer)) thelevel["gameobjects"].append(i) thelevel["drawables"].add(i) hastag = False if hasattr(i, 'tag'): tag = i.tag hastag = True if instance.attributes.has_key("tag"): tag = instance.attributes["tag"].value hastag = True if hastag: if not thelevel.has_key(tag): thelevel[tag] = [] thelevel[tag].append(i) #sys.exit() return thelevel else: print("Could not find file " + filename) return False