From bf461cbd055b2f1a4c5b092f5e5f44a4c64f1a03 Mon Sep 17 00:00:00 2001 From: Ben Friedland Date: Mon, 7 Jan 2013 05:16:17 -0800 Subject: Orc battle at entrance of cave. Player now has hit_points. --- weatbag/__init__.py | 19 +++++++++++++++ weatbag/tiles/e2.py | 8 +++++++ weatbag/tiles/n2.py | 4 +++- weatbag/tiles/n3.py | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 weatbag/tiles/n3.py (limited to 'weatbag') diff --git a/weatbag/__init__.py b/weatbag/__init__.py index 7c39d21..f9c34bf 100644 --- a/weatbag/__init__.py +++ b/weatbag/__init__.py @@ -8,6 +8,7 @@ class Player: self.name = name self.inventory = Counter() self.position = (0,0) + self.hit_points = 6 def has(self, item): "Does the player have any of item?" @@ -24,6 +25,24 @@ class Player: else: raise KeyError(item) + def state_string(self): + "Returns a string reporting the players health." + if self.hit_points >= 6: + return "feeling great" + elif self.hit_points == 5: + return "feeling good" + elif self.hit_points == 4: + return "feeling ok" + elif self.hit_points == 3: + return "feeling poor" + elif self.hit_points == 2: + return "feeling very unhealthy" + elif self.hit_points == 1: + return "nearly dead" + elif self.hit_points == 0: + return "dead" + + class World: def __init__(self): self.tiles = {} diff --git a/weatbag/tiles/e2.py b/weatbag/tiles/e2.py index 2a8690b..ca66475 100644 --- a/weatbag/tiles/e2.py +++ b/weatbag/tiles/e2.py @@ -22,6 +22,14 @@ class Tile: def action(self, player, do): if do[0] == "guess" and self.contents['unlit torch']: try: + if do[1] == "correctly": + print("You sly dog, you!") + print("The dwarf hands you a torch! " + "He then suddenly vanishes, leaving behind a pile of dust.") + self.contents['unlit torch'] -= 1 + player.give('unlit torch') + return + guess = int(do[1]) self.guess_count += 1 if guess > self.lucky_number: diff --git a/weatbag/tiles/n2.py b/weatbag/tiles/n2.py index b903ed3..a9bca43 100644 --- a/weatbag/tiles/n2.py +++ b/weatbag/tiles/n2.py @@ -2,7 +2,9 @@ from weatbag import words class Tile: def describe(self): - print("You are standing in the mouth of a cave. The air feels cool.") + print("You are standing in the mouth of a cave. The air feels cool and " + "is filled with the stench of rotting flesh. " + "A dark path leads to the north. An exit to the south.") def leave(self, player, direction): if direction=='s': diff --git a/weatbag/tiles/n3.py b/weatbag/tiles/n3.py new file mode 100644 index 0000000..e0a1a4a --- /dev/null +++ b/weatbag/tiles/n3.py @@ -0,0 +1,67 @@ +from weatbag import words +import random + +class Tile: + def __init__(self): + self.enemy_dead = False + self.enemy_life = 2 + self.player_life = 4 + + def describe(self): + if not self.enemy_dead: + print("You encounter an orc! He's got a mean look, but you're a " + "strong lad. You should be able to take him. Swing or flee?") + else: + print("You see an orc corpse here.") + + + def enemy_swing(self, player): + if self.enemy_dead: + return + print("The enemy takes a swing at you!") + hit = random.choice((True,False)) + if hit: + print("He hits you!") + player.hit_points -= 1 + self.report_player_state(player) + else: + print("He misses!") + + def player_swing(self, player): + hit = random.choice((True,False)) + if hit: + print("You hit him!") + self.enemy_life -= 1 + else: + print("You miss!") + + if self.enemy_life == 0: + print("You have killed the orc!") + self.enemy_dead = True + + def report_player_state(self, player): + print("You are %s" % (player.state_string())) + + def leave(self, player, direction): + if not self.enemy_dead: + if not random.choice((True,False)): + print("The orc blocks your way!") + self.enemy_swing(player) + return False + else: + print("You manage to flee!") + + if direction=='s': + return True + + def action(self, player, do): + if do[0] == "flee": + self.leave(player, 's') + return + + if do[0] == "swing": + self.player_swing(player) + self.enemy_swing(player) + + else: + print("Sorry, I don't understand") -- cgit v1.3 From d531a0e0a763b2066bc94446df3dc5425538a1fc Mon Sep 17 00:00:00 2001 From: Ben Friedland Date: Mon, 7 Jan 2013 05:27:38 -0800 Subject: Removed additional player_life attribute from n3 tile. It's been moved to Player class. --- weatbag/tiles/n3.py | 1 - 1 file changed, 1 deletion(-) (limited to 'weatbag') diff --git a/weatbag/tiles/n3.py b/weatbag/tiles/n3.py index e0a1a4a..93933d8 100644 --- a/weatbag/tiles/n3.py +++ b/weatbag/tiles/n3.py @@ -5,7 +5,6 @@ class Tile: def __init__(self): self.enemy_dead = False self.enemy_life = 2 - self.player_life = 4 def describe(self): if not self.enemy_dead: -- cgit v1.3 From a537a2b9c2ac6f9816368c4284d9b8073e94a8ea Mon Sep 17 00:00:00 2001 From: Ben Friedland Date: Mon, 7 Jan 2013 05:54:05 -0800 Subject: Support for attack words. --- weatbag/tiles/n3.py | 4 ++-- weatbag/words.py | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) (limited to 'weatbag') diff --git a/weatbag/tiles/n3.py b/weatbag/tiles/n3.py index 93933d8..a290372 100644 --- a/weatbag/tiles/n3.py +++ b/weatbag/tiles/n3.py @@ -9,7 +9,7 @@ class Tile: def describe(self): if not self.enemy_dead: print("You encounter an orc! He's got a mean look, but you're a " - "strong lad. You should be able to take him. Swing or flee?") + "strong one. You should be able to take him. Attack or flee?") else: print("You see an orc corpse here.") @@ -58,7 +58,7 @@ class Tile: self.leave(player, 's') return - if do[0] == "swing": + if do[0] in words.attack: self.player_swing(player) self.enemy_swing(player) diff --git a/weatbag/words.py b/weatbag/words.py index 11ede7d..25f762e 100644 --- a/weatbag/words.py +++ b/weatbag/words.py @@ -14,6 +14,7 @@ fight = {'fight', 'kill', 'hit', 'attack'} drop = {'drop'} take = {'pick', 'take', 'get', 'collect'} look = {'look', 'inspect', 'examine'} +attack = {'attack', 'swing', 'hit', 'punch', 'fight'} inventory = {'inventory', 'possessions', 'belongings', 'bag'} surroundings = {'surroundings', 'around', 'scenery'} -- cgit v1.3 From 3276c1c553e80780e2cd88032b61b923517dc115 Mon Sep 17 00:00:00 2001 From: Ben Friedland Date: Mon, 7 Jan 2013 06:58:47 -0800 Subject: leave method returns True if they manage to flee --- weatbag/tiles/n3.py | 1 + 1 file changed, 1 insertion(+) (limited to 'weatbag') diff --git a/weatbag/tiles/n3.py b/weatbag/tiles/n3.py index a290372..992d7f2 100644 --- a/weatbag/tiles/n3.py +++ b/weatbag/tiles/n3.py @@ -49,6 +49,7 @@ class Tile: return False else: print("You manage to flee!") + return True if direction=='s': return True -- cgit v1.3 From bde94f80e518182888d944430bcfe7329d48e817 Mon Sep 17 00:00:00 2001 From: Ben Friedland Date: Mon, 7 Jan 2013 07:19:31 -0800 Subject: Else condition for reporting health is now a catch-all (in case they go into negative hit points). --- weatbag/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'weatbag') diff --git a/weatbag/__init__.py b/weatbag/__init__.py index f9c34bf..303962e 100644 --- a/weatbag/__init__.py +++ b/weatbag/__init__.py @@ -39,7 +39,7 @@ class Player: return "feeling very unhealthy" elif self.hit_points == 1: return "nearly dead" - elif self.hit_points == 0: + else: return "dead" -- cgit v1.3 From 6d1a17c456b15841763b953a1427e8bd856f2263 Mon Sep 17 00:00:00 2001 From: Ben Friedland Date: Mon, 7 Jan 2013 22:24:09 -0800 Subject: Fixed leave condition at cave entrance. --- weatbag/tiles/n3.py | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'weatbag') diff --git a/weatbag/tiles/n3.py b/weatbag/tiles/n3.py index 992d7f2..7591110 100644 --- a/weatbag/tiles/n3.py +++ b/weatbag/tiles/n3.py @@ -53,6 +53,11 @@ class Tile: if direction=='s': return True + if direction=='n': + return True + + print("There is no path leading in that direction.") + return False def action(self, player, do): if do[0] == "flee": -- cgit v1.3 From ead0301daaa6f07d9fc740050019bf204543f0ea Mon Sep 17 00:00:00 2001 From: Thomas Kluyver Date: Thu, 10 Jan 2013 16:45:18 +0000 Subject: Allow custom message when player attempts to move to nonexistent tile. Closes gh-7 --- doc/interface.rst | 6 ++++++ weatbag/__init__.py | 7 ++++--- weatbag/tiles/n3.py | 8 ++------ 3 files changed, 12 insertions(+), 9 deletions(-) (limited to 'weatbag') diff --git a/doc/interface.rst b/doc/interface.rst index 796fa4b..dd960b4 100644 --- a/doc/interface.rst +++ b/doc/interface.rst @@ -52,3 +52,9 @@ Your module should define a class called ``Tile``, with the following interface: :param player: the active :class:`~weatbag.Player` instance :param direction: the direction the player is trying to leave, as a single lowercase character, n/e/s/w + + .. attribute:: no_path_msg + + *Optional.* The message to display if the player attempts to leave in a + direction where there is no tile. The default message says "The undergrowth + in that direction is impassable. You turn back." diff --git a/weatbag/__init__.py b/weatbag/__init__.py index 9a743e0..511e8bc 100644 --- a/weatbag/__init__.py +++ b/weatbag/__init__.py @@ -101,7 +101,9 @@ def main(): name = input("What is your name? ") player = Player(name) interact(player) - + +no_path_msg = "The undergrowth in that direction is impassable. You turn back." + def interact(player): world = World() tile = world[player.position] @@ -120,8 +122,7 @@ def interact(player): try: tile = world[new_posn] except KeyError: - print("The undergrowth in that direction is impassable. " - "You turn back.") + print(getattr(tile, 'no_path_msg', no_path_msg)) else: player.position = new_posn tile.describe() diff --git a/weatbag/tiles/n3.py b/weatbag/tiles/n3.py index 7591110..125898b 100644 --- a/weatbag/tiles/n3.py +++ b/weatbag/tiles/n3.py @@ -51,13 +51,9 @@ class Tile: print("You manage to flee!") return True - if direction=='s': - return True - if direction=='n': - return True + return True - print("There is no path leading in that direction.") - return False + no_path_msg = "There is no path leading in that direction." def action(self, player, do): if do[0] == "flee": -- cgit v1.3