|
| 1 | +import shlex |
| 2 | + |
| 3 | +class Player(object): |
| 4 | + def __init__(self, location): |
| 5 | + self.location = location |
| 6 | + self.location.here.append(self) |
| 7 | + self.playing = True |
| 8 | + self.inventory = [] |
| 9 | + |
| 10 | + def get_input(self): |
| 11 | + return raw_input(">") |
| 12 | + |
| 13 | + def process_input(self, input): |
| 14 | + parts = shlex.split(input) |
| 15 | + if len(parts) == 0: |
| 16 | + return [] |
| 17 | + if len(parts) == 1: |
| 18 | + parts.append("") |
| 19 | + verb = parts[0] |
| 20 | + noun = " ".join(parts[1:]) |
| 21 | + |
| 22 | + handler = self.find_handler(verb, noun) |
| 23 | + if handler is None: |
| 24 | + return [input + |
| 25 | + "? I don't know how to do that!"] |
| 26 | + return handler(self, noun) |
| 27 | + |
| 28 | + def find_handler(self, verb, noun): |
| 29 | + if noun != "": |
| 30 | + object = [x for x in self.location.here + self.inventory |
| 31 | + if x is not self and |
| 32 | + x.name == noun and |
| 33 | + verb in x.actions] |
| 34 | + if len(object) > 0: |
| 35 | + return getattr(object[0], verb) |
| 36 | + if verb.lower() in self.actions: |
| 37 | + return getattr(self, verb) |
| 38 | + elif verb.lower() in self.location.actions: |
| 39 | + return getattr(self.location, verb) |
| 40 | + |
| 41 | + def quit(self, player, noun): |
| 42 | + self.playing = False |
| 43 | + return ["bye bye!"] |
| 44 | + |
| 45 | + actions = ['quit', 'inv', 'get', 'drop'] |
| 46 | + |
| 47 | + def get(self, player, noun): |
| 48 | + return [noun + "? I can't see that here."] |
| 49 | + |
| 50 | + def drop(self, player, noun): |
| 51 | + return [noun + "? I don't have that!"] |
| 52 | + |
| 53 | + def inv(self, player, noun): |
| 54 | + result = ["You have:"] |
| 55 | + if self.inventory: |
| 56 | + result += [x.name for x in self.inventory] |
| 57 | + else: |
| 58 | + result += ["nothing!"] |
| 59 | + return result |
| 60 | + |
| 61 | +def test(): |
| 62 | + import cave |
| 63 | + empty_cave = cave.Cave( |
| 64 | + "Empty Cave", |
| 65 | + "A desolate, empty cave, " |
| 66 | + "waiting for someone to fill it.") |
| 67 | + |
| 68 | + import item |
| 69 | + sword = item.Item("sword", "A pointy sword.", empty_cave) |
| 70 | + coin = item.Item("coin", "A shiny gold coin. " |
| 71 | + "Your first piece of treasure!", empty_cave) |
| 72 | + |
| 73 | + player = Player(empty_cave) |
| 74 | + |
| 75 | + print player.location.name |
| 76 | + print player.location.description |
| 77 | + while player.playing: |
| 78 | + input = player.get_input() |
| 79 | + result = player.process_input(input) |
| 80 | + print "\n".join(result) |
| 81 | + |
| 82 | +if __name__ == '__main__': |
| 83 | + import cave |
| 84 | + caves = cave.create_caves() |
| 85 | + |
| 86 | + cave1 = caves[0] |
| 87 | + import item |
| 88 | + sword = item.Item("sword", "A pointy sword.", cave1) |
| 89 | + coin = item.Item("coin", "A shiny gold coin. " |
| 90 | + "Your first piece of treasure!", cave1) |
| 91 | + |
| 92 | + player = Player(cave1) |
| 93 | + print '\n'.join(player.location.look(player, '')) |
| 94 | + while player.playing: |
| 95 | + input = player.get_input() |
| 96 | + result = player.process_input(input) |
| 97 | + print "\n".join(result) |
| 98 | + |
| 99 | + |
| 100 | + |
0 commit comments