changeset 457:597b066d5ccb

Player now has 3 lives and will receive a game over message once they are used up.
author prock@33b003aa-7bff-0310-803a-e67f0ece8222
date Thu, 08 Apr 2010 21:18:36 +0000
parents 41fd97da94d1
children e77ebf128a74
files demos/shooter/gui/gameover.xml demos/shooter/gui/hud.xml demos/shooter/gui/mainmenu.xml demos/shooter/scripts/gui/guis.py demos/shooter/scripts/scene.py demos/shooter/scripts/ships/player.py demos/shooter/scripts/world.py
diffstat 7 files changed, 104 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/demos/shooter/gui/gameover.xml	Thu Apr 08 21:18:36 2010 +0000
@@ -0,0 +1,7 @@
+<Container name="GameOver" position="0,0" size="1024,768" border_size="0" base_color="0,0,0" opaque="0">
+
+	<VBox opaque='1' base_color="188, 0, 0" position="412,200" name="game_over">
+		<Label text="Game Over!" min_size="200,0" border_size="5"/>
+	</VBox>
+
+</Container>
--- a/demos/shooter/gui/hud.xml	Wed Apr 07 21:26:26 2010 +0000
+++ b/demos/shooter/gui/hud.xml	Thu Apr 08 21:18:36 2010 +0000
@@ -15,4 +15,8 @@
 			<Label text = "SCORE: "/>
 			<Label name="score" text="0                             "/>
 	</HBox>
+	<HBox opaque='0'>
+			<Label text = "Lives: "/>
+			<Label name="lives" text="0                             "/>
+	</HBox>
 </VBox>
\ No newline at end of file
--- a/demos/shooter/gui/mainmenu.xml	Wed Apr 07 21:26:26 2010 +0000
+++ b/demos/shooter/gui/mainmenu.xml	Thu Apr 08 21:18:36 2010 +0000
@@ -1,20 +1,15 @@
-<Container name="Main" position="0,0" size="1024,768" border_size="0" base_color="0,0,0" opaque="0">
+<Container name="MainMenu" position="0,0" size="1024,768" border_size="0" base_color="0,0,0" opaque="0">
 
 <!--
 	<Icon image="gui/backgrounds/800_main_bg.png" position="0,0" size="1024,768" name="main_background_icon"/>
 -->
 
 	<VBox opaque='0' base_color="188, 0, 0" position="412,200" name="main_menu">
-		<HBox>
-			<VBox>
-				<Button name="continue" text="Continue" min_size="200,0" border_size="0"/>
-				<Button name="new_game" text="New Game" min_size="200,0" border_size="0"/>
-				<Button name="high_scores" text="High Scores" min_size="200,0" border_size="0"/>
-				<Button name="credits" text="Credits" min_size="200,0" border_size="0"/>
-				<Button name="quit" text="Quit" min_size="200,0" border_size="0"/>
-			</VBox>
-
-		</HBox>
+		<Button name="continue" text="Continue" min_size="200,0" border_size="0"/>
+		<Button name="new_game" text="New Game" min_size="200,0" border_size="0"/>
+		<Button name="high_scores" text="High Scores" min_size="200,0" border_size="0"/>
+		<Button name="credits" text="Credits" min_size="200,0" border_size="0"/>
+		<Button name="quit" text="Quit" min_size="200,0" border_size="0"/>
 	</VBox>
 
 </Container>
--- a/demos/shooter/scripts/gui/guis.py	Wed Apr 07 21:26:26 2010 +0000
+++ b/demos/shooter/scripts/gui/guis.py	Thu Apr 08 21:18:36 2010 +0000
@@ -82,6 +82,7 @@
 		self._velocitytext = self._widget.findChild(name="velocity")
 		self._positiontext = self._widget.findChild(name="position")
 		self._scoretext = self._widget.findChild(name="score")
+		self._livestext = self._widget.findChild(name="lives")	
 		self._widget.position = (0,0)
 		
 	def show(self):
@@ -100,4 +101,17 @@
 		self._velocitytext.text = text
 		
 	def setScoreText(self, text):
-		self._scoretext.text = text
\ No newline at end of file
+		self._scoretext.text = text
+		
+	def setLivesText(self, text):
+		self._livestext.text = text
+		
+class GameOverDisplay(object):
+	def __init__(self):
+		self._widget = pychan.loadXML('gui/gameover.xml')
+		
+	def show(self):
+		self._widget.show()
+		
+	def hide(self):
+		self._widget.hide()
\ No newline at end of file
--- a/demos/shooter/scripts/scene.py	Wed Apr 07 21:26:26 2010 +0000
+++ b/demos/shooter/scripts/scene.py	Thu Apr 08 21:18:36 2010 +0000
@@ -27,7 +27,6 @@
 from scripts.ships.enemies import *
 from scripts.common.helpers import Rect
 
-
 class SceneNode(object):
 	def __init__(self, spaceobjects = None):
 		if not spaceobjects:
@@ -44,8 +43,9 @@
 	spaceobjects = property(_getObjects, _setObjects)
 
 class Scene(object):
-	def __init__(self, engine, objectLayer):
+	def __init__(self, world, engine, objectLayer):
 		self._engine = engine
+		self._world = world
 		self._model = engine.getModel()
 		self._layer = objectLayer
 		self._nodes = list()
