-
Notifications
You must be signed in to change notification settings - Fork 0
/
RPG-game
205 lines (176 loc) · 6.55 KB
/
RPG-game
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import json
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!
You are getting tired; each time you move you lose one health point.
Commands:
go [north | south | east | west]
get [item]
""")
def showStatus():
# Print the player's current status
print("---------------------------")
print(name + " is in the " + currentRoom)
print("Health : " + str(health))
# Print the current inventory
print("Inventory : " + str(inventory))
# Print an item if there is one
if len(rooms[currentRoom]["item"])>0:
print("You see a " + str(rooms[currentRoom]["item"]))
print("---------------------------")
#-# CODE WILL BE ADDED HERE IN THE NEXT STEP #-#
# Load data from the file
newGame = input("Do you want to start a new game? Y/N >").lower()
if newGame == "y":
# Set up a new game
name = None
health = 5
currentRoom = "Hall"
inventory = []
gamedata = {
"playername": name,
"playerhealth": health,
"playercurrentroom": currentRoom,
"inventory": []
}
with open("gamedata.json","w") as f:
json.dump(gamedata, f)
# 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",
"item" : []
}
}
with open("rooms.json","w") as f:
json.dump(rooms, f)
else:
try:
print("Retrieving player details")
with open("gamedata.json","r") as f:
gamedata = json.load(f)
name = gamedata["playername"]
health = gamedata["playerhealth"]
currentRoom = gamedata["playercurrentroom"]
inventory = gamedata["inventory"]
with open("rooms.json","r") as f:
rooms = json.load(f)
except FileNotFoundError:
print("No previous game found. Starting a new game.")
# Set up a new game
name = None
health = 5
currentRoom = "Hall"
inventory = []
gamedata = {
"playername": name,
"playerhealth": health,
"playercurrentroom": currentRoom,
"inventory": inventory
}
with open("gamedata.json","w") as f:
json.dump(gamedata, f)
# 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",
"item" : []
}
}
with open("rooms.json","w") as f:
json.dump(rooms, f)
# Ask the player their name
if name is None:
name = input("What is your name, Adventurer? ")
gamedata.update({"playername": name})
with open("gamedata.json","w") as f:
json.dump(gamedata, f)
showInstructions()
# Loop forever
while True:
showStatus()
# Get the player's next "move"
# .split() breaks it up into an list array
# e.g. 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":
health = health - 1
gamedata.update({"playerhealth": health})
# 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]]
gamedata.update({"playercurrentroom": currentRoom})
# or, if 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 move[1] in rooms[currentRoom]["item"]:
# move item from the room into the inventory
inventory.append(rooms[currentRoom]["item"].pop(rooms[currentRoom]["item"].index(move[1])))
gamedata.update({"inventory": inventory})
# Display a helpful message
print(move[1] + " collected!")
# Delete the item from the room
#del rooms[currentRoom]["item"]
#rooms.update({currentRoom:
# Otherwise, if the item isn't there to get
else:
# Tell them they can't get it
print("Can't get " + move[1] + "!")
if move[0] == "drop":
#check to see if they hold the thing they want to drop
if move[1] in inventory:
#move item from inventory into currentRoom
rooms[currentRoom]["item"].append(inventory.pop(inventory.index(move[1])))
gamedata.update({"inventory": inventory})
else:
print("You're not holding " + str(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
if health == 0:
print("You collapse from exhaustion ... GAME OVER!")
break
# Player wins if they get to the garden with a key and a potion
if currentRoom == "Garden" and "key" in inventory and "potion" in inventory:
print("You escaped the house ... YOU WIN!")
break
#-# CODE WILL BE ADDED HERE IN THE NEXT STEP #-#
# Save game data to the file
with open("gamedata.json","w") as f:
json.dump(gamedata, f)
with open("rooms.json","w") as f:
json.dump(rooms, f)