-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpython-tutorial.py
More file actions
96 lines (79 loc) · 2.18 KB
/
python-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
import random
class Area:
inventory = []
def __init__(self, name, inventory):
self.name = name
self.inventory = inventory
def displayInventory(self):
i = 0
print "You look around, and see:"
for item in self.inventory:
print str(i) + ": " + item.name
i += 1
class Consumable:
def __init__(self, name):
self.name = name
def consume(self, player):
player.level += 1
class Potion(Consumable):
def consume(self, player):
player.health += 1
class Poison(Consumable):
def consume(self, player):
player.health -= 1
class Player:
level = 1
health = 10
area = Area("Test", [Potion("Test Potion"), Poison("Test Poison")])
inventory = []
def pickupItem(self, index):
self.area.inventory[index]
self.inventory.append(self.area.inventory[index])
del self.area.inventory[index]
def useItem(self, index):
self.inventory[index].consume(self)
del self.inventory[index]
def displayInventory(self):
i = 0
print "Your inventory:"
for item in self.inventory:
print str(i) + ": " + item.name
i += 1
def displayStatus(self):
print "Level: " + str(self.level)
print "Health: " + str(self.health)
class Output:
def printHelp(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()
if userInput[0] == 'inventory' or userInput[0] == 'i':
player.displayInventory()
elif userInput[0] == 'status' or userInput[0] == 's':
player.displayStatus()
elif userInput[0] == 'pickup' or userInput[0] == 'p':
try:
player.pickupItem(int(userInput[1]))
except:
print "That item doesn't exist!"
elif userInput[0] == 'use' or userInput[0] == 'u':
try:
player.useItem(int(userInput[1]))
except:
print "That item doesn't exist!"
elif userInput[0] == 'look' or userInput[0] == 'l':
player.area.displayInventory()
elif userInput[0] == 'quit' or userInput[0] == 'q':
quit()
elif userInput[0] == 'help' or userInput[0] == 'h':
output.printHelp()
else:
print "Uh oh! I can't find the command '" + userInput[0] + "', please enter another command."