forked from TomJohnH/streamlit-dungeon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstreamlit_app.py
566 lines (439 loc) · 17.9 KB
/
streamlit_app.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
import game_config
import game_js
import game_def
import json
import numpy as np
import pandas as pd
import random
from random import randrange
import streamlit as st
import streamlit.components.v1 as components
import time
# -------------- refrence docs: --------------
# https://www.pythonmorsels.com/making-auto-updating-attribute/
# https://developer.mozilla.org/en-US/docs/Games/Techniques/Tilemaps
# ------------------------------------------------------------
#
# Visual settings and functions
#
# ------------------------------------------------------------
st.set_page_config(
page_title="The Dungeon", page_icon="🗡️", initial_sidebar_state="collapsed"
)
@st.cache_resource
def local_css(file_name):
with open(file_name) as f:
st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)
# ------------------------------------------------------------
#
# Classes
#
# ------------------------------------------------------------
# object constructor for player and monsters
class GameObject:
"""
Base class for game objects, containing common properties and methods
for characters and inanimate objects.
"""
def __init__(self, x, y, file, base_url):
"""
Initialize a new game object instance.
:param x: x-coordinate of the object
:param y: y-coordinate of the object
:param file: filename of the object's image
:param base_url: base URL for the object's image
"""
self.x = x
self.y = y
self.file = base_url + file
@property
def html(self):
"""
Generate the HTML code to display the game object.
:return: string with the HTML code
"""
return (
"<img src='"
+ str(self.file)
+ "' style='grid-column-start: "
+ str(self.x)
+ "; grid-row-start: "
+ str(self.y)
+ ";'>"
)
class Character(GameObject):
"""
Class representing a character in the game, inheriting from GameObject.
"""
def __init__(self, x, y, file, hp, gold, alive):
"""
Initialize a new character instance.
:param x: x-coordinate of the character
:param y: y-coordinate of the character
:param file: filename of the character's image
:param hp: character's health points
:param gold: character's gold count
:param alive: character's alive status (True or False)
"""
base_url = "https://raw.githubusercontent.com/TomJohnH/streamlit-dungeon/main/graphics/other/"
super().__init__(x, y, file, base_url)
self.hp = hp
self.gold = gold
self.alive = alive
class InanimateObject(GameObject):
"""
Class representing an inanimate object in the game, inheriting from GameObject.
"""
def __init__(self, x, y, file, visible):
"""
Initialize a new inanimate object instance.
:param x: x-coordinate of the object
:param y: y-coordinate of the object
:param file: filename of the object's image
:param visible: object's visibility status (True or False)
"""
base_url = "https://raw.githubusercontent.com/TomJohnH/streamlit-dungeon/main/graphics/tileset/"
super().__init__(x, y, file, base_url)
self.visible = visible
# ------------------------------------------------------------
#
# Objects
#
# ------------------------------------------------------------
# ------------------------------------------------------------
#
# Variables and constants
#
# ------------------------------------------------------------
# ---------------- initiat session states ----------------
if "left_clicked" not in st.session_state:
st.session_state["left_clicked"] = False
if "right_clicked" not in st.session_state:
st.session_state["right_clicked"] = False
if "up_clicked" not in st.session_state:
st.session_state["up_clicked"] = False
if "down_clicked" not in st.session_state:
st.session_state["down_clicked"] = False
if "steps" not in st.session_state:
st.session_state["steps"] = 0
if "bubble_text" not in st.session_state:
st.session_state["bubble_text"] = ""
if "cold_start" not in st.session_state:
st.session_state["cold_start"] = True
if "end" not in st.session_state:
st.session_state["end"] = False
if "fly_mode" not in st.session_state:
st.session_state["fly_mode"] = False
# ---------------- links ----------------
# ------------------------------------------------------------
#
# Callbacks
#
# ------------------------------------------------------------
def move_callback(direction):
"""
Args:
direction (str): The direction to move the player in. Possible values are "left", "right", "up", and "down".
Returns:
None
"""
x_offset, y_offset = 0, 0
if direction == "left":
x_offset = -1
elif direction == "right":
x_offset = 1
elif direction == "up":
y_offset = -1
elif direction == "down":
y_offset = 1
if game_def.character_can_move(
st.session_state["level"],
game_config.tileset_movable,
st.session_state["player"].y + y_offset,
st.session_state["player"].x + x_offset,
):
st.session_state["player"].x += x_offset
st.session_state["player"].y += y_offset
st.session_state[direction + "_clicked"] = True
st.session_state["steps"] += 1
for monster in st.session_state["monsters"]:
game_def.move_to_player(st.session_state["player"], monster)
game_def.encounter(st.session_state["player"], monster)
for chest in st.session_state["chests"]:
game_def.treasures(st.session_state["player"], chest)
def left_callback():
move_callback("left")
def right_callback():
move_callback("right")
def up_callback():
move_callback("up")
def down_callback():
move_callback("down")
# ------------------------------------------------------------
#
# Functions
#
# ------------------------------------------------------------
# ---------------- data fetch ----------------
@st.cache_data
def fetch_data(level_name):
df = pd.read_csv(level_name, sep=",", header=None)
return df
# ---------------- game restart ----------------
def restart_game():
for key in st.session_state.keys():
del st.session_state[key]
st.rerun()
st.rerun()
# ------------------------------------------------------------
#
# Graphics engine
#
# ------------------------------------------------------------
# ---------------- CSS ----------------
local_css("style.css")
# ------------------------------------------------------------
#
# Game enigne - frontend html creator
#
# ------------------------------------------------------------
# --------------- level config ------------------------
current_level_name = "level2"
if "level_data" not in st.session_state:
level_config = game_config.level_config
st.session_state.level_data = json.loads(level_config)
# ---------------- INTERACTIVE LEVEL ELEMENTS ----------------
# ---------------- creating player html ----------------
if "player" not in st.session_state:
temp = st.session_state.level_data["players_stats"]
temp_xy = st.session_state.level_data[current_level_name]["player_xy"]
st.session_state["player"] = Character(
x=temp_xy["x"],
y=temp_xy["y"],
file=temp["file"],
hp=temp["hp"],
gold=temp["gold"],
alive=temp["alive"],
)
player = f"""
<img src="{game_config.player_img}" id="player" class="player" style="grid-column-start: {st.session_state["player"].x}; grid-row-start: {st.session_state["player"].y};">"""
# ---------------- creating monsters html ----------------
# we are constructing monsters in iteractions based on level configuration
if "monsters" not in st.session_state:
st.session_state["monsters"] = []
for monsters_name in st.session_state.level_data[current_level_name]["monsters"]:
temp = st.session_state.level_data[current_level_name]["monsters"][
monsters_name
]
st.session_state["monsters"].append(
Character(
x=temp["x"],
y=temp["y"],
file=temp["file"],
hp=temp["hp"],
gold=temp["gold"],
alive=temp["alive"],
)
)
# we are creating monsters html
monsters = game_def.generate_monsters_html(st.session_state["monsters"])
# ---------------- chests ----------------
# chests are interactive therfore we are creating objects
if "chests" not in st.session_state:
st.session_state["chests"] = []
for chests_name in st.session_state.level_data[current_level_name]["chests"]:
temp = st.session_state.level_data[current_level_name]["chests"][chests_name]
st.session_state["chests"].append(
InanimateObject(
x=temp["x"],
y=temp["y"],
file=temp["file"],
visible=temp["visible"],
)
)
chests = game_def.generate_chests_html(st.session_state["chests"])
# ---------------- NON-INTERACTIVE LEVEL ELEMENTS ----------------
# ---------------- boxes ----------------
if "boxes" not in st.session_state:
st.session_state["boxes"] = game_def.additional_layers_html(
current_level_name, "boxes"
)
boxes = st.session_state["boxes"]
# ---------------- voids ----------------
if "voids" not in st.session_state:
st.session_state["voids"] = game_def.additional_layers_html(
current_level_name, "voids", "xyz"
)
voids = st.session_state["voids"]
# ---------------- troches ----------------
if "torches" not in st.session_state:
st.session_state["torches"] = game_def.additional_layers_html(
current_level_name, "torches"
)
torches = st.session_state["torches"]
# ---------------- creating visual layers textboxes ----------------
text_boxes_html = game_def.get_text_boxes(
st.session_state["player"].x, st.session_state["player"].y, current_level_name
)
# ------------------------------------------------------------
#
# fetching level data from csv
#
# ------------------------------------------------------------
# fetch level with certain number
df = fetch_data(st.session_state.level_data[current_level_name]["level_csv"])
if "level" not in st.session_state: # or st.session_state["level_change"]:
st.session_state["level"] = df.values
# ---------------- END CONDITION ----------------
if (
st.session_state["player"].x
== st.session_state.level_data[current_level_name]["exit"]["x"]
and st.session_state["player"].y
== st.session_state.level_data[current_level_name]["exit"]["y"]
):
st.session_state["end"] = True
# ------------------------------------------------------------
#
# Game enigne - frontend html renderer
#
# ------------------------------------------------------------
tab1, tab2 = st.tabs(["intro", "start game"])
# ----------------- game start --------
with tab1:
st.subheader("| Intro")
col1, col2 = st.columns(2, gap="small")
with col1:
# main_image
st.image("graphics/other/dungeon_crawler.png")
st.caption(
"The Dungeon: a streamlit dungeon crawler game", unsafe_allow_html=True
)
with col2:
intro_text = """
Explore the depths of an ancient dungeon in the first streamlit-based dungeon crawler game!
Navigate through dangerous traps, defeat fearsome monsters and uncover the secrets of the DuNgeOn.
With intuitive controls and beautiful graphics, this game will keep you entertained for hours.
Experience the thrill of adventure as you progress through levels and uncover powerful treasures.
Join the adventure today and become the hero of the dungeon!
"""
st.write(f'<p style="color:#9c9d9f">{intro_text}</p>', unsafe_allow_html=True)
audio_file = open("audio/intro.mp3", "rb")
audio_bytes = audio_file.read()
st.audio(audio_bytes, format="audio/mpeg")
st.subheader("| Game start")
st.write(
'<p style="color:#9c9d9f">To start the game go to the "start game" tab. Please be sure to switch to <b>dark mode</b> or the custom theme. The Dungeon is meant to be played in the dark! </p>',
unsafe_allow_html=True,
)
st.subheader("| Controls")
st.write(
'<p style="color:#9c9d9f">Desktop: please use keyboard arrows | Mobile (Android, Chrome): please use on-screen buttons | iOS: unfortunately, the auto-scrolling feature does not work yet for iOS.</p>',
unsafe_allow_html=True,
)
st.subheader("| Github")
st.write(
'<p style="color:#9c9d9f">Create your own dungeon! Visit <a href="https://github.com/TomJohnH/streamlit-dungeon">GitHub</a>. Edit your levels with <a href="https://dungeon-editor.streamlit.app/">The Dungeon editor</a>.</p><br>',
unsafe_allow_html=True,
)
with tab2:
####################################################
#
# THIS PART RENDERS MAIN SCREEN
#
####################################################
html = game_def.level_renderer(
st.session_state["level"],
player + monsters + boxes + voids + torches + text_boxes_html + chests,
)
display_html = st.empty()
if not st.session_state["end"]:
if st.session_state["player"].alive:
display_html = st.markdown(html, unsafe_allow_html=True)
else:
display_html = st.markdown(
"💀 The monster was more powerful than expected, resulting in your defeat in battle. Your journey has come to an unexpected end. To continue playing, please restart the app.<br><br>",
unsafe_allow_html=True,
)
if st.button("restart"):
restart_game()
if st.session_state["end"] == True:
display_html = st.markdown(
"Thank you for playing the demo of The Dungeon. More content coming soom!",
unsafe_allow_html=True,
)
st.markdown(
f"""
<div><br>
<a href="https://www.buymeacoffee.com/tomjohn" style="color: grey; text-decoration:none;">
<div style="justify-content: center;margin:0px; border:solid 2px;background-color: #0e1117; ;border-radius:10px; border-color:#21212f; width: fit-content;padding:0.425rem">
<img src="https://raw.githubusercontent.com/TomJohnH/streamlit-game/main/images/coffe.png" style="max-width:20px;margin-right:10px;">
Buy me a coffee</a></div></div>""",
unsafe_allow_html=True,
)
st.button("L", on_click=left_callback, key="L")
st.button("R", on_click=right_callback, key="R")
st.button("U", on_click=up_callback, key="U")
st.button("D", on_click=down_callback, key="D")
# ------------------------------------------------------------
#
# Game enigne - sidebar for backup input
#
# ------------------------------------------------------------
# ------------ sidebar for backup input ---------------------------
with st.sidebar:
st.write("Use keyboard arrows or buttons below")
st.markdown("<br>", unsafe_allow_html=True)
left_col, middle_col, right_col = st.columns([1, 1, 1])
with middle_col:
st.button("UP", on_click=up_callback, key="UP", use_container_width=True)
st.markdown("<br>", unsafe_allow_html=True)
left_col, middle_col, right_col = st.columns([1, 1, 1])
with left_col:
st.button(
"LEFT", on_click=left_callback, key="LEFT", use_container_width=True
)
with right_col:
st.button(
"RIGHT", on_click=right_callback, key="RIGHT", use_container_width=True
)
st.markdown("<br>", unsafe_allow_html=True)
left_col, middle_col, right_col = st.columns([1, 1, 1])
with middle_col:
st.button(
"DOWN", on_click=down_callback, key="DOWN", use_container_width=True
)
st.markdown("<br>", unsafe_allow_html=True)
dev_options = st.checkbox("Developer options")
god_mode = st.checkbox("God mode")
fly_mode = st.checkbox("Fly mode")
st.markdown(
"<br>Check the level editor<br> [The Dungeon Level Editor](https://dungeon-editor.streamlit.app/)",
unsafe_allow_html=True,
)
# ------------------------------------------------------------
#
# Game enigne - console div
#
# ------------------------------------------------------------
st.markdown(
f"""
<div class="bpad" id="bpad">HP: {st.session_state["player"].hp}/20 | Gold: {st.session_state["player"].gold} | Exp: 0 </div>""",
unsafe_allow_html=True,
)
# ------------------------------------------------------------
#
# Game enigne - JS trickery
#
# ------------------------------------------------------------
components.html(
game_js.js_script_optimized,
height=0,
width=0,
)
if dev_options:
st.caption("Player x: " + str(st.session_state["player"].x))
st.caption("Player y: " + str(st.session_state["player"].y))
if god_mode:
st.session_state["player"].hp = 999
if fly_mode:
st.session_state["fly_mode"] = True