Last active
March 4, 2023 12:49
-
-
Save horstjens/398f65aef4131dd91f07a7ae3eff8c20 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import adventurelib as adv | |
class Game: | |
current_room = None | |
player_inventory = adv.Bag() | |
bedroom = adv.Room(""" | |
This is a bedroom. It's big and comfortable | |
""") | |
livingroom = adv.Room(""" | |
This is the livingroom. | |
""") | |
computerroom = adv.Room(""" | |
A room with computers | |
""") | |
kitchen = adv.Room(""" | |
This is the kitchen. | |
""") | |
bedroom.inventory = adv.Bag() | |
livingroom.inventory = adv.Bag() | |
kitchen.inventory = adv.Bag() | |
computerroom.inventory = adv.Bag() | |
bedroom.west = livingroom | |
livingroom.north = computerroom | |
computerroom.east = kitchen | |
computerroom.locked = {"east":True} | |
bedroom.locked = dict() | |
kitchen.locked = dict() | |
livingroom.locked = dict() | |
Game.current_room = bedroom | |
def use_kitchen_key(): | |
"""returns boolean destroy is True when item was used successfull, otherwise False""" | |
# only works in the computerrrom | |
if Game.current_room == computerroom: | |
print("You unlock the door to the kitchen") | |
computerroom.locked["east"] = False | |
return True | |
else: | |
print("You fail to locate a kitchen door to unlock. Try another room.") | |
return False | |
kitchen_key = adv.Item("an old rusty key for the kitchen door", "key") | |
kitchen_key.use_item = use_kitchen_key | |
bedroom.inventory.add(kitchen_key) | |
@adv.when("use ITEM") | |
def use(item): | |
# do we have item? | |
print("taking item:", item) | |
obj = Game.player_inventory.take(item) | |
if not obj: | |
print("You don't have", item) | |
#destroy = False | |
else: | |
print("using item:", item) | |
destroy = obj.use_item() | |
# destroy item after use? | |
if not destroy: | |
Game.player_inventory.add(obj) | |
@adv.when("inventory") | |
@adv.when("show inventory") | |
@adv.when("show my items") | |
def show(): | |
print("you carry those items with you:") | |
for item in Game.player_inventory: | |
print(item) | |
if len(Game.player_inventory) == 0: | |
print("you carry nothing") | |
@adv.when("take ITEM") | |
def get(item): | |
obj = Game.current_room.inventory.take(item) | |
if not obj: | |
print("In this room there is no ", item, "to take") | |
else: | |
print("You take", item) | |
Game.player_inventory.add(obj) | |
@adv.when("look") | |
def look(): | |
adv.say(Game.current_room) | |
print("Items in this room:") | |
for item in Game.current_room.inventory: | |
print(item) | |
if len(Game.current_room.inventory) == 0: | |
print("no items in this room") | |
print("exits:") | |
print(Game.current_room.exits()) | |
@adv.when("go DIRECTION") | |
@adv.when("DIRECTION") | |
def go(direction): | |
next_room = Game.current_room.exit(direction) | |
if next_room: | |
# locked ? | |
if direction in Game.current_room.locked: | |
if Game.current_room.locked[direction]: | |
print("the door is closed") | |
return | |
Game.current_room = next_room | |
print("you go to another room") | |
look() | |
else: | |
print("This direction is not possible") | |
if __name__ == "__main__": | |
look() | |
print("type help to see a list of all commands") | |
adv.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment