diff demos/shooter/scripts/gui/guis.py @ 464:be035dff788a

Added high score window. Added the ability to save high scores.
author prock@33b003aa-7bff-0310-803a-e67f0ece8222
date Tue, 13 Apr 2010 19:43:31 +0000
parents 597b066d5ccb
children 4e58dab2fcdc
line wrap: on
line diff
--- 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 = ''
+
+		#<Container name="HighScores" position="0,0" size="1024,768" border_size="0" base_color="0,0,0" opaque="0">
+	
+		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)
+
+		#<VBox opaque='1' base_color="188, 0, 0" position="412,200" name="high_score">
+		
+		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)
+		
+		#<Label name="high_scores" text="High Scores" min_size="100,0" border_size="0"/>
+		
+		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
+
+	
+		#<Button name="close" text="Close" min_size="100,0" border_size="0"/>
+	
+		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