Last active
December 31, 2015 02:49
-
-
Save bitoffdev/7923140 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class game: | |
def __init__(self): | |
#Set up variables | |
self.items = {"map":[-1,-1], "knife":[2,0], "scissors":[2,0]} | |
self.coords = [0,0] | |
self.locations = [ | |
["You are in a large open field. There is a house to the south and the field extends to the east.", | |
"You are in a large open field. The field extends to the west and a dense forest surrounds you.", | |
"", | |
"You are surrounded by trees. There appears to be more light to the east.", | |
"You are in a small clearing."], | |
["You are standing outside a small house. The main door in just to the south of you.", | |
"", | |
"You are in a dark section of the woods. To the south the light is brighter.", | |
"You seem to be lost in the woods."], | |
["You are in the kitchen of the small house. There appears to be an open to the north and a door to the east.", | |
"You are in the backyard of a small house. There is a door west. To the east a path goes into the woods.", | |
"You are in the beginning of the woods. To the west a house can be seen in the distance.", | |
""] | |
] | |
def mainLoop(self): | |
self.typeOutput(self.locations[self.coords[0]][self.coords[1]], 2.0) | |
if len(self.itemsAtLocation(self.coords))>0: | |
print "I see %s." %(", ".join(self.itemsAtLocation(self.coords))) | |
decision = raw_input("> ") | |
if decision == "quit": | |
print "Goodbye!" | |
elif decision == "inventory": | |
print self.itemsAtLocation([-1,-1]) | |
elif decision == "help" or decision == "?": | |
print "This is an adventure game. Type 'n', 'e', 's', or 'w' to move or type 'quit' to exit the program." | |
elif any(direct == decision for direct in ["n", "s", "e", "w"]): | |
self.move(decision) | |
else: | |
print "I don't know what you mean. Type 'help' or '?' for help." | |
#Loop if not quitting | |
if decision != "quit": | |
self.mainLoop() | |
def move(self, direction): | |
if direction == "n" and self.locationInArray([self.coords[0]-1, self.coords[1]], self.locations): | |
self.coords[0] -= 1 | |
elif direction == "s" and self.locationInArray([self.coords[0]+1, self.coords[1]], self.locations): | |
self.coords[0] += 1 | |
elif direction == "e" and self.locationInArray([self.coords[0], self.coords[1]+1], self.locations): | |
self.coords[1]+=1 | |
elif direction == "w" and self.locationInArray([self.coords[0], self.coords[1]-1], self.locations): | |
self.coords[1]-=1 | |
else: | |
print "You can't go that way" | |
def locationInArray(self, coordinates, array): | |
#Check if 1st coordinate exists | |
if 0<=coordinates[0]<len(array): | |
#Check if 2nd coordinate exists | |
if 0<=coordinates[1]<len(array[coordinates[0]]): | |
#If element exists at coordinates, make sure the element is not blank | |
if array[coordinates[0]][coordinates[1]] != "": | |
return True | |
#If location doesn't exist then return false | |
return False | |
def itemsAtLocation(self, coordinates): | |
temparr = [] | |
for key in self.items.keys(): | |
if self.items[key] == coordinates: | |
temparr.append(key) | |
return temparr | |
def typeOutput(self, text, time): | |
speed = float(time)/len(text) | |
from time import sleep | |
from sys import stdout | |
for char in text: | |
stdout.write(char) | |
sleep(speed) | |
prog = game() | |
prog.mainLoop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment