Commits

Radomir Dopieralski committed d7a7310

Add player hit points

Comments (0)

Files changed (4)

jelly/__init__.py

 #!/usr/bin/env python
 # -*- coding: utf-8 -*-
 
-import sys
-
 import pygame
 from pygame import locals as pg
 
         self.clock = pygame.time.Clock()
 
     def run(self):
-        self.start()
-        self.main()
+        try:
+            while True:
+                try:
+                    self.start()
+                    while True:
+                        if not self.main():
+                            self.game_over()
+                        else:
+                            self.victory()
+                except util.QuitLevel:
+                    pass
+        except util.QuitGame:
+            return
 
-    def start(self):
-        self.display.render_image('jelly/title.png')
+    def game_menu(self):
         menu = message.Menu((80, 380), ['Start new game', 'Exit'])
         self.display.render_menu(menu)
         choice = None
                 self.display.draw()
                 choice = menu.update()
         except (util.QuitLevel, util.QuitGame):
-            sys.exit()
+            raise util.QuitGame
         if choice == 1:
-            sys.exit()
+            raise util.QuitGame
+
+    def start(self):
+        self.display.render_image('jelly/title.png')
+        self.game_menu()
+
+    def game_over(self):
+        self.display.render_image('jelly/dead.png')
+        self.game_menu()
 
     def next_level(self):
         self.level = rooms_level.Level(self.pc)
         self.display.render_level(self.level)
 
     def main(self):
-        self.game_over = False
         self.pc = mobs.Player()
         self.next_level()
-        try:
-            while not self.game_over:
-                self.clock.tick(15)
-                self.display.update()
-                self.display.draw()
-                for event in pygame.event.get():
-                    if event.type == pg.ACTIVEEVENT and event.gain:
-                        self.display.refresh()
-                    if event.type == pg.QUIT:
-                        raise util.QuitGame()
-                    elif event.type == pg.KEYDOWN and event.key == pg.K_n:
-                        self.next_level()
-                    elif event.type == pg.KEYDOWN and event.key == pg.K_ESCAPE:
-                        raise util.QuitLevel()
-                try:
-                    pc_animation = self.display.mob_sprites[id(self.pc)].animations
-                except KeyError:
-                    pc_animation = None
-                    self.display.events(self.level.events)
-                    self.level.events = []
-                if not pc_animation:
-                    if self.pc.control(self.level):
-                        for mob in list(self.level.mobs):
-                            if mob == self.pc:
-                                continue
-                            mob.control(self.level)
-                    self.display.events(self.level.events)
-                    self.level.events = []
-        except util.QuitLevel:
-            return
-        except util.QuitGame:
-            sys.exit()
+        while not self.level.is_game_over:
+            self.clock.tick(15)
+            self.display.update()
+            self.display.draw()
+            for event in pygame.event.get():
+                if event.type == pg.ACTIVEEVENT and event.gain:
+                    self.display.refresh()
+                if event.type == pg.QUIT:
+                    raise util.QuitGame()
+                elif event.type == pg.KEYDOWN and event.key == pg.K_n:
+                    self.next_level()
+                elif event.type == pg.KEYDOWN and event.key == pg.K_ESCAPE:
+                    raise util.QuitLevel()
+            try:
+                pc_animation = self.display.mob_sprites[id(self.pc)].animations
+            except KeyError:
+                pc_animation = None
+                self.display.events(self.level.events)
+                self.level.events = []
+            if not pc_animation:
+                if self.pc.control(self.level):
+                    for mob in list(self.level.mobs):
+                        if mob == self.pc:
+                            continue
+                        mob.control(self.level)
+                self.display.events(self.level.events)
+                self.level.events = []
 
         self.BlockedMob = BlockedMob
 
         self.events = []
+        self.is_game_over = False
 
     def __getitem__(self, (x, y)):
         if self.width > x >= 0 and self.height > y >= 0:
         if self.width > x >= 0 and self.height > y >= 0:
             self.map[y][x] = value
 
+    def is_blocked(self, pos):
+        for m in self.mobs:
+            if m.pos == pos:
+                return True
+        return not self[pos]
+
+
     def walk(self, mob, direction):
         if direction is None:
             return
         if other is not None:
             self.hit(other, mob)
 
-    def is_blocked(self, pos):
-        for m in self.mobs:
-            if m.pos == pos:
-                return True
-        return not self[pos]
+    def game_over(self):
+        self.events.append(('mob_death', self.pc))
+        self.is_game_over = True
 
     def update(self):
         for event in pygame.event.get():
-            if event.type == pg.ACTIVEEVENT and event.gain:
-                self.display.refresh()
-            elif event.type == pg.KEYDOWN and event.key == pg.K_UP:
+            if event.type == pg.KEYDOWN and event.key == pg.K_UP:
                 self.move(-1)
             elif event.type == pg.KEYDOWN and event.key == pg.K_DOWN:
                 self.move(1)
             elif event.type == pg.KEYDOWN and event.key == pg.K_z:
-                return self.cursor.item
+                item = self.cursor.item
+                for sprite in self.sprites:
+                    sprite.kill()
+                return item
             elif event.type == pg.QUIT:
                 raise util.QuitGame()
             elif event.type == pg.KEYDOWN and event.key == pg.K_ESCAPE:
         self.hp = self.max_hp
         self.status = {}
 
-    def make_sprite(self, tiles):
-        sprite = self.Sprite(tiles, self.pos, self.animation)
-        shadow = sprites.Shadow(tiles, sprite)
-        return sprite, shadow, None
-
     def control(self, level):
         for behavior in self.behaviors:
             if behavior(self, level):
     animation = None
     color = 5
 
+    def __init__(self):
+        self.max_hp = 100
+        self.hp = self.max_hp
+
+
     def place(self, (x, y)):
         self.pos = x, y
         self.direction = 0
 
     def hit(self, level, attacker):
         self.direction = pos2dir(self.pos, attacker.pos)
+        self.hp -= 1
+        if self.hp <= 0:
+            level.game_over()
         return True
 
     def control(self, level):