-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
330 lines (284 loc) · 13.5 KB
/
main.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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import json
from os import system as s
class Game:
def __init__(self):
self.locations = {}
self.player = None
self.createLocations()
self.createPlayer()
def createLocations(self):
# Iterates through locations.json, dynamically creates Location objects, adds to Game.locations dictionary #
with open('locations.json') as dataFile:
data = json.load(dataFile)
for index in range(0, len(data["locations"])):
newLocation = Location(id_num = data["locations"][index]["id"], name = data["locations"][index]["name"])
# Add the directions to link certain locations together #
for direction in list(data["locations"][index]["directions"].keys()):
newLocation.directions[direction] = data["locations"][index]["directions"][direction]
# Some locations have items - try/except block scans locations and adds them to Location objects #
try:
itemsToAdd = data["locations"][index]["items"]
with open('items.json') as itemsFile:
itemData = json.load(itemsFile)
for itemIndex in itemsToAdd:
# Create Item object(s) #
newItem = Item(id_num = itemData["items"][itemIndex]["id"], itemName = itemData["items"][itemIndex]["name"], itemDescription = itemData["items"][itemIndex]["description"])
# Some items have events, too. #
try:
events = itemData["items"][itemIndex]["events"]
for event in events:
# 'event' will be an ID that corresponds to an ID in the events.json file #
with open('events.json') as eventsFile:
eventsData = json.load(eventsFile)["events"]
# Add the linked event to the newLocation object #
newEvent = Event(id_num=eventsData[event]["id"], data=eventsData[event]["data"])
newItem.events[event] = newEvent
except:
pass
newLocation.items[newItem.name.lower()] = newItem
except KeyError:
pass
# Some locations have events - try/except block scans locations and adds them to Location objects #
try:
events = data["locations"][index]["events"]
for event in events:
# 'event' will be an ID that corresponds to an ID in the events.json file #
with open('events.json') as eventsFile:
eventsData = json.load(eventsFile)["events"]
# Add the linked event to the newLocation object #
newEvent = Event(id_num=eventsData[event]["id"], data=eventsData[event]["data"])
newLocation.events[event] = newEvent
except:
pass
# Locations can also have people - try/except block adds these too #
try:
people = data["locations"][index]["people"]
for person in people:
with open('people.json') as peopleFile:
peopleData = json.load(peopleFile)["people"]
newPerson = Person(id_num=peopleData[person]["id"], name=peopleData[person]["name"], events=peopleData[person]["events"])
events = peopleData[person]["events"]
with open('events.json') as eventsFile:
eventsData = json.load(eventsFile)["events"]
# Add the linked event to the newLocation object #
newEvent = Event(id_num=eventsData[peopleData[person]["events"][0]]["id"], data=eventsData[peopleData[person]["events"][0]]["data"])
newPerson.events[person] = newEvent
newLocation.people.append(newPerson)
except:
pass
# Add to Game.locations dictionary #
self.locations[index] = newLocation
def createPlayer(self):
# Create a Player object #
self.player = Player()
self.player.location = self.locations[self.player.position]
def gameLoop(self):
while True:
# Beginning of loop - Clear Screen #
s("cls")
# 1 - Update player location #
self.player.location = self.locations[self.player.position]
# 2 - Display HUD #
self.HUD()
# 3 - Check for items #
#if self.player.location.checkForItems():
# print("There are items here!")
# 4 - Check for events at this location #
if self.player.location.checkForEvents():
self.player.location.runEvents()
# Add the HUD again #
s("cls")
self.HUD()
# 5 - User prompt #
self.command(input("\n---> What now? "))
input()
def HUD(self):
print("Current Location:", self.player.location.name,"\n")
# Draw Map #
directionArrows = ["↑", "↓", "←", "→"]
directionDisplay = [" ", " ", " ", " "]
for direction in list(self.player.location.directions.keys()):
if direction == "left":
directionDisplay[0] = directionArrows[2]
elif direction == "right":
directionDisplay[3] = directionArrows[3]
elif direction == "up":
directionDisplay[1] = directionArrows[0]
elif direction == "down":
directionDisplay[2] = directionArrows[2]
print(" ",directionDisplay[1]," ")
print(directionDisplay[0],"*",directionDisplay[3])
print(" ",directionDisplay[2]," ")
def command(self, user):
# Prevent any erroneous capitalisations #
# Break user command into 'chunks' #
user = user.lower().split(" ")
# MOVE #
if user[0] == "move":
if user[1] in list(self.player.location.directions.keys()):
self.player.position = self.player.location.directions[user[1]]
print("Moved ",user[1],".", sep='')
# LOOK #
if user[0] == "look":
self.player.location.displayItems()
self.player.location.displayPeople()
# TAKE #
if user[0] == "take":
# Search for item in location #
keyList = list(self.player.location.items.keys())
for item in keyList:
item = item.lower()
individualElements = item.split(' ')
try:
if user[1] in individualElements:
# Item may be linked to an event - check for this #
if len(self.player.location.items[item].events) > 0:
self.player.location.items[item].runEvents()
# Add to inventory, remove from location #
self.player.inventory[item] = self.player.location.items[item]
del self.player.location.items[item]
print("Took the",self.player.inventory[item].name + ".\n")
print("-"*len(self.player.inventory[item].description) + "----------")
print(self.player.inventory[item].name)
print(self.player.inventory[item].description)
print("-"*len(self.player.inventory[item].description) + "----------\n")
except Exception as e:
if len(user) == 1:
print("Please specify an item.")
else:
print("Can't find that item.")
# TALK TO #
try:
if user[0] == "talk" and user[1] == "to":
for person in range(0, len(self.player.location.people)):
if user[2] in list(self.player.location.people[person].name[person].lower().split(" ")):
for event in self.player.location.people[person].events:
self.player.location.people[person].runEvents()
except IndexError:
pass
# INV / INVENTORY #
if user[0] in ["inv", "inventory"]:
self.player.displayInventory()
class Location:
def __init__(self, id_num, name):
self.id = id_num
self.name = name
self.items = {}
self.events = {}
self.directions = {}
self.people = []
def checkForEvents(self):
if len(self.events) > 0:
return True
else:
return False
def checkForItems(self):
if len(self.items) > 0:
return True
else:
return False
def runEvents(self):
for event in self.events:
eventKeys = list(self.events[event].data.keys())
for singleEvent in eventKeys:
if singleEvent == "speech":
self.events[event].execute(singleEvent)
# Events have completed - remove from events dictionary to avoid it happening multiple times #
self.events.clear()
def displayItems(self):
print("----"*2,"Items","----"*2)
for item in list(self.items.keys()):
print(self.items[item].name)
print(self.items[item].description,"\n")
if not(self.checkForItems()):
print("No items here.")
print("-"*21 + "--")
def displayPeople(self):
print("\n")
print("----"*2,"People","----"*2)
for person in range(0, len(self.people)):
print(self.people[person].name[person])
print("-"*22 + "--")
class Player:
def __init__(self):
self.position = 2
self.location = None
self.inventory = {}
self.quests = []
def displayInventory(self):
for item in self.inventory:
print("-"*len(self.inventory[item].description) + "-")
print(self.inventory[item].name)
print(self.inventory[item].description)
print("-"*len(self.inventory[item].description) + "-")
if len(self.inventory) == 0:
print("You don't have any items.")
class Item:
def __init__(self, id_num, itemName, itemDescription):
self.id = id_num
self.name = itemName
self.description = itemDescription
self.events = {}
def runEvents(self):
for event in self.events:
eventKeys = list(self.events[event].data.keys())
for singleEvent in eventKeys:
if singleEvent == "speech":
self.events[event].execute(singleEvent)
# Events have completed - remove from events dictionary to avoid it happening multiple times #
self.events.clear()
class Event:
def __init__(self, id_num, data):
self.id = id_num
self.data = data
def execute(self, eventType):
if eventType == "speech":
# This is a dialogue event #
with open('speech.json') as speechFile:
speechData = json.load(speechFile)["speech"][self.id]
DialogueEvent = Dialogue(id_num=speechData["id"], characters=speechData["characters"], data=speechData["data"])
elif eventType == "give":
# This is where the player needs to give an item to a person #
print("Begin give event")
with open('give.json') as giveFile:
giveData = json.load(giveFile)["give"][self.data[eventType]]
NewQuest = Quest(id_num=giveData["id"], required_items=giveData["required_items"])
print(NewQuest)
class Dialogue:
def __init__(self, id_num, characters, data):
self.id = id_num
self.characters = characters
self.data = data
self.read()
def read(self):
# The ID key in the data dictionary corresponds to list indices in the self.characters list. #
lineKeys = list(self.data.keys())
for turn in range(0, len(self.data["lines"])):
s("cls")
# The person saying the line #
lengthOfLineCalculation = len(self.data["lines"][turn]) - len(self.characters[self.data["order"][turn]])
lengthOfLine = int(round(lengthOfLineCalculation / 2))
if lengthOfLine*2 > 79:
lengthOfLine = int(round(79/2))-1
print("-"*lengthOfLine + self.characters[self.data["order"][turn]] + "-"*lengthOfLine)
# The line #
print(self.data["lines"][turn], "\n")
offset = round(len(self.characters[self.data["order"][turn]])/4+1)
input(' '*int(round(lengthOfLine+offset)) + "(cont.)")
class Person:
def __init__(self, id_num, name, events):
self.id = id_num
self.name = name
self.events = events
def runEvents(self):
for event in self.events:
for singleEvent in list(event.data.keys()):
event.execute(singleEvent)
class Quest:
def __init__(self, id_num, required_items):
self.id = id_num
self.required_items = []
s("title Some Python Adventure Game")
MainGame = Game()
MainGame.gameLoop()
input()