forked from TomJohnH/streamlit-game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
streamlit_app.py
171 lines (137 loc) · 5.03 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
import streamlit as st
import streamlit.components.v1 as components
from streamlit_extras.stoggle import stoggle
from streamlit_extras.metric_cards import style_metric_cards
import time
import random
import game_scenes
# additional components from https://extras.streamlit.app/
# -------------- app config ---------------
st.set_page_config(page_title="StreamlitLand Adventure RPG", page_icon="🐲")
# define external css
def local_css(file_name):
with open(file_name) as f:
st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)
###############################################
#
#
# VARIABLES DEFINITION
#
#
################################################
# variable responsible for checking if player provided his name and game can be started
start = False
# set session states
# this is streamlit specific. For more contex please check streamlit documenation
if "health" not in st.session_state:
st.session_state["health"] = 100
if "mana" not in st.session_state:
st.session_state["mana"] = 80
if "gold" not in st.session_state:
st.session_state["gold"] = 5
if "place" not in st.session_state:
st.session_state["place"] = "introScene"
if "sheep_anger" not in st.session_state:
st.session_state["sheep_anger"] = 0
if "sword" not in st.session_state:
st.session_state["sword"] = 0
if "dragon_alive" not in st.session_state:
st.session_state["dragon_alive"] = 1
if "dragon_hp" not in st.session_state:
st.session_state["dragon_hp"] = 20
if "temp" not in st.session_state:
st.session_state["temp"] = ""
if "counter" not in st.session_state:
st.session_state["counter"] = 0
if "scenes_counter" not in st.session_state:
st.session_state["scenes_counter"] = {
"intro_counter": 0,
"cave_counter": 0,
"trip_counter": 0,
"elf_counter": 0,
}
###############################################
#
#
# GAME ENGINE
#
#
################################################
# ---------------- CSS ----------------
local_css("style.css")
# ----------------- game start --------
welcome = st.empty()
welcome.title("Welcome to StreamlitLand, adventurer!")
# hero base statistics
player_name_container = st.empty()
player_name_container.text_input(
"State your name and hit enter to start the game", key="player_name"
)
main_text_container = st.empty()
main_text_container.caption("Create your own adventure visit [GitHub](https://github.com/TomJohnH/streamlit-dungeon)")
if st.session_state.player_name != "":
player_name_container.empty()
main_text_container.empty()
start = True
# START THE GAME
if start:
# delete welcome
welcome.empty()
if st.session_state.place == "introScene":
game_scenes.introScene()
elif st.session_state.place == "sheepScene":
game_scenes.sheepScene()
elif st.session_state.place == "southpathScene":
game_scenes.southpathScene()
elif st.session_state.place == "elfScene":
game_scenes.elfScene()
elif st.session_state.place == "caveScene":
game_scenes.caveScene()
elif st.session_state.place == "poScene":
game_scenes.poScene()
elif st.session_state.place == "dragonScene":
game_scenes.dragonScene()
elif st.session_state.place == "libraryScene":
game_scenes.libraryScene()
# player stats
col1, col2, col3 = st.columns(3)
col1.metric(label="Health", value=st.session_state.health, delta=0)
col2.metric(label="Mana", value=st.session_state.mana, delta=0)
col3.metric(label="Gold", value=st.session_state.gold, delta=0)
style_metric_cards(
background_color="#black", border_color="#21212f", border_left_color="#21212f"
)
if st.session_state["sword"] == 1:
st.write("🗡️ sword equipped")
# this part of the code focuses input on text window
# please note that counter is required - for streamlit specific it does not work without it
components.html(
f"""
<div>some hidden container</div>
<p>{st.session_state.counter}</p>
<script>
var input = window.parent.document.querySelectorAll("input[type=text]");
for (var i = 0; i < input.length; ++i) {{
input[i].focus();
}}
</script>
""",
height=0,
)
hide_streamlit_style = """
<style>
footer {visibility: hidden;}
</style>
"""
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
# MainMenu {visibility: hidden;}
# ------------ footer ---------------------------
st.markdown(
f"""
<div class="bpad" id="bpad">
<a href="https://www.buymeacoffee.com/tomjohn" style="color: grey; text-decoration:none;">
<div class="coffee_btn" >
<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,
)