-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrpg_1.py
More file actions
120 lines (101 loc) · 3.82 KB
/
rpg_1.py
File metadata and controls
120 lines (101 loc) · 3.82 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/python3
"""Driving a simple game framework with
a dictionary object | Alta3 Research"""
def showInstructions():
"""Show the game instructions when called"""
#print a main menu and the commands
print('''
==========================================================
RPG Game
==========================================================
Objective: Get to the garden with the key and the potion.
Watch out for the Monster!!!
==========================================================
Commands:
go [direction]
get [item]
==========================================================
''')
def showStatus():
"""determine the current status of the player"""
# print the player's current location
print('---------------------------')
print('You are in the ' + currentRoom)
# print what the player is carrying
print('Inventory:', inventory)
# check if there's an item in the room, if so print it
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 rooms
## A dictionary linking a room to other rooms
## A dictionary linking a room to other rooms
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()
# breaking this while loop means the game is over
while True:
showStatus()
# the player MUST type something in
# otherwise input will keep asking
move = ''
while move == '':
move = input('>')
# normalizing input:
# .lower() makes it lower case, .split() turns it to a list
# therefore, "get golden key" becomes ["get", "golden key"]
move = move.lower().split(" ", 1)
#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]]
# if they aren't allowed to go that way:
else:
print('You can\'t go that way!')
#if they type 'get' first
if move[0] == 'get' :
# make two checks:
# 1. if the current room contains an item
# 2. if the item in the room matches the item the player wishes to get
if "item" in rooms[currentRoom] and move[1] in rooms[currentRoom]['item']:
#add the item to their inventory
inventory.append(move[1])
#display a helpful message
print(move[1] + ' got!')
#delete the item key:value pair from the room's dictionary
del rooms[currentRoom]['item']
# if there's no item in the room or the item doesn't match
else:
#tell them they can't get it
print('Can\'t get ' + move[1] + '!')
## If a player enters 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
## Define how a player can win
if currentRoom == 'Garden' and 'key' in inventory and 'potion' in inventory:
print('You escaped the house with the ultra rare key and magic potion... YOU WIN!')
break