-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKanjin-Engine
299 lines (255 loc) · 10.1 KB
/
Kanjin-Engine
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
from pywebio import start_server
from functions import wait, startGame, query_equip, change_equip, get_instructions, error_message, parse
from functions import current_inventory, current_equipment
from player import *
def game_logic():
name, gender, age, race, job = startGame()
put_text(f"Character Created: {name}, {gender}, {age} years old, {race} {job}")
if __name__ == '__main__':
start_server(game_logic, port=8080)
class Engine(object):
seed = None
def __init__(self, scene_map):
# self.name = "name"
# self.descrip = "descrip"
self.scene_map = scene_map
# gets the game's map from instance "mymap" at bottom of this file
def play(self):
current_scene = self.scene_map.opening_scene()
last_scene = self.scene_map.next_scene('finished')
while current_scene != last_scene:
next_scene_name = current_scene.enter()
current_scene = self.scene_map.next_scene(next_scene_name)
current_scene.enter()
# # note: will throw error if no new scene passed in by next line:
# next_scene_name = current_scene.action()
# # get the name of the next scene from the action() function that
# # runs in the current scene - what it returns
#
# current_scene = self.scene_map.next_scene(next_scene_name)
# # here we use that val returned by current scene to go to
# # the next scene, running function in Map
class Scene(object):
def __init__(self):
self.objects = []
# def enter(self):
# print(self.name) # works
# print(self.descrip) # works
# # this applies to all classes under Scene, but Zed does it differently
def process_command(command, object1):
output = None
if command == "help":
output = get_instructions()
# if command == "scene":
# output = read_room()
elif command == "inventory":
current_inventory()
elif command == "equipment":
response = query_equip()
if response == "view":
output = current_equipment()
elif response == "change":
output = change_equip()
elif command == "examine":
for item_key, item_value in user.inventory.items():
if object1 == item_key.name.lower():
print(item_key)
else:
output = "Unable to examine that."
elif command == "take":
user.add_inventory(object1)
elif command == "loot":
user.loot_object(object1)
elif command == "error":
output = error_message()
elif command == "drop":
user.update_inventory(user.drop_item(object1))
return output
def read_room():
print("Glancing around the room you see:")
for scene, room in Map.scenes.items(): # iterate map scenes
if Engine.seed == scene: # set new Engine seed in each room.enter()
for list_key, list_value in room.available_items.items(): # string, dictionary
for var, count in list_value.items(): # variable/object, integer
available_items = f'{room.available_items[list_key][var]}x {list_key}.'
print(available_items)
if room.available_items[list_key][var] == 0:
del room.available_items[list_key]
# enter() needs to return a map name.
class Tutorial(Scene):
available_items = {
"paper": {paper: 3},
"rock": {rock: 12}
}
lootable_items = {
"chest": {GP1: 5, rock: 1},
"corpse": {GP1: 100}
}
name = "Tutorial Level"
descrip = ("There are certain commands that will be available almost anytime you are able to type,\n"
"such as viewing your inventory, checking your equipped items, and also changing them.\n"
"You can also view your stats including your current and max hp.\n"
"Give it a go starting with 'Check inventory' or 'Check equipment'.\n"
"See what else you can do, then enter 'continue' to move to the next step.")
has_visited = False
current_seed = "tutorial"
def enter(self):
Engine.seed = "tutorial"
if not self.has_visited:
print("===========")
print(self.name)
print("===========")
print(self.descrip)
else:
print("Welcome back to the tutorial.")
self.has_visited = True
action = input(">> ").lower()
while action != "continue": # move scene to parser
if action == "scene":
read_room()
action = input(">> ").lower()
else:
process_command(*parse(action))
action = input(">> ").lower()
else:
if action == "continue":
return "begin"
def action(self):
pass
class Begin(Scene):
available_items = {
"rock": {rock: 10},
"paper": {paper: 13}}
name = "Kanjin - An RPG Text Adventure"
descrip = "Welcome to Kanjin.\nThis game is based off of the D&D SRD ruleset, and will see you, our adventurer, " \
"exploring, adventuring, and plundering.\n\nTo play Kanjin is very simple.\nRead the instructions, " \
"understand the scene, and take control.\nAll directional cues will be given and clear but you " \
"can also use specific commands at almost anytime when you give your input.\n" \
"After character creation, type 'Help' to view a list of commands and explanations.\n"
has_visited = False
current_seed = "begin"
def enter(self):
Engine.seed = "begin"
if not self.has_visited:
print("===========")
print(self.name)
print("===========")
print(self.descrip)
wait()
user.new_char()
else:
print("Welcome back to the beginning")
self.has_visited = True
action = "continue"
while action not in ("continue", "scene", "back"):
process_command(*parse(action))
action = input(">> ").lower()
else:
if action == "continue":
return "cave entrance"
elif action == "back":
return "tutorial"
elif action == "scene":
print(self.descrip)
return self.current_seed
return "Cave entrance"
def action(self):
pass
class CaveEntrance(Scene):
available_items = {}
name = "Cave Entrance"
descrip = "A small broken statue at the mouth of a cave marks the entrance to a dungeon.\n" \
"Beyond the broken statue lies a massive, rugged room, too dark to see into.\n" \
"You make out the silhouette of broken pottery sprawled around.\n" \
"\nBehind you is the forest you traversed through to get here, about 1500m away from the road.\n" \
"To the east, a canyon. Too wide to cross and too deep to see the bottom.\n" \
"On the west is a mountain face. You are not prepared to take that on.\n"
descFalse = "You return to the cave entrance with the road behind you, a canyon to the east, " \
"and a mountain face on the right."
has_visited = False
current_seed = "cave entrance"
def enter(self):
Engine.seed = "cave entrance"
if not self.has_visited:
print("===========")
print(self.name)
print("===========")
print(self.descrip)
else:
print("Welcome back to the cave")
self.has_visited = True
action = input(">> ").lower()
while action not in ("continue", "scene", "back"):
process_command(*parse(action))
action = input(">> ").lower()
else:
if action == "continue":
return "begin"
elif action == "back":
return "begin"
elif action == "scene":
print(self.descrip)
return "Cave entrance"
def action(self):
pass
class A1(Scene):
available_items = {}
name = "Laser Weapon Armory"
descrip = "Shelves and cases line the walls of this room. Weapons of every description " \
"fill the shelves and cases. There is a digital keypad set into the wall."
current_seed = "A1"
def enter(self):
print("===========")
print(self.name)
print("===========")
print(self.descrip)
def action(self):
pass
# class Death(Scene):
#
# def __init__(self):
# self.name = "You have died!"
# self.descrip = "Your spirit leaves swiftly as your body collapses."
#
# quips = [
# "You died. You kinda suck at this.",
# "Your Mom would be proud...if she were smarter.",
# "Such a loser.",
# "I have a small puppy that's better at this.",
# "You're worse than your Dad's jokes."
# ]
#
# def enter(self):
# print(Death.quips[randint(0, len(self.quips) - 1)])
# exit(1)
#
# def action(self):
# pass
# Map tells us where we are and where we can go
# it does not make us move - Engine does that
class Map(object):
scenes = {
'tutorial': Tutorial(),
"begin": Begin(),
'cave entrance': CaveEntrance(),
'A1': A1(),
# 'death': Death(),
# 'finished': Finished()
}
def __init__(self, start_scene_key):
self.start_scene_key = start_scene_key
# above we make a local var named start_scene_key
# start_scene_key remains unchanged throughout the game
@staticmethod
def next_scene(scene_name):
val = Map.scenes.get(scene_name)
# above is how we get value out of the dictionary named scenes
return val
# this function can be called repeatedly in the game,
# unlike opening_scene, which is called only ONCE
def opening_scene(self):
return self.next_scene(self.start_scene_key)
seed = "tutorial" # starts the game
myMap = Map(seed) # instantiate a new Map object w/ one arg
myGame = Engine(myMap) # instantiate a new Engine object w/ one arg
myGame.play() # call function from that Engine instance