changeset 40:9c5ba35a6596

a simple health display
author Radomir Dopieralski <sheep-devel@sheep.art.pl>
date Tue, 23 Aug 2011 21:45:10 +0200
parents 36e009505a41
children a07bb1dabe9a
files main.py menu.py
diffstat 2 files changed, 32 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/main.py	Tue Aug 23 21:21:28 2011 +0200
+++ b/main.py	Tue Aug 23 21:45:10 2011 +0200
@@ -6,7 +6,7 @@
 import display
 import level
 import hero
-
+import menu
 
 class Game(object):
     def __init__(self):
@@ -14,6 +14,9 @@
         self.display = display.Display((320, 200))
         self.hero = hero.Hero()
         self.start_level()
+        self.health = menu.GameInfo(288, 8, 'HP %d/10' % self.hero.hp,
+                                    (32, 192, 0))
+        self.display.menus.add(self.health)
 
     def start_level(self):
         self.display.empty()
@@ -32,6 +35,7 @@
             if self.level.map[self.hero.y][self.hero.x] == '>':
                 self.hero.exit(self.level)
                 return
+            self.health.change('HP %d/10' % self.hero.hp)
             pressed = self.display.get_pressed()
             if pygame.locals.K_UP in pressed:
                 self.hero.walk(self.level, 0, -1)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/menu.py	Tue Aug 23 21:45:10 2011 +0200
@@ -0,0 +1,27 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+
+import pygame
+import effect
+
+
+class GameInfo(pygame.sprite.Sprite):
+    def __init__(self, x, y, text, color):
+        super(GameInfo, self).__init__()
+        self.font = pygame.font.Font(None, 16)
+        self.x, self.y = x, y
+        self.text = text
+        self.color = color
+        self.image = effect.outlined_text(self.font, text, color, (32, 32, 32))
+        self.rect = self.image.get_rect()
+        self.rect.center = self.x, self.y
+
+    def change(self, text=None, color=None):
+        if text is not None:
+            self.text = text
+        if color is not None:
+            self.color = color
+        self.image = effect.outlined_text(self.font, self.text, self.color,
+                                          (32, 32, 32))
+        self.rect = self.image.get_rect()
+        self.rect.center = self.x, self.y