# 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)
+
+ #
+
+ self.startElement('HBox', AttributesNSImpl({}, {}))
+ attr_values = {
+ (None, 'name'): 'high_scores',
+ (None, 'text'): 'High Scores',
+ (None, 'min_size'): '100,0',
+ (None, 'border_size'): '0',
+ }
+
+ attr_names = {
+ (None, 'name'): 'name',
+ (None, 'text'): 'text',
+ (None, 'min_size'): 'min_size',
+ (None, 'border_size'): 'border_size',
+ }
+ self.startElement('Label', AttributesNSImpl(attr_values, attr_names))
+ self.endElement('Label')
+
+ self.endElement('HBox')
+
+ i = 1
+ for score in self._scores:
+ self.startElement('HBox', AttributesNSImpl({}, {}))
+
+ plc_attr_values = {
+ (None, 'name'): str(i),
+ (None, 'text'): str(i),
+ (None, 'min_size'): '20,0',
+ (None, 'border_size'): '5',
+ }
+
+ plc_attr_names = {
+ (None, 'name'): 'name',
+ (None, 'text'): 'text',
+ (None, 'min_size'): 'min_size',
+ (None, 'border_size'): 'border_size',
+ }
+
+ name_attr_values = {
+ (None, 'name'): str(i) + 'name',
+ (None, 'text'): score._name,
+ (None, 'min_size'): '50,0',
+ (None, 'border_size'): '5',
+ }
+
+ name_attr_names = {
+ (None, 'name'): 'name',
+ (None, 'text'): 'text',
+ (None, 'min_size'): 'min_size',
+ (None, 'border_size'): 'border_size',
+ }
+
+ score_attr_values = {
+ (None, 'name'): str(i) + 'score',
+ (None, 'text'): str(score._score),
+ (None, 'min_size'): '150,0',
+ (None, 'border_size'): '5',
+ }
+
+ score_attr_names = {
+ (None, 'name'): 'name',
+ (None, 'text'): 'text',
+ (None, 'min_size'): 'min_size',
+ (None, 'border_size'): 'border_size',
+ }
+
+ self.startElement('Label', AttributesNSImpl(plc_attr_values, plc_attr_names))
+ self.endElement('Label')
+ self.startElement('Label', AttributesNSImpl(name_attr_values, name_attr_names))
+ self.endElement('Label')
+ self.startElement('Label', AttributesNSImpl(score_attr_values, score_attr_names))
+ self.endElement('Label')
+
+
+ self.endElement('HBox')
+ i += 1
+
+
+ #
+
+ attr_values = {
+ (None, 'name'): 'close',
+ (None, 'text'): 'Close',
+ (None, 'min_size'): '100,0',
+ (None, 'border_size'): '0',
+ }
+
+ attr_names = {
+ (None, 'name'): 'name',
+ (None, 'text'): 'text',
+ (None, 'min_size'): 'min_size',
+ (None, 'border_size'): 'border_size',
+ }
+
+ self.startElement('Button', AttributesNSImpl(attr_values, attr_names))
+ self.endElement('Button')
+
+ self.endElement('VBox')
+ self.endElement('Container')
+
+ def show(self):
+ self._widget.show()
+
+ def hide(self):
+ self.saveHighScores()
self._widget.hide()
\ No newline at end of file
diff -r ac0f62a07a3e -r be035dff788a demos/shooter/scripts/world.py
--- a/demos/shooter/scripts/world.py Mon Apr 12 20:41:24 2010 +0000
+++ b/demos/shooter/scripts/world.py Tue Apr 13 19:43:31 2010 +0000
@@ -74,6 +74,9 @@
self._gameover = GameOverDisplay()
self._gameover.hide()
+ self._highscores = HighScores(self)
+ self._highscores.hide()
+
self._genericrenderer = None
def showMainMenu(self):
@@ -89,7 +92,7 @@
pass
def showHighScores(self):
- pass
+ self._highscores.show()
def quit(self):
self.reset()
@@ -185,6 +188,12 @@
def gameOver(self):
self._gameover.show()
+
+ if self._highscores.isHighScore(self.scene.player.score):
+ self._highscores.addHighScore(HighScore("player", self.scene.player.score))
+ self._highscores.show()
+
+
def newGame(self):
self.loadLevel("maps/shooter_map1.xml")