changeset 12:2dcb3b6fb6f4

Player character death
author Radomir Dopieralski <sheep-devel@sheep.art.pl>
date Sun, 28 Apr 2013 22:36:37 +0200
parents 9964f1c0236c
children 92a7c310241f
files blow/game.py blow/game_scene.py blow/scene.py
diffstat 3 files changed, 19 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/blow/game.py	Sun Apr 28 21:33:56 2013 +0200
+++ b/blow/game.py	Sun Apr 28 22:36:37 2013 +0200
@@ -21,7 +21,7 @@
         self.scenes.append(
             GameScene(self.display, self.resources)
         )
-        self.scenes[-1].update(True)
+        self.scenes[-1].update(self.scenes, True)
         while self.scenes:
-            self.scenes[-1].update()
+            self.scenes[-1].update(self.scenes)
 
--- a/blow/game_scene.py	Sun Apr 28 21:33:56 2013 +0200
+++ b/blow/game_scene.py	Sun Apr 28 22:36:37 2013 +0200
@@ -5,7 +5,7 @@
 import pygame
 
 from blow.sprite import Sprite
-from blow.scene import Scene
+from blow.scene import Scene, GameOver
 
 
 BOTTOM = 420
@@ -79,6 +79,8 @@
         self.sword = SwordSprite(self, scene.resources.weapons)
         scene.sprites.add(self.sword)
         self.stunned = False
+        self.hp = 5
+        self.rect.midbottom = (self.hp * 64, BOTTOM)
 
     def walk(self, scene, wx):
         self.rect.move_ip(wx, 0)
@@ -105,16 +107,21 @@
                 self.jump(scene)
             if keys[pygame.K_SPACE]:
                 self.swing(scene)
-        if self.rect.left < SCROLL:
+        if self.rect.left < self.hp * 64:
             scene.scroll(6, 0)
         elif self.rect.right > 768 - SCROLL:
             scene.scroll(-6, 0)
+        if self.hp <= 0 and not self.stunned:
+            raise GameOver('lost')
 
 
     def stun(self, scene):
+        if self.stunned:
+            return
         self.dy = -8
         self.dx = -6
         self.animation = self.stun_animation(scene)
+        self.hp -= 1
 
     def stun_animation(self, scene):
         self.image = self.frames[1]
@@ -320,7 +327,7 @@
         self.image = self.frames[6]
         self.dead = True
         for i in xrange(20):
-            scene.game_over = True
+            raise GameOver('won')
 
 
 class SwordSprite(Sprite):
@@ -401,9 +408,7 @@
         super(GameScene, self).__init__(display, resources)
         self.monsters = pygame.sprite.Group()
         self.background.fill((0xf2, 0xf0, 0xf4))
-        self.game_over = False
         self.hero = HeroSprite(self)
-        self.hero.rect.midbottom = (320, BOTTOM)
         self.sprites.add(self.hero)
 
         for i in xrange(26):
@@ -430,6 +435,3 @@
         for sprite in self.sprites:
             sprite.rect.move_ip(x, y)
 
-    def update(self, *args, **kwargs):
-        super(GameScene, self).update(*args, **kwargs)
-
--- a/blow/scene.py	Sun Apr 28 21:33:56 2013 +0200
+++ b/blow/scene.py	Sun Apr 28 22:36:37 2013 +0200
@@ -10,8 +10,13 @@
         self.sprites = SortedUpdates()
         self.background = self.display.screen.copy()
 
-    def update(self, first=False):
+    def update(self, scenes, first=False):
         tick = self.display.update(self.background, self.sprites, first)
-        self.sprites.update(tick, self)
+        try:
+            self.sprites.update(tick, self)
+        except GameOver:
+            scenes.remove(self)
 
 
+class GameOver(BaseException):
+    """Raise this to end a scene."""