class Player(object): def __init__(self,room, inventory): self.room = room self.inventory = inventory def check_command(self, command): if command in self.room.exits: self.move(command) def show_inventory(self): print("You are carrying: ") if len(self.inventory) != 0: for item in self.inventory: print "- " + item.name+"\n" else: print("nothing\n") def look(self): print ("You are in the " + self.room.name + ".\n") if len(self.room.items) != 0: print ("You see:") for item in self.room.items: print "- " + item.name def move(self, direction): if self.room.exits[direction] is None: print ("No exit") else: self.room = self.room.exits[direction] def take(self, item): if item in self.room.items: print ("You take the " + item.short_name + ".") self.inventory.append(item) self.room.items.remove(item) else: print ("I can't see that here.") def drop(self, item): if item in self.inventory: print ("You drop the " + item.short_name + " on the floor.") self.inventory.remove(item) self.room.items.append(item) else: print ("You're not carrying that.") def eat(self, item): if item in self.inventory: if item.eatable: print("Nom nom nom\n") self.inventory.remove(item) else: print("You can't eat " + item.name + "!") elif item in self.room.items: print("You don't have it.") else: print("I don't see that here.") #---------------------------------------------------------- class Room(object): def __init__(self, name, items): self.name = name self.items = items self.exits = {} def assign_exits(self, exit_dict): self.exits.update(exit_dict) #---------------------------------------------------------- class Item(object): def __init__(self, name, short_name, eatable): self.name = name self.short_name = short_name self.eatable = eatable #---------------------------------------------------------- #room items carpet = Item("a carpet", "carpet", False) tv = Item("a TV", "TV", False) radio = Item("a radio", "radio",False) apple = Item("an apple", "apple", True) key = Item("a key", "key", False) #put items in lists red_items = [carpet] blue_items = [tv,radio] green_items = [apple] inventory = [key] #creating rooms + placing items red_room = Room("red room",red_items) blue_room = Room("blue room",blue_items) green_room = Room("green room",green_items) #assigning room exits red_room.assign_exits({"s": blue_room, "n":None}) blue_room.assign_exits({"n": red_room, "s": green_room}) green_room.assign_exits({"n": blue_room, "s":None}) #creating player in starting room player = Player(green_room, inventory) #testing the functions: if __name__ == '__main__': player.look() player.show_inventory() player.drop(key) player.show_inventory() player.eat(apple) player.take(apple) player.eat(apple) player.look() '''print player.room.name print player.room.items print ("Inv: " +str(player.inventory)) player.check_command("s") print player.room.name print player.room.items print ("Inv: " +str(player.inventory)) player.take("TV") print player.room.items print ("Inv: " +str(player.inventory)) player.drop("key") print player.room.items print ("Inv: " +str(player.inventory))'''
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question