diff --git a/colors.py b/colors.py index 9770e06..6aa5590 100644 --- a/colors.py +++ b/colors.py @@ -7,6 +7,7 @@ def __init__(self): curses.init_pair(2, curses.COLOR_YELLOW, curses.COLOR_BLACK) curses.init_pair(3, curses.COLOR_WHITE, curses.COLOR_BLACK) curses.init_pair(4, curses.COLOR_RED, curses.COLOR_BLACK) + curses.init_pair(5, curses.COLOR_GREEN, curses.COLOR_BLACK) Colors.DARK_GRAY = curses.color_pair(1) | curses.A_BOLD Colors.BROWN = curses.color_pair(2) @@ -14,3 +15,6 @@ def __init__(self): Colors.LIGHT_GRAY = curses.color_pair(3) Colors.DARK_RED = curses.color_pair(4) Colors.YELLOW = curses.color_pair(2) | curses.A_BOLD + Colors.GREEN = curses.color_pair(5) | curses.A_BOLD + Colors.RED = curses.color_pair(4) | curses.A_BOLD + Colors.DARK_GREEN = curses.color_pair(5) \ No newline at end of file diff --git a/items.py b/items.py index 17f8863..66dba75 100644 --- a/items.py +++ b/items.py @@ -22,4 +22,4 @@ def glyph(self): return('K', Colors.DARK_RED) def __str__(self): - return "key" + return "key" \ No newline at end of file diff --git a/map.txt b/map.txt index 422e34c..78a85f4 100644 --- a/map.txt +++ b/map.txt @@ -1,3 +1,4 @@ + ################################################################################ #......2###############................#.......................................# #######.###############+##########.....#.......................................# diff --git a/rogue.py b/rogue.py index e5ef503..1766b55 100755 --- a/rogue.py +++ b/rogue.py @@ -12,7 +12,6 @@ class Map: def __init__(self, filename): with open(filename) as f: self.tiles = [] - for y, r in enumerate(f): row = [] for x, c in enumerate(r.strip()): @@ -26,11 +25,27 @@ def show(self, screen): screen.addstr(y, x, character, color_pair) +class Panel: + def __init__(self, some_player): + self.player = some_player + + def show(self, screen): + screen.addstr(0, 54, "lives: {0:<3}"\ + .format(str(self.player.get_life())),\ + Colors.GREEN if self.player.get_life() > 1 else Colors.RED) + screen.addstr(0, 64, "health: {0:<4}".format(str(self.player.get_health())) ,Colors.GREEN if self.player.get_health() > 10 else Colors.RED) + + class Player: - def __init__(self, y, x): + MAX_HEALTH = 100 + MAX_LIFE = 3 + + def __init__(self, y, x, health = MAX_HEALTH, life = MAX_LIFE): self.y = y self.x = x self.items = [] + self.__health = health + self.__life = life def show(self, screen): character, color_pair = self.glyph() @@ -49,10 +64,8 @@ def move(self, tile): def drop(self, tile, item): if item is None: raise actions.ActionException("you don't have such an item") - if tile.item is not None: raise actions.ActionException("there's no space on the floor") - tile.item = item self.items.remove(item) return tile.item @@ -60,7 +73,6 @@ def drop(self, tile, item): def pickup(self, tile): if not tile.item: raise actions.ActionException("there's nothing here") - self.items.append(tile.item) tile.item = None return self.items[-1] @@ -74,6 +86,42 @@ def close(self, tile): def use(self, tile, item): return tile.use(item) + def get_health(self): + return self.__health + + def get_life(self): + return self.__life + + def decrease_health(self, health_points, max_health = MAX_HEALTH): + if self.__health - health_points > 0: + self.__health -= health_points + else: + life_to_subtract = 1 + ((health_points - self.__health) // max_health) + health_to_subtract = (health_points - self.__health) % max_health + self.decrease_life(life_to_subtract) + if self.__life > 0: + self.__health = max_health -health_to_subtract + else: + self.__health = 0 + + def increase_health(self, health_points, max_health = MAX_HEALTH): + if self.__health + health_points < max_health: + if self.__life > 0: + self.__health += health_points + else: + self.__health = max_health + + def decrease_life(self, life_points): + if self.__life - life_points > 0: + self.__life -= life_points + else: + self.__life = 0 + + def increase_life(self, life_points, max_life = MAX_LIFE): + if self.__life + life_points < max_life: + self.__life += life_points + else: + self.__life = max_life class Game: DIRECTIONS = [ @@ -85,9 +133,11 @@ class Game: def __init__(self, screen, map): self.screen = screen - self.status = curses.newwin(1, 80, 25, 0) + self.status = curses.newwin(1, 80, 26, 0) self.map = Map(map) - self.player = Player(1, 1) + self.player = Player(2, 1) + self.panel = Panel(self.player) + def _get_direction(self, message=None, pressed=None): if message: @@ -120,7 +170,6 @@ def action(self): # open if pressed == ord('o'): - y, x = self._get_direction("Open what") if y and x: if str(self.map.tiles[y][x]) == 'teleport': @@ -152,6 +201,13 @@ def action(self): if y and x: return actions.Use(self.player, self.map.tiles[y][x], item) + # health test - remove it + if pressed == ord('x'): + self.player.decrease_health(5) + if pressed == ord('z'): + self.player.increase_health(20) + + # options if pressed == ord('?'): return actions.Wait(": walk, O: open, C: close, P: pick up, D: drop, U: use") @@ -169,10 +225,12 @@ def run(self): while True: self.map.show(self.screen) + self.panel.show(self.screen) self.player.show(self.screen) self.screen.refresh() action = self.action() + if action: message, color = action.perform() #print(message, color) # trzeba by wypisywac do pliku, tj. robic log diff --git a/tiles.py b/tiles.py index 4d8d606..54789ca 100644 --- a/tiles.py +++ b/tiles.py @@ -143,7 +143,6 @@ def use(self, item): super().use(item) - # new comment class TileFactory: TILES = { @@ -153,7 +152,7 @@ class TileFactory: '1': KeyDoor, '2': KeyFloor, '3': Teleport, - '(': KnifeFloor + '(': KnifeFloor, } @staticmethod