-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
250 lines (223 loc) · 7.62 KB
/
app.py
File metadata and controls
250 lines (223 loc) · 7.62 KB
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
import random
import dash
import dash_mantine_components as dmc
from dash import Input, Output, State, callback, dcc, html, no_update
app = dash.Dash(
name="Tamadashi",
title="Tamadashi",
update_title=None,
)
server = app.server
app.index_string = """
<!DOCTYPE html>
<html>
<head>
{%metas%}
<title>Tamadashi - Virtual Pet Game</title>
<meta name="title" content="Tamadashi - Virtual Pet Game" />
<meta name="description" content="Take care of your virtual pet Tamadashi! Feed, play, and pet your digital companion." />
<meta property="og:type" content="website" />
<meta property="og:url" content="https://tamadashi.plotly.app/" />
<meta property="og:title" content="Tamadashi - Virtual Pet Game" />
<meta property="og:description" content="Take care of your virtual pet Tamadashi! Feed, play, and pet your digital companion." />
<meta property="og:image" content="https://tamadashi.plotly.app/assets/thumbnail.png" />
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:url" content="https://tamadashi.plotly.app/" />
<meta name="twitter:title" content="Tamadashi - Virtual Pet Game" />
<meta name="twitter:description" content="Take care of your virtual pet Tamadashi! Feed, play, and pet your digital companion." />
<meta name="twitter:image" content="https://tamadashi.plotly.app/assets/thumbnail.png" />
{%favicon%}
{%css%}
</head>
<body>
{%app_entry%}
<footer>
{%config%}
{%scripts%}
{%renderer%}
</footer>
</body>
</html>
"""
# Centered div + layered images with CSS classes
def make_tamadashi_box():
tamadashi_body = html.Img(src="assets/Body.png", className="tamadashi-body")
tamadashi_eyes = html.Img(src="assets/Eyes.png", className="tamadashi-eyes")
tamadashi_mouth = html.Img(src="assets/Mouth.png", className="tamadashi-mouth")
layout = html.Div(
className="tamadashi-container",
children=[
html.Div(
className="tamadashi-box",
id="tamadashi-box",
children=[tamadashi_body, tamadashi_eyes, tamadashi_mouth],
)
],
)
return layout
def make_controls():
return html.Div(
className="controls-container",
children=[
html.Button(
className="controls-button",
children="🍔",
id="feed-button",
title="Feed!",
),
html.Button(
className="controls-button",
children="🎾",
id="play-button",
title="Play!",
),
html.Button(
className="controls-button",
children="🫳",
id="pat-button",
title="Pet!",
),
],
)
def layout():
return dmc.MantineProvider(
html.Div(
className="main-container",
children=[
html.H1(
className="tamadashi-title",
children=[html.Span(x) for x in ("tama", "dash", "i")],
),
make_tamadashi_box(),
make_controls(),
dcc.Interval(id="interval", interval=6000),
dcc.Store(
id="status",
data={"food": 42, "happiness": 42, "energy": 42},
storage_type="memory",
),
html.Div(id="flying-text-container"),
dmc.Affix(
dcc.Link(
dmc.Button("Try Plotly Cloud", className="cloud-button"),
href="https://cloud.plotly.com/",
target="_blank",
),
position={"bottom": 20, "right": 20},
),
],
)
)
@callback(
Output("status", "data", allow_duplicate=True),
Output("feed-button", "disabled", allow_duplicate=True),
Output("play-button", "disabled", allow_duplicate=True),
Output("pat-button", "disabled", allow_duplicate=True),
State("status", "data"),
Input("interval", "n_intervals"),
prevent_initial_call=True,
)
async def update_status(status, n_intervals):
status["food"] -= 1
status["happiness"] -= 1
status["energy"] += 2
return status, False, False, False
@callback(
Output("tamadashi-box", "className"),
Input("status", "data"),
)
async def update_tamadashi_box(status):
classname = "tamadashi-box"
if status["food"] <= 0 or status["happiness"] <= 0 or status["energy"] <= 0:
classname += " dead"
elif status["food"] < 40 or status["happiness"] < 40 or status["energy"] < 40:
classname += " sad"
elif status["food"] > 75 and status["happiness"] > 75 and status["energy"] > 75:
classname += " happy"
return classname
@callback(
Output("status", "data", allow_duplicate=True),
Output("feed-button", "disabled", allow_duplicate=True),
Output("play-button", "disabled", allow_duplicate=True),
Output("pat-button", "disabled", allow_duplicate=True),
Output("flying-text-container", "children"),
State("status", "data"),
Input("feed-button", "n_clicks"),
Input("play-button", "n_clicks"),
Input("pat-button", "n_clicks"),
prevent_initial_call=True,
)
async def interact(status, n_clicks_feed, n_clicks_play, n_clicks_pat):
ctx = dash.callback_context
minmax = lambda x: min(100, max(0, x))
flying_text = None
if ctx.triggered_id == "feed-button":
status["food"] = minmax(status["food"] + 10)
status["energy"] = minmax(status["energy"] + 5)
feed_messages = [
"Yum!",
"Delicious!",
"Nom nom!",
"Tasty!",
"Mmm!",
"Yummy!",
"Scrumptious!",
"Yay food!",
]
flying_text = html.Div(
random.choice(feed_messages),
className="flying-text feed",
key=f"feed-{n_clicks_feed}",
)
feed_disabled = True
play_disabled = no_update
pat_disabled = no_update
elif ctx.triggered_id == "play-button" and status["energy"] > 0:
status["happiness"] = minmax(status["happiness"] + 10)
status["energy"] = minmax(status["energy"] - 10)
status["food"] = minmax(status["food"] - 5)
play_messages = [
"Whee!",
"Fun!",
"Yay!",
"Playtime!",
"Awesome!",
"Exciting!",
"Woo!",
"Amazing!",
]
flying_text = html.Div(
random.choice(play_messages),
className="flying-text play",
key=f"play-{n_clicks_play}",
)
feed_disabled = no_update
play_disabled = True
pat_disabled = no_update
elif ctx.triggered_id == "pat-button":
status["happiness"] = minmax(status["happiness"] + 10)
status["energy"] = minmax(status["energy"] - 5)
status["food"] = minmax(status["food"] - 2)
pat_messages = [
"Aww!",
"Cute!",
"Love!",
"Sweet!",
"Gentle!",
"Kind!",
"Warm!",
"Cozy!",
]
flying_text = html.Div(
random.choice(pat_messages),
className="flying-text pat",
key=f"pat-{n_clicks_pat}",
)
feed_disabled = no_update
play_disabled = no_update
pat_disabled = True
print(status)
return status, feed_disabled, play_disabled, pat_disabled, flying_text
app.layout = layout
if __name__ == "__main__":
app.run(debug=True)