@@ -65,11 +65,14 @@
 		
 		self._paused = False
 		self._timemod = 0
+		
+		self._gameover = False
 
 	def initScene(self, mapobj):
 		self._player = Player(self, 'player')
 		self._player.width = 0.075
 		self._player.height = 0.075
+		self._player.init()
 		self._player.start()
 				
 		enemies = self._layer.getInstances('enemy')
@@ -99,7 +102,7 @@
 			
 		#and finally add the player to the scene
 		self.addObjectToScene(self._player)
-
+		
 	def pause(self, time):
 		self._pausedtime = time
 		self._paused = True
@@ -107,6 +110,37 @@
 	def unpause(self, time):
 		self._timemod += time - self._pausedtime
 		self._paused = False
+		
+	def playerDied(self):
+		self._player.destroy()
+		if self._player.lives <= -1:
+			self._gameover = True
+			self._world.gameOver()
+			return
+		
+		#TODO: Have to find a better way to do this.  If the player
+		#dies too many times right at the start of the level he will
+		#get pushed past the edge of the map to the left.
+		#IDEA: count down to ready player to start again
+		oldpos = self._player.location
+		pos = oldpos.getExactLayerCoordinates()
+		pos.x -= 5
+		oldpos.setExactLayerCoordinates(pos)
+		self._player.location = oldpos
+		self._camera.setLocation(self._player.location)
+		
+		projtodelete = list()
+		for p in self._projectiles:
+			p.destroy()
+			projtodelete.append(p)
+			
+		for p in projtodelete:
+			self._projectiles.remove(p)
+			
+		print len(self._projectiles)
+		
+	def gameOver(self):
+		self._world.gameOver()
 
 	def getObjectsInNode(self, nodeindex):
 		return self._nodes[nodeindex].instances
@@ -181,7 +215,9 @@
 
 		#update objects on the screen
 		for obj in screenlist:
-			obj.update()
+			if not (obj == self._player and self._gameover):
+				obj.update()
+			
 			if obj.changedposition:
 				self.moveObjectInScene(obj)
 
@@ -192,7 +228,8 @@
 				if obj.boundingbox.intersects(self._player.boundingbox):
 					#player touched an enemy.  Destroy player and 
 					#re-initialize scene
-					self._player.destroy()
+					self.playerDied()
+					return
 		
 		
 		#update the list of projectiles
@@ -204,12 +241,18 @@
 				#cant get hit by your own bullet
 				if p.owner != o:
 					if p.boundingbox.intersects(o.boundingbox):
-						self._player.applyScore(100)
-						p.destroy()
-						o.destroy()
-						#TODO:  the destroy functions should spawn an explosion
-						#and also destroy the instance and remove itself from the scene
-						self.removeObjectFromScene(o)
+						if o != self._player:
+							self._player.applyScore(100)
+							p.destroy()
+							o.destroy()
+							#TODO:  the destroy functions should spawn an explosion
+							#and also destroy the instance and remove itself from the scene
+							self.removeObjectFromScene(o)
+						else:
+							#player got hit by a projectile
+							p.destroy()
+							self.playerDied()
+							return
 			
 			#build a list of projectiles to remove (ttl expired)
 			if not p.running:
--- a/demos/shooter/scripts/ships/player.py	Wed Apr 07 21:26:26 2010 +0000
+++ b/demos/shooter/scripts/ships/player.py	Thu Apr 08 21:18:36 2010 +0000
@@ -39,6 +39,9 @@
 		
 		self._lives = 3
 	
+	def init(self):
+		self._lives = 3
+	
 	def _getScore(self):
 		return self._score
 		
@@ -47,9 +50,13 @@
 		
 	def destroy(self):
 		self._lives -= 1
-		print "player has been destroyed!"
+		
+		if self._lives < 0:
+			self._lives = -1
+
 	
 	def update(self):
+	
 		key = False
 
 		oldpos = self.location
--- a/demos/shooter/scripts/world.py	Wed Apr 07 21:26:26 2010 +0000
+++ b/demos/shooter/scripts/world.py	Thu Apr 08 21:18:36 2010 +0000
@@ -70,6 +70,8 @@
 		self._hudwindow = HeadsUpDisplay(self)
 		self._hudwindow.hide()
 
+		self._gameover = GameOverDisplay()
+		self._gameover.hide()		
 	
 	def showMainMenu(self):
 		if self.scene:
@@ -107,15 +109,18 @@
 		self.reset()
 		self.map = loadMapFile(self.filename, self.engine)
 
-		self.scene = Scene(self.engine, self.map.getLayer('objects'))
+		self.scene = Scene(self, self.engine, self.map.getLayer('objects'))
 		self.scene.initScene(self.map)
 
 		self.initCameras()
 
 		self._hudwindow.show()
+		self._gameover.hide()
 		
 		self._starttime = self.timemanager.getTime()
-		
+
+	def gameOver(self):
+		self._gameover.show()
 
 	def newGame(self):
 		self.loadLevel("maps/shooter_map1.xml")
@@ -226,6 +231,9 @@
 			score = unicode(str(player.score))
 			self._hudwindow.setScoreText(score)
 			
+			lives = unicode(str(player.lives))
+			self._hudwindow.setLivesText(lives)
+			
 		else:
 			if not self.scene.paused:
 				self.scene.pause(self.timemanager.getTime() - self._starttime)