from xml.dom import minidom import sys # This program is a very simple text adventure game # It reads from the file gamefile.xml, parses the xml # and acts like a command line game. # The main class class Game: chapters = [] # All chapter objects are stored here # Constructor def __init__(self): print "Welcome! Please type '-1' at any time to quit the game. Enjoy!" self.parseXml() self.show_chapter() # This function displays the chapter, your options and # takes input from the command line def show_chapter(self, idno = 1): print "" chapter = 0 for chapt in self.chapters: if(chapt.get_idno() == idno): chapter = chapt if(chapter == 0): print "Error: no chapter found!" self.quit() chapter.show() print "" chapter.show_options() print "" try: selection = input('Please enter a number: ') except SyntaxError: selection = 9999 # Out of range if(selection == -1): self.quit() next_chapter = chapter.get_option(selection) while(next_chapter == 0): # The user typed an invalid number try: selection = input("Wrong number selected. Please type another one:") if(selection == -1): self.quit() next_chapter= chapter.get_option(selection) except SyntaxError: selection= 9999 # Out of range self.show_chapter(next_chapter) # Quit the game def quit(self): print "Thank you for playing. Have a nice day!" sys.exit(0) # Parse the game xml file, and initialize the game def parseXml(self): xmldoc = minidom.parse('gamefile.xml') game = xmldoc.documentElement for chapter in game.childNodes: idno = 0 text = "" goto = 0 optiontext = "" if(chapter.nodeName == "chapter"): options = [] # Empty option list for chapterinfo in chapter.childNodes: if(chapterinfo.nodeName == "id"): for info in chapterinfo.childNodes: idno = int(info.nodeValue) if(chapterinfo.nodeName == "text"): for info in chapterinfo.childNodes: text = info.nodeValue if(chapterinfo.nodeName == "option"): for info in chapterinfo.childNodes: if(info.nodeName == "goto"): for optioninfo in info.childNodes: goto = int(optioninfo.nodeValue) if(info.nodeName == "text"): for optioninfo in info.childNodes: optiontext = optioninfo.nodeValue # Create an option object for each parsed option option_obj = Option(goto, optiontext) options.append(option_obj) # Create a chapter object for each parsed chapter chapter_obj = Chapter(idno, text) chapter_obj.set_options(options) self.chapters.insert(idno, chapter_obj) class Chapter: idno = 0 text = "" options = [] # Constructor def __init__(self, idno, text): self.idno = idno self.text = text def set_options(self, options): self.options = options def show(self): print self.text def show_options(self): selection = 0 # The number the user types to choose this option for option in self.options: option.show(selection) selection = selection + 1 # Get the selected option id def get_option(self, selection): if(selection + 1 > len(self.options)): return 0 return self.options[selection].get_goto() def get_idno(self): return self.idno class Option: goto = 0 text = "" # Constructor def __init__(self, goto, text): self.goto = goto self.text = text def show(self, selection): print self.text+": type "+str(selection) def get_goto(self): return self.goto # Start the game! game = Game()