Steven Smith revised this gist . Go to revision
1 file changed, 103 insertions
textadventure_sample.py(file created)
| @@ -0,0 +1,103 @@ | |||
| 1 | + | """Welcome to py-textadventure! | |
| 2 | + | ||
| 3 | + | This is a library intended to make writing text adventures really simple. | |
| 4 | + | ||
| 5 | + | Below is a sample text adventure that mirrors the first few areas of Zork with a couple small changes. | |
| 6 | + | ||
| 7 | + | I hope you enjoy using this library!""" | |
| 8 | + | ||
| 9 | + | from textadventure import Room, Object, Item | |
| 10 | + | import random | |
| 11 | + | ||
| 12 | + | ||
| 13 | + | class Mailbox(Object): | |
| 14 | + | # view text is shown in the 'look' text for a given room | |
| 15 | + | view = "There is a small mailbox here." | |
| 16 | + | # Descriptions are shown with 'examine Mailbox' | |
| 17 | + | description = "I see nothing special about the mailbox." | |
| 18 | + | # if objects is not None, an object is openable (closed by default) and contains the given items | |
| 19 | + | objects = [Leaflet] | |
| 20 | + | ||
| 21 | + | # Object = fixed part of scene. Item = inventoriable object | |
| 22 | + | class Leaflet(Item): | |
| 23 | + | description = __doc__ | |
| 24 | + | ||
| 25 | + | class Welcome_Mat(Item): | |
| 26 | + | description = "Welcome to py-textadventure!" | |
| 27 | + | view = "A rubber mat saying \"{}\" lies by the door.".format(self.description) | |
| 28 | + | ||
| 29 | + | class Handset(Item): | |
| 30 | + | description = "The voice on the other end is crackly, but you can make out the words \"Thank you for playing this sample game!\" before it explodes, killing you instantly." | |
| 31 | + | instant_death = True | |
| 32 | + | ||
| 33 | + | ||
| 34 | + | # Room bold text is taken from class name (West_of_House >> "West of House"), but can be overridden | |
| 35 | + | class West_of_House(Room): | |
| 36 | + | description = "This is an open field west of a white house, with a boarded front door." | |
| 37 | + | objects = [Mailbox, Welcome_Mat] | |
| 38 | + | north = North_of_House | |
| 39 | + | south = South_of_House | |
| 40 | + | west = Forest | |
| 41 | + | east = False, "The door is locked, and there is evidently no key." | |
| 42 | + | ||
| 43 | + | _house_desc = "You are facing the {} side of a white house. There is no door here, and all the windows are barred." | |
| 44 | + | class North_of_House(Room): | |
| 45 | + | description = _house_desc.format("north") | |
| 46 | + | north = Forest | |
| 47 | + | south = False, "The windows are all barred." | |
| 48 | + | east = East_of_House | |
| 49 | + | west = West_of_House | |
| 50 | + | ||
| 51 | + | class South_of_House(Room): | |
| 52 | + | description = _house_desc.format("south") | |
| 53 | + | north = False, "The windows are all barred" | |
| 54 | + | south = Forest | |
| 55 | + | west = West_of_House | |
| 56 | + | east = East_of_House | |
| 57 | + | ||
| 58 | + | class East_of_House(Room): | |
| 59 | + | # Room bold text, defaults to class name without underscores | |
| 60 | + | view = "Behind House" | |
| 61 | + | description = "You are behind the white house. In one corner of the house there is a small window which is slightly ajar." | |
| 62 | + | north = North_of_House | |
| 63 | + | south = South_of_House | |
| 64 | + | west = self.is_window_closed() | |
| 65 | + | east = Forest | |
| 66 | + | objects = [Window] | |
| 67 | + | def is_window_closed(self): | |
| 68 | + | if self.window_open == True: | |
| 69 | + | return Game_Over | |
| 70 | + | return False, "The window is closed." | |
| 71 | + | ||
| 72 | + | class Clearing(Room): | |
| 73 | + | description = "You are in a clearing, with a forest surrounding you on the west and south." | |
| 74 | + | north = Clearing | |
| 75 | + | east = Clearing | |
| 76 | + | west = Forest | |
| 77 | + | south = Forest | |
| 78 | + | ||
| 79 | + | class Forest(Room): | |
| 80 | + | description = random.choice([ | |
| 81 | + | "This is a forest, with trees in all directions around you.", | |
| 82 | + | "This is a large forest, with trees obstructing all views except to the east, where a small clearing may be seen through the trees.", | |
| 83 | + | "This is a dimly lit forest, with large trees all around. To the east, there appears to be sunlight." | |
| 84 | + | ]) | |
| 85 | + | north = self.wandering_test(South_of_House) | |
| 86 | + | south = self.wandering_test(North_of_House) | |
| 87 | + | west = self.wandering_test(East_of_House) | |
| 88 | + | east = Clearing | |
| 89 | + | ||
| 90 | + | def wandering_test(self, dest_room): | |
| 91 | + | if self.player.wandering > 2: | |
| 92 | + | self.player.wandering = 0 | |
| 93 | + | return dest_room | |
| 94 | + | self.player.wandering += 1 | |
| 95 | + | return Forest | |
| 96 | + | ||
| 97 | + | class Game_Over(Room): | |
| 98 | + | description = "The inside of the house is an enormous empty white room with a disconnected handset in the centre. You hear buzzing from the handset." | |
| 99 | + | objects = [Handset] | |
| 100 | + | north = False, "There's no way out there." | |
| 101 | + | south = False, "There's no way out there." | |
| 102 | + | east = East_of_House | |
| 103 | + | west = False, "There's no way out there." | |
Newer
Older