# HG changeset patch # User prock@33b003aa-7bff-0310-803a-e67f0ece8222 # Date 1271187811 0 # Node ID be035dff788aa31a85acc243a1dbd6dc03113885 # Parent ac0f62a07a3e76b5f48a5b8c6996faeec306d438 Added high score window. Added the ability to save high scores. diff -r ac0f62a07a3e -r be035dff788a demos/shooter/gui/highscores.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/demos/shooter/gui/highscores.xml Tue Apr 13 19:43:31 2010 +0000 @@ -0,0 +1,91 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff -r ac0f62a07a3e -r be035dff788a demos/shooter/maps/shooter_map1.xml --- a/demos/shooter/maps/shooter_map1.xml Mon Apr 12 20:41:24 2010 +0000 +++ b/demos/shooter/maps/shooter_map1.xml Tue Apr 13 19:43:31 2010 +0000 @@ -437,7 +437,7 @@ - + diff -r ac0f62a07a3e -r be035dff788a demos/shooter/run.py --- a/demos/shooter/run.py Mon Apr 12 20:41:24 2010 +0000 +++ b/demos/shooter/run.py Tue Apr 13 19:43:31 2010 +0000 @@ -73,7 +73,10 @@ self.listener = ApplicationListener(self.engine, self.world) def requestQuit(self): - self.breakRequested = True + cmd = fife.Command() + cmd.setSource(None) + cmd.setCommandType(fife.CMD_QUIT_GAME) + self.engine.getEventManager().dispatchCommand(cmd) def loadSettings(self): """ @@ -84,7 +87,7 @@ engineSetting = self.engine.getSettings() engineSetting.setDefaultFontGlyphs(" abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,!?-+/():;%&`'*#=[]\"") engineSetting.setDefaultFontPath("fonts/FreeSans.ttf") - engineSetting.setDefaultFontSize(12) + engineSetting.setDefaultFontSize(16) engineSetting.setBitsPerPixel(16) engineSetting.setScreenWidth(1024) engineSetting.setScreenHeight(768) diff -r ac0f62a07a3e -r be035dff788a demos/shooter/scripts/gui/guis.py --- a/demos/shooter/scripts/gui/guis.py Mon Apr 12 20:41:24 2010 +0000 +++ b/demos/shooter/scripts/gui/guis.py Tue Apr 13 19:43:31 2010 +0000 @@ -25,6 +25,9 @@ from fife.extensions import pychan from fife.extensions.pychan import widgets +from xml.sax.saxutils import XMLGenerator +from xml.sax.xmlreader import AttributesNSImpl + class MainMenu(object): def __init__(self, world): self._world = world @@ -114,4 +117,225 @@ self._widget.show() def hide(self): + self._widget.hide() + +class HighScore(object): + def __init__(self, name, score): + self._name = name + self._score = int(score) + +class HighScores(object): + def __init__(self, world): + self._world = world + self.load() + + def load(self): + self._widget = pychan.loadXML('gui/highscores.xml') + self._scores = list() + + for i in range(1,11): + place = self._widget.findChild(name=str(i)) + name = self._widget.findChild(name="%iname" % i) + score = self._widget.findChild(name="%iscore" % i) + highscore = HighScore(name.text, int(score.text)) + self._scores.append(highscore) + + eventMap = { + 'close': self.hide, + } + + self._widget.mapEvents(eventMap) + + def isHighScore(self, score): + for highscore in self._scores: + if score > highscore._score: + return True + + return False + + def addHighScore(self, score): + if not self.isHighScore(score._score): + return + + i = 0 + element = 0 + for highscore in self._scores: + if score._score > highscore._score: + element = i + break + i += 1 + + self._scores.insert(element, score) + #remove last element + self._scores.pop() + + self.saveHighScores() + self.load() + + def startElement(self, name, attrs): + self._file.write(self._indent_level) + self._xmlout.startElementNS((None, name), name, attrs) + self._file.write('\n') + self._indent_level = self._indent_level + '\t' + + def endElement(self, name): + self._indent_level = self._indent_level[0:(len(self._indent_level) - 1)] + self._file.write(self._indent_level) + self._xmlout.endElementNS((None, name), name) + self._file.write('\n') + + def saveHighScores(self): + self._file = open('gui/highscores.xml', 'w') + self._xmlout = XMLGenerator(self._file, 'ascii') + self._xmlout.startDocument() + + self._indent_level = '' + + # + + attr_values = { + (None, 'name'): 'HighScores', + (None, 'position'): '0,0', + (None, 'size'): '1024,768', + (None, 'border_size'): '0', + (None, 'base_color'): '0,0,0', + (None, 'opaque'): '0', + } + attr_names = { + (None, 'name'): 'name', + (None, 'position'): 'position', + (None, 'size'): 'size', + (None, 'border_size'): 'border_size', + (None, 'base_color'): 'base_color', + (None, 'opaque'): 'opaque', + } + + container_attrs = AttributesNSImpl(attr_values, attr_names) + self.startElement('Container', container_attrs) + + # + + attr_values = { + (None, 'opaque'): '1', + (None, 'base_color'): '188,0,0', + (None, 'position'): '412,200', + (None, 'name'): 'high_score', + } + + attr_names = { + (None, 'opaque'): 'opaque', + (None, 'base_color'): 'base_color', + (None, 'position'): 'position', + (None, 'name'): 'name', + } + + vbox_attrs = AttributesNSImpl(attr_values, attr_names) + self.startElement('VBox', vbox_attrs) + + #