Last active
April 10, 2016 18:25
-
-
Save anirudhpillai/6b8b9ca436d8ae22c9b781979ddbbeae 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 sys | |
class Room: | |
def __init__(self, name, description): | |
self.name = name | |
self.items = [] | |
self.exits = [] | |
self.description = description | |
def __str__(self): | |
return self.name | |
def getExits(self): | |
return self.exits | |
def getName(self): | |
return self.name | |
def describe(self): | |
print(self.description) | |
def setExits(self, list): | |
self.exits = list | |
def setItems(self, items): | |
self.items = items | |
def removeItem(self, item): | |
self.items.remove(item) | |
def addItem(self, item): | |
self.items.append(item) | |
def getItems(self): | |
return self.items | |
class Item: | |
def __init__(self, name, description, weight): | |
self.name = name | |
self.description = description | |
self.weight = weight | |
def getWeight(self): | |
return self.weight | |
def getName(self): | |
return self.name | |
def describe(self): | |
print("%s %s\n %s" % (self.name, self.weight, self.description)) | |
def __str__(self): | |
rep = self.name + " " + str(self.weight) | |
return rep | |
class Character: | |
def __init__(self, name, room): | |
self.name = name | |
self.energy = 3 | |
self.backpack = [] | |
self.backpack_capacity = 10 | |
self.currentRoom = room | |
def describe(self): | |
print(self.name) | |
print("Energy: %d" % self.energy) | |
print("Free space in backpack: %d" % self.backpack_capacity) | |
self.seeItems() | |
print("Currently in %s" % self.currentRoom) | |
def see(self): | |
print("Items in room") | |
for item in self.currentRoom.getItems(): | |
print("%s" % item) | |
print("\nPaths from this room") | |
for room in self.currentRoom.getExits(): | |
print(room) | |
def move(self, newRoom): | |
for room in self.currentRoom.getExits(): | |
if newRoom == room.getName(): | |
self.energy -= 1 | |
if self.energy < 0: | |
print("You ran out of energy!") | |
sys.exit("Thanks for playing") | |
return | |
self.currentRoom = room | |
print("Entered %s:" % room.getName()) | |
room.describe() | |
return | |
print("You can't get to that room from here") | |
def seeItems(self): | |
if not self.backpack: | |
print("No items in backpack") | |
return | |
print("Items in backpack") | |
for item in self.backpack: | |
print(item) | |
def grab(self, itemName): | |
for temp in self.currentRoom.getItems(): | |
if temp.getName() == itemName: | |
if self.backpack_capacity >= temp.getWeight(): | |
self.backpack_capacity -= temp.getWeight() | |
self.backpack.append(temp) | |
self.currentRoom.removeItem(temp) | |
print("%s added to backpack" % temp.getName()) | |
return | |
else: | |
self.currentRoom.addItem(temp) | |
print('You dont have enough space in your backpack') | |
return | |
print("Item is not in room") | |
def discardItem(self, itemName): | |
for temp in self.backpack: | |
if temp.getName() == itemName: | |
self.backpack.remove(temp) | |
self.backpack_capacity += temp.getWeight() | |
print("%s discarded" % temp.getName()) | |
self.currentRoom.addItem(temp) | |
return | |
print("Item not in backpack") | |
def examineItem(self, itemName): | |
for temp in self.currentRoom.getItems(): | |
if temp.getName() == itemName: | |
temp.describe() | |
return | |
for temp in self.backpack: | |
if temp.getName() == itemName: | |
temp.describe() | |
return | |
print("Item not in room or backpack") | |
def executeCommand(hero, command, parameter): | |
if command == "help": | |
print("Commands Manual") | |
print("<...> is just a placeholder") | |
print("status - to get view your health, energy, location and backpack") | |
print("see - to view the rooms you can move to from the current one and also to see the items in the current room") | |
print("move <room name> - to move to the specified room eg: move Bedroom") | |
print("grab <item name> - to add specified item to backpack") | |
print("throw <item name> - to remove item from backpack") | |
print("examine <item name> - to get more details about the specified item") | |
print("quit - to finish the game") | |
elif command == "status": | |
hero.describe() | |
elif command == "see": | |
hero.see() | |
elif command == "move": | |
hero.move(parameter) | |
elif command == "grab": | |
hero.grab(parameter) | |
elif command == "throw": | |
hero.discardItem(parameter) | |
elif command == "examine": | |
hero.examineItem(parameter) | |
elif command == "quit": | |
sys.exit("Thanks for playing! Hope you enjoyed the game") | |
else: | |
print("Invalid command, type help for list of commands") | |
def main(): | |
sword = Item("Sword", "The magnificent Excaliber, approved by King Arthur", 10) | |
mobile = Item("Mobile", "Use this to call you friends", 5) | |
rock = Item("Rock", "Some random rock", 7) | |
wand = Item("Wand", "This is the magic wand which belonged to someone name Harry Potter", 2) | |
ruby = Item("Ruby", "A bright red gem", 2) | |
whip = Item("Whip", "Indian Jones left this here", 5) | |
ladder = Item("Ladder", "For you to enjoy the view from the top", 8) | |
jetpack = Item("Jetpack", "Maybe this can fly you out of here", 5) | |
cloak = Item("Invisible Cloak", "Another thing that Harry left behind", 3) | |
perfume = Item("Perfume", "If you really can't stand the smell of this place anymore", 4) | |
shield = Item("Shield", "Use this to defend your self from the dragon", 6) | |
one = Room("Spawn Site", "The place where it all began") | |
two = Room("Dungeon", "Rumor has it that there is a hidden treasure over here") | |
three = Room("Labyrinth", "See if you can get your way out of this") | |
four = Room("Chamber of Secrets", "Maybe you can bump into Harry Potter over here") | |
five = Room("Armoury", "Find your self some weapons") | |
one.setExits([two, three]) | |
one.setItems([rock, mobile]) | |
two.setExits([one, four]) | |
two.setItems([ladder, perfume]) | |
three.setExits([one, two, four, five]) | |
three.setItems([wand, ruby]) | |
four.setExits([five, two]) | |
four.setItems([cloak, jetpack]) | |
five.setExits([one, two, three]) | |
five.setItems([sword, whip, shield]) | |
name = input("Can we get your name\n") | |
hero = Character(name, one) | |
print('Hello %s, Welcome to Terminal Adventure' % name) | |
print("In this game you can move from one room to the other until you have energy left") | |
print("Use status to keep track of your energy. Once it turns 0 and if you try to move to a different room then the game will finish") | |
print("Each room has some items and paths to other rooms") | |
print("You can add items to your backpack and throw them in any other rooms") | |
print("Each item has a weight and you can add items until there is capacity in your backpack") | |
print("You can get more information about an item as well") | |
print("Lets get you up and running with the commands. Try typing \'help\' to get a list of all the commands") | |
while(True): | |
user_input = input() | |
input_list = user_input.split(" ") | |
command = input_list[0] | |
parameter = "no parameter" | |
if len(input_list) > 1: | |
parameter = " ".join(input_list[1:]) | |
print() | |
executeCommand(hero, command, parameter) | |
print() | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment