-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathgame_def.py
370 lines (279 loc) · 12.1 KB
/
game_def.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
import game_config
import streamlit as st
from random import randrange
# ------------------------------------------------------------
#
# MOVEMENT FUNCTIONS
#
# ------------------------------------------------------------
def character_can_move(level_matrix, tileset_movable, x, y):
"""
This function checks if the move is "legal" for a character in a given level matrix.
Args:
level_matrix (numpy.ndarray): A 2D array representing the level grid.
tileset_movable (list): A dictionary of tileset names and booleans representing the movability of each tile type in the level.
x (int): The x-coordinate of the character's desired position.
y (int): The y-coordinate of the character's desired position.
Returns:
bool: True if the character can move to the desired position, False otherwise.
"""
is_movable = tileset_movable[level_matrix[x - 1, y - 1]]
return is_movable or st.session_state["fly_mode"]
def squared_distance(x1, y1, x2, y2):
"""
Calculate the squared distance between two points.
Args:
x1: The x-coordinate of the first point.
y1: The y-coordinate of the first point.
x2: The x-coordinate of the second point.
y2: The y-coordinate of the second point.
Returns:
The squared distance between the two points.
"""
return (x1 - x2) ** 2 + (y1 - y2) ** 2
def distance_from_player(player, movable_object):
"""
Calculate the squared distances between a player and a movable object
for current position and four possible moves (left, right, up, down).
Args:
player: The player object with x and y attributes.
movable_object: The movable object with x and y attributes.
Returns:
A tuple with the squared distances for current position, left, right, up, and down moves.
"""
px, py = player.x, player.y
mx, my = movable_object.x, movable_object.y
distance = squared_distance(px, py, mx, my)
distance_l = squared_distance(px + 1, py, mx, my)
distance_r = squared_distance(px - 1, py, mx, my)
distance_u = squared_distance(px, py + 1, mx, my)
distance_d = squared_distance(px, py - 1, mx, my)
return distance, distance_l, distance_r, distance_u, distance_d
# ------------------------------------------------------------
#
# move to player modularization
#
# ------------------------------------------------------------
def get_move_index(player, movable_object):
"""
Get the index of the move with the shortest distance to the player.
Args:
player (object): The player object containing its x and y coordinates.
movable_object (object): The movable object containing its x and y coordinates.
Returns:
int: The index of the move with the shortest distance to the player.
"""
distances = distance_from_player(player, movable_object)
return distances.index(min(distances))
def move_object(movable_object, direction, distance):
"""
Move the object in the given direction by the given distance.
Args:
movable_object (object): The object to be moved containing its x and y coordinates.
direction (str): The direction to move the object in ("left", "right", "up", or "down").
distance (int): The distance to move the object.
"""
if direction == "left":
movable_object.x -= distance
elif direction == "right":
movable_object.x += distance
elif direction == "up":
movable_object.y -= distance
elif direction == "down":
movable_object.y += distance
def is_valid_move(movable_object, direction):
"""
Check if a move in the given direction is valid for the movable_object.
Args:
movable_object (object): The object to be moved containing its x and y coordinates.
direction (str): The direction to check the move in ("left", "right", "up", or "down").
Returns:
bool: True if the move is valid, False otherwise.
"""
new_x = movable_object.x
new_y = movable_object.y
if direction == "left":
new_x -= 1
elif direction == "right":
new_x += 1
elif direction == "up":
new_y -= 1
elif direction == "down":
new_y += 1
return character_can_move(st.session_state["level"], game_config.tileset_movable, new_y, new_x)
def move_to_player(player, movable_object):
"""
Move the movable_object towards the player or perform a random move based on a random probability.
Args:
player (object): The player object containing its x and y coordinates.
movable_object (object): The movable object containing its x and y coordinates.
"""
selected_move_index = get_move_index(player, movable_object)
directions = {1: "left", 2: "right", 3: "up", 4: "down"}
if randrange(10) < 5:
direction = directions.get(selected_move_index)
if direction and is_valid_move(movable_object, direction):
move_object(movable_object, direction, 1)
else:
random_move(movable_object)
def random_move(movable_object):
"""
Move the movable_object in a random direction.
Args:
movable_object (object): The movable object containing its x and y coordinates.
"""
def random_direction():
return ["right", "left", "down", "up"][randrange(4)]
direction = random_direction()
directions = {
"right": (movable_object.y, movable_object.x + 1),
"left": (movable_object.y, movable_object.x - 1),
"down": (movable_object.y + 1, movable_object.x),
"up": (movable_object.y - 1, movable_object.x),
}
new_y, new_x = directions[direction]
if character_can_move(st.session_state["level"], game_config.tileset_movable, new_y, new_x):
movable_object.y, movable_object.x = new_y, new_x
# ------------------------------------------------------------
#
# INTERACTION FUNCTIONS
#
# ------------------------------------------------------------
def encounter(player, enemy):
# if you encounter an enemy and enemy is alive
# you will lose health but enemy will die
# player = st.session_state["player"]
if player.x == enemy.x and player.y == enemy.y and enemy.alive == True:
damage = randrange(30)
st.session_state["bubble_text"] = create_text_bubble_html(
"OMG -" + str(damage) + "hp",
player.x,
player.y - 1,
)
player.hp = player.hp - damage
enemy.alive = False
if player.hp <= 0:
player.alive = False
def treasures(player, treasure):
# if you encounter treasure you will get gold
if player.x == treasure.x and player.y == treasure.y and treasure.visible:
gold = randrange(20)
st.session_state["bubble_text"] = create_text_bubble_html(
"yeah! +" + str(gold) + " g",
player.x,
player.y - 1,
)
treasure.visible = False
player.gold = player.gold + gold
# ------------------------------------------------------------
#
# RENDERING FUNCTIONS
#
# ------------------------------------------------------------
def level_renderer(df, game_objects):
"""
The heart of graphical engine, More optimized version.
Generates the HTML for rendering a game level based on a dataframe and game objects.
:param df: A dataframe representing the game level grid, with each cell containing an index for the tileset.
:param game_objects: A string containing the HTML for game objects to be added to the level.
:return: A string with the generated HTML for the game level.
"""
def generate_tile_html(tile, col, row):
return f'<img src="{game_config.tileset[tile]}" style="grid-column-start: {col}; grid-row-start: {row};">'
level_rows = [
"".join(generate_tile_html(tile, j + 1, i + 1) for j, tile in enumerate(row))
for i, row in enumerate(df)
]
level_html = f'<div class="container"><div class="gamegrid">{" ".join(level_rows)}{game_objects}</div></div>'
return level_html
def tile_html(src, x, y, z):
# this little function provides html for additional layers
return f"""<img src="{src}" style="grid-column-start: {x}; grid-row-start: {y}; grid-column-end:{z}">"""
def additional_layers_html(level_name, layer_name, coordinates="xy"):
"""
Generate HTML for additional layer elements such as torches, boxes, and voids.
Args:
level_name (str): The name of the level for which the additional layers are to be generated.
layer_name (str): The name of the layer within the level for which the additional layers are to be generated.
coordinates (str, optional): The coordinate system to be used. Default is 'xy', which sets z = x.
If set to 'xyz', the z-coordinate from the layer data will be used.
Returns:
str: The generated HTML string for the specified additional layer elements.
"""
name = ""
for layer_item in st.session_state.level_data[level_name][layer_name]:
temp = st.session_state.level_data[level_name][layer_name][layer_item]
# Set z-coordinate based on input
z = temp["z"] if coordinates == "xyz" else temp["x"]
name += tile_html(
src=game_config.tileset[temp["text"]],
x=temp["x"],
y=temp["y"],
z=z,
)
return name
# ------------------------------------------------------------
#
# game objects functions
#
# ------------------------------------------------------------
def generate_monsters_html(monsters_list):
"""Generates HTML for monsters.
Args:
monsters_state (list): A list of monster objects from the session state
Returns:
str: The generated HTML string.
"""
# Initialize an empty string for HTML content
html_content = ""
# Iterate through the list of monsters and append their HTML content if they are alive
for monster in monsters_list:
if monster.alive:
html_content += monster.html
return html_content
def generate_chests_html(chests_list):
html_content = ""
for chest in chests_list:
if chest.visible:
html_content += chest.html
return html_content
# ------------------------------------------------------------
#
# visual gimmicks
#
# ------------------------------------------------------------
def create_text_bubble_html(text, x, y):
"""Create an HTML text bubble with the given text and position.
Args:
text: A string representing the text to be displayed in the bubble.
x: An integer representing the starting x position of the bubble.
y: An integer representing the starting y position of the bubble.
Returns:
A string containing the HTML code for the text bubble.
"""
return f"""
<div class="container_text" style="position: relative;
grid-column-start: {x}; grid-row-start: {y}; grid-column-end: {x+4};">
<img src="https://raw.githubusercontent.com/TomJohnH/streamlit-dungeon/main/graphics/other/message.png">
<div style="position: absolute; top: 40%; left: 50%; transform: translate(-50%, -50%); font-size:0.875rem;">{text}</div>
</div>
"""
def get_text_boxes(player_x, player_y, level_name):
"""Get the text bubble at the player's current position in the given level.
Args:
player_x: An integer representing the x position of the player.
player_y: An integer representing the y position of the player.
level_name: A string representing the name of the level.
Returns:
A string containing the HTML code for the text bubble at the player's position.
"""
result = ""
level_data = st.session_state.level_data.get(level_name, {})
bubble_data = level_data.get("bubbles", {})
for bubble_name, data in bubble_data.items():
if data.get("x") == player_x and data.get("y") == player_y:
result = create_text_bubble_html(data.get("text"), player_x, player_y - 1)
if st.session_state.bubble_text:
result = st.session_state.bubble_text
st.session_state.bubble_text = ""
return result