- Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpython-tutorial.py
More file actions
Latest commit
96 lines (79 loc) · 2.18 KB
/
Copy pathpython-tutorial.py
File metadata and controls
96 lines (79 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
importrandom
classArea:
inventory= []
def__init__(self, name, inventory):
self.name=name
self.inventory=inventory
defdisplayInventory(self):
i=0
print"You look around, and see:"
foriteminself.inventory:
printstr(i) +": "+item.name
i+=1
classConsumable:
def__init__(self, name):
self.name=name
defconsume(self, player):
player.level+=1
classPotion(Consumable):
defconsume(self, player):
player.health+=1
classPoison(Consumable):
defconsume(self, player):
player.health-=1
classPlayer:
level=1
health=10
area=Area("Test", [Potion("Test Potion"), Poison("Test Poison")])
inventory= []
defpickupItem(self, index):
self.area.inventory[index]
self.inventory.append(self.area.inventory[index])
delself.area.inventory[index]
defuseItem(self, index):
self.inventory[index].consume(self)
delself.inventory[index]
defdisplayInventory(self):
i=0
print"Your inventory:"
foriteminself.inventory:
printstr(i) +": "+item.name
i+=1
defdisplayStatus(self):
print"Level: "+str(self.level)
print"Health: "+str(self.health)
classOutput:
defprintHelp(self):
print"Known commands are:"
print"inventory"
print"pickup <item>"
print"use <item>"
print"quit"
print"help"
player=Player()
output=Output()
while(True):
userInput=raw_input("Please enter a command:")
userInput=userInput.split()
ifuserInput[0] =='inventory'oruserInput[0] =='i':
player.displayInventory()
elifuserInput[0] =='status'oruserInput[0] =='s':
player.displayStatus()
elifuserInput[0] =='pickup'oruserInput[0] =='p':
try:
player.pickupItem(int(userInput[1]))
except:
print"That item doesn't exist!"
elifuserInput[0] =='use'oruserInput[0] =='u':
try:
player.useItem(int(userInput[1]))
except:
print"That item doesn't exist!"
elifuserInput[0] =='look'oruserInput[0] =='l':
player.area.displayInventory()
elifuserInput[0] =='quit'oruserInput[0] =='q':
quit()
elifuserInput[0] =='help'oruserInput[0] =='h':
output.printHelp()
else:
print"Uh oh! I can't find the command '"+userInput[0] +"', please enter another command."