changeset 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 ac0f62a07a3e
children 4e58dab2fcdc
files demos/shooter/gui/highscores.xml demos/shooter/maps/shooter_map1.xml demos/shooter/run.py demos/shooter/scripts/gui/guis.py demos/shooter/scripts/world.py
diffstat 5 files changed, 331 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- /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 @@
+<?xml version="1.0" encoding="ascii"?>
+<Container base_color="0,0,0" border_size="0" opaque="0" position="0,0" name="HighScores" size="1024,768">
+	<VBox name="high_score" position="412,200" opaque="1" base_color="188,0,0">
+		<HBox>
+			<Label name="high_scores" border_size="0" text="High Scores" min_size="100,0">
+			</Label>
+		</HBox>
+		<HBox>
+			<Label name="1" border_size="5" text="1" min_size="20,0">
+			</Label>
+			<Label name="1name" border_size="5" text="player" min_size="50,0">
+			</Label>
+			<Label name="1score" border_size="5" text="1200" min_size="150,0">
+			</Label>
+		</HBox>
+		<HBox>
+			<Label name="2" border_size="5" text="2" min_size="20,0">
+			</Label>
+			<Label name="2name" border_size="5" text="aaa" min_size="50,0">
+			</Label>
+			<Label name="2score" border_size="5" text="1000" min_size="150,0">
+			</Label>
+		</HBox>
+		<HBox>
+			<Label name="3" border_size="5" text="3" min_size="20,0">
+			</Label>
+			<Label name="3name" border_size="5" text="bbb" min_size="50,0">
+			</Label>
+			<Label name="3score" border_size="5" text="1000" min_size="150,0">
+			</Label>
+		</HBox>
+		<HBox>
+			<Label name="4" border_size="5" text="4" min_size="20,0">
+			</Label>
+			<Label name="4name" border_size="5" text="ccc" min_size="50,0">
+			</Label>
+			<Label name="4score" border_size="5" text="1000" min_size="150,0">
+			</Label>
+		</HBox>
+		<HBox>
+			<Label name="5" border_size="5" text="5" min_size="20,0">
+			</Label>
+			<Label name="5name" border_size="5" text="ddd" min_size="50,0">
+			</Label>
+			<Label name="5score" border_size="5" text="1000" min_size="150,0">
+			</Label>
+		</HBox>
+		<HBox>
+			<Label name="6" border_size="5" text="6" min_size="20,0">
+			</Label>
+			<Label name="6name" border_size="5" text="eee" min_size="50,0">
+			</Label>
+			<Label name="6score" border_size="5" text="1000" min_size="150,0">
+			</Label>
+		</HBox>
+		<HBox>
+			<Label name="7" border_size="5" text="7" min_size="20,0">
+			</Label>
+			<Label name="7name" border_size="5" text="fff" min_size="50,0">
+			</Label>
+			<Label name="7score" border_size="5" text="1000" min_size="150,0">
+			</Label>
+		</HBox>
+		<HBox>
+			<Label name="8" border_size="5" text="8" min_size="20,0">
+			</Label>
+			<Label name="8name" border_size="5" text="ggg" min_size="50,0">
+			</Label>
+			<Label name="8score" border_size="5" text="1000" min_size="150,0">
+			</Label>
+		</HBox>
+		<HBox>
+			<Label name="9" border_size="5" text="9" min_size="20,0">
+			</Label>
+			<Label name="9name" border_size="5" text="hhh" min_size="50,0">
+			</Label>
+			<Label name="9score" border_size="5" text="1000" min_size="150,0">
+			</Label>
+		</HBox>
+		<HBox>
+			<Label name="10" border_size="5" text="10" min_size="20,0">
+			</Label>
+			<Label name="10name" border_size="5" text="iii" min_size="50,0">
+			</Label>
+			<Label name="10score" border_size="5" text="1000" min_size="150,0">
+			</Label>
+		</HBox>
+		<Button name="close" border_size="0" text="Close" min_size="100,0">
+		</Button>
+	</VBox>
+</Container>
--- 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 @@
 	</layer>
 	<layer y_scale="0.25" y_offset="0.0" pathing="cell_edges_and_diagonals" grid_type="square" id="objects" rotation="0.0" x_scale="0.25" x_offset="0.0" transparency="0">
 		<instances>
-			<i o="ship1" z="0.0" x="7.0" r="0" y="0.0" id="player"></i>
+			<i o="ship1" z="1.0" x="7.0" r="0" y="0.0" id="player"></i>
 			<i r="0" id="dodge1" x="20.0" o="saucer1" z="0.0" y="-2.0"></i>
 			<i r="0" id="dodge1" x="20.0" o="saucer1" z="0.0" y="1.0"></i>
 			<i r="0" id="dodge1" x="20.0" o="saucer1" z="0.0" y="4.0"></i>
--- 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.,!?-+/():;%&amp;`'*#=[]\"")
 		engineSetting.setDefaultFontPath("fonts/FreeSans.ttf")
-		engineSetting.setDefaultFontSize(12)
+		engineSetting.setDefaultFontSize(16)
 		engineSetting.setBitsPerPixel(16)
 		engineSetting.setScreenWidth(1024)
 		engineSetting.setScreenHeight(768)
--- 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
--- 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")