Skip to content

[WIP] Player's state. #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ 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)
Colors.WHITE = curses.color_pair(3) | curses.A_BOLD
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)
2 changes: 1 addition & 1 deletion items.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ def glyph(self):
return('K', Colors.DARK_RED)

def __str__(self):
return "key"
return "key"
1 change: 1 addition & 0 deletions map.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

################################################################################
#......2###############................#.......................................#
#######.###############+##########.....#.......................................#
Expand Down
74 changes: 66 additions & 8 deletions rogue.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()):
Expand All @@ -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()
Expand All @@ -49,18 +64,15 @@ 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

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]
Expand All @@ -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 = [
Expand All @@ -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:
Expand Down Expand Up @@ -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':
Expand Down Expand Up @@ -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("<arrows>: walk, O: open, C: close, P: pick up, D: drop, U: use")

Expand All @@ -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
Expand Down
3 changes: 1 addition & 2 deletions tiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ def use(self, item):
super().use(item)



# new comment
class TileFactory:
TILES = {
Expand All @@ -153,7 +152,7 @@ class TileFactory:
'1': KeyDoor,
'2': KeyFloor,
'3': Teleport,
'(': KnifeFloor
'(': KnifeFloor,
}

@staticmethod
Expand Down