-
Notifications
You must be signed in to change notification settings - Fork 0
/
6 oct.py
105 lines (83 loc) · 2.86 KB
/
6 oct.py
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
97
98
99
100
101
102
103
104
105
#!/bin/python3
def showInstructions():
#print a main menu and the commands
print('''
RPG Game
========
Get to the Garden with a key and a potion
Avoid the monsters!
Commands:
go [direction]
get [item]
''')
def showStatus():
#print the player's current status
print('---------------------------')
print('You are in the ' + currentRoom)
#print the current inventory
print("Inventory : " + str(inventory))
#print an item if there is one
if "item" in rooms[currentRoom]:
print('You see a ' + rooms[currentRoom]['item'])
print("---------------------------")
#an inventory, which is initially empty
inventory = []
#a dictionary linking a room to other room positions
rooms = {
'Hall' : { 'south' : 'Kitchen',
'east' : 'Dining Room',
'item' : 'key'
},
'Kitchen' : { 'north' : 'Hall',
'item' : 'monster'
},
'Dining Room' : { 'west' : 'Hall',
'south' : 'Garden',
'item' : 'potion'
},
'Garden' : { 'north' : 'Dining Room' }
}
#start the player in the Hall
currentRoom = 'Hall'
showInstructions()
#loop forever
while True:
showStatus()
#get the player's next 'move'
#.split() breaks it up into an list array
#eg typing 'go east' would give the list:
#['go','east']
move = ''
while move == '':
move = input('>')
move = move.lower().split()
#if they type 'go' first
if move[0] == 'go':
#check that they are allowed wherever they want to go
if move[1] in rooms[currentRoom]:
#set the current room to the new room
currentRoom = rooms[currentRoom][move[1]]
#there is no door (link) to the new room
else:
print('You can\'t go that way!')
#if they type 'get' first
if move[0] == 'get' :
#if the room contains an item, and the item is the one they want to get
if 'item' in rooms[currentRoom] and move[1] in rooms[currentRoom]['item']:
#add the item to their inventory
inventory += [move[1]]
#display a helpful message
print(move[1] + ' got!')
#delete the item from the room
del rooms[currentRoom]['item']
#otherwise, if the item isn't there to get
else:
#tell them they can't get it
print('Can\'t get ' + move[1] + '!')
# player loses if they enter a room with a monster
if 'item' in rooms[currentRoom] and 'monster' in rooms[currentRoom]['item']:
print('A monster has got you... GAME OVER!')
break
# player wins if they get to the garden with a key and a shield
if currentRoom == 'Garden' and 'key' in inventory and 'potion' in inventory:
print('You escaped the house... YOU WIN!')