Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -3,53 +3,52 @@ import random
|
|
3 |
import json
|
4 |
import math
|
5 |
|
6 |
-
# Set page config
|
7 |
-
st.set_page_config(page_title="HexCitySaga", layout="wide")
|
8 |
|
9 |
# Initialize game state
|
10 |
if 'game_state' not in st.session_state:
|
11 |
st.session_state.game_state = {
|
12 |
'hex_grid': [[{'type': 'empty', 'emoji': '', 'alive': False} for _ in range(12)] for _ in range(16)],
|
13 |
-
'
|
|
|
|
|
|
|
|
|
14 |
'players': {},
|
15 |
-
'train': {'x': 0, 'y': 5, 'dir': 1},
|
16 |
-
'monster_mode': False,
|
17 |
-
'current_monster': 'Godzilla',
|
18 |
-
'monster_x': 8,
|
19 |
-
'monster_y': 6,
|
20 |
'story': [],
|
21 |
-
'drawn_cards': 0
|
|
|
22 |
}
|
23 |
|
24 |
# Constants
|
25 |
HEX_SIZE = 40
|
26 |
PLAYER_NAMES = ["SkyWalker", "ForestRanger", "CityBuilder", "MonsterTamer", "RailMaster"]
|
27 |
PLANTS = ["π±", "π²", "π³", "π΄", "π΅"]
|
28 |
-
|
29 |
-
|
30 |
-
SUIT_PROPERTIES = {"Hearts": "
|
31 |
SENTENCE_TEMPLATES = {
|
32 |
-
"
|
33 |
-
"Monster": "A {property} {word}
|
34 |
-
"
|
|
|
|
|
35 |
}
|
36 |
|
37 |
-
# Game of Life
|
38 |
def apply_game_of_life(grid):
|
39 |
new_grid = [[cell.copy() for cell in row] for row in grid]
|
40 |
for i in range(len(grid)):
|
41 |
for j in range(len(grid[0])):
|
42 |
neighbors = count_neighbors(grid, i, j)
|
43 |
-
if grid[i][j]['type'] in ['
|
44 |
-
|
45 |
-
if neighbors < 2 or neighbors > 3: # Underpopulation or overpopulation
|
46 |
new_grid[i][j]['alive'] = False
|
47 |
new_grid[i][j]['type'] = 'empty'
|
48 |
new_grid[i][j]['emoji'] = ''
|
49 |
-
|
50 |
-
|
51 |
-
elif neighbors == 3 and grid[i][j]['type'] == 'empty': # Reproduction
|
52 |
-
new_grid[i][j]['type'] = 'placed'
|
53 |
new_grid[i][j]['emoji'] = random.choice(PLANTS)
|
54 |
new_grid[i][j]['alive'] = True
|
55 |
return new_grid
|
@@ -65,7 +64,7 @@ def count_neighbors(grid, x, y):
|
|
65 |
count += 1
|
66 |
return count
|
67 |
|
68 |
-
# Card deck
|
69 |
def create_deck():
|
70 |
suits = ["Hearts", "Diamonds", "Clubs", "Spades"]
|
71 |
ranks = list(range(1, 14))
|
@@ -81,11 +80,11 @@ p5js_code = """
|
|
81 |
const HEX_SIZE = 40;
|
82 |
const SQRT_3 = Math.sqrt(3);
|
83 |
let hexGrid = [];
|
84 |
-
let
|
85 |
-
let
|
86 |
-
let
|
87 |
-
let
|
88 |
-
let
|
89 |
let playerId;
|
90 |
let story = [];
|
91 |
|
@@ -96,25 +95,24 @@ function setup() {
|
|
96 |
|
97 |
function updateFromState() {
|
98 |
hexGrid = JSON.parse(document.getElementById('hex_grid').innerHTML);
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
monsterY = parseInt(document.getElementById('monster_y').innerHTML);
|
105 |
playerId = document.getElementById('player_id').innerHTML;
|
106 |
story = JSON.parse(document.getElementById('story').innerHTML);
|
107 |
}
|
108 |
|
109 |
function draw() {
|
110 |
-
background(
|
111 |
drawHexGrid();
|
112 |
-
|
113 |
-
|
114 |
-
|
|
|
|
|
115 |
drawStory();
|
116 |
-
train.x += train.dir * 0.05;
|
117 |
-
if (train.x >= hexGrid.length || train.x < 0) train.dir *= -1;
|
118 |
}
|
119 |
|
120 |
function drawHexGrid() {
|
@@ -122,7 +120,7 @@ function drawHexGrid() {
|
|
122 |
for (let j = 0; j < hexGrid[i].length; j++) {
|
123 |
let x = i * HEX_SIZE * 1.5;
|
124 |
let y = j * HEX_SIZE * SQRT_3 + (i % 2 ? HEX_SIZE * SQRT_3 / 2 : 0);
|
125 |
-
fill(hexGrid[i][j].type === '
|
126 |
stroke(0);
|
127 |
drawHex(x, y);
|
128 |
if (hexGrid[i][j].emoji) {
|
@@ -142,32 +140,58 @@ function drawHex(x, y) {
|
|
142 |
endShape(CLOSE);
|
143 |
}
|
144 |
|
145 |
-
function
|
146 |
-
|
147 |
-
let
|
148 |
-
let
|
149 |
-
|
150 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
151 |
ellipse(x, y, HEX_SIZE * 0.8);
|
152 |
textSize(20);
|
153 |
-
text(
|
154 |
});
|
155 |
}
|
156 |
|
157 |
-
function
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
|
|
|
|
|
|
163 |
}
|
164 |
|
165 |
-
function
|
166 |
-
let x =
|
167 |
-
let y =
|
168 |
-
fill(
|
169 |
-
ellipse(x, y, HEX_SIZE *
|
170 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
171 |
}
|
172 |
|
173 |
function drawStory() {
|
@@ -181,47 +205,98 @@ function drawStory() {
|
|
181 |
function mousePressed() {
|
182 |
let i = Math.floor(mouseX / (HEX_SIZE * 1.5));
|
183 |
let j = Math.floor((mouseY - (i % 2 ? HEX_SIZE * SQRT_3 / 2 : 0)) / (HEX_SIZE * SQRT_3));
|
184 |
-
if (i >= 0 && i < hexGrid.length && j >= 0 && j < hexGrid[0].length
|
185 |
-
let
|
186 |
-
if (
|
187 |
-
|
188 |
-
buildings.push({
|
189 |
-
x: i, y: j, emoji: emoji,
|
190 |
-
color: [random(100, 255), random(100, 255), random(100, 255)],
|
191 |
-
player: playerId
|
192 |
-
});
|
193 |
-
hexGrid[i][j].type = 'building';
|
194 |
-
hexGrid[i][j].alive = true;
|
195 |
-
addStory('Building', emoji);
|
196 |
-
} else {
|
197 |
-
hexGrid[i][j].type = 'placed';
|
198 |
-
hexGrid[i][j].emoji = emoji;
|
199 |
-
hexGrid[i][j].alive = true;
|
200 |
-
}
|
201 |
-
updateState();
|
202 |
}
|
203 |
}
|
204 |
}
|
205 |
|
206 |
function keyPressed() {
|
207 |
-
|
208 |
-
|
209 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
210 |
}
|
211 |
-
|
212 |
-
if (
|
213 |
-
|
214 |
-
|
215 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
216 |
}
|
217 |
-
updateState();
|
218 |
}
|
219 |
|
220 |
function addStory(type, word) {
|
221 |
let cardIndex = st.session_state.drawn_cards % 52;
|
222 |
let card = JSON.parse(document.getElementById('deck').innerHTML)[cardIndex];
|
223 |
let suit = card[0];
|
224 |
-
let sentence = `A ${SUIT_PROPERTIES[suit]} event
|
225 |
story.push(sentence);
|
226 |
st.session_state.drawn_cards += 1;
|
227 |
}
|
@@ -232,12 +307,11 @@ function updateState() {
|
|
232 |
headers: { 'Content-Type': 'application/json' },
|
233 |
body: JSON.stringify({
|
234 |
hex_grid: hexGrid,
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
monster_y: monsterY,
|
241 |
story: story,
|
242 |
player_id: playerId
|
243 |
})
|
@@ -248,60 +322,105 @@ function updateState() {
|
|
248 |
# UI Components
|
249 |
if 'player_id' not in st.session_state:
|
250 |
st.session_state.player_id = random.choice(PLAYER_NAMES)
|
|
|
|
|
|
|
251 |
|
252 |
-
player_id = st.sidebar.selectbox("Choose
|
253 |
st.session_state.player_id = player_id
|
254 |
|
255 |
-
st.sidebar.subheader("
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
st.session_state.selected_emoji = emoji
|
261 |
|
262 |
st.sidebar.subheader("π Scores")
|
263 |
for p, s in st.session_state.game_state['players'].items():
|
264 |
st.sidebar.write(f"{p}: {s}")
|
265 |
|
266 |
-
# Game HTML
|
267 |
game_html = f"""
|
268 |
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.2/p5.min.js"></script>
|
269 |
<div id="hex_grid" style="display:none">{json.dumps(st.session_state.game_state['hex_grid'])}</div>
|
270 |
-
<div id="
|
271 |
-
<div id="
|
272 |
-
<div id="
|
273 |
-
<div id="
|
274 |
-
<div id="
|
275 |
-
<div id="monster_y" style="display:none">{st.session_state.game_state['monster_y']}</div>
|
276 |
<div id="player_id" style="display:none">{player_id}</div>
|
277 |
-
<div id="selected_emoji" style="display:none">{selected_emoji}</div>
|
278 |
<div id="story" style="display:none">{json.dumps(st.session_state.game_state['story'])}</div>
|
279 |
<div id="deck" style="display:none">{json.dumps(st.session_state.deck)}</div>
|
280 |
<script>{p5js_code}</script>
|
281 |
"""
|
282 |
|
283 |
# Main layout
|
284 |
-
st.title("HexCitySaga:
|
285 |
-
st.write(f"
|
286 |
st.components.v1.html(game_html, height=500)
|
287 |
|
288 |
-
# State update handler
|
289 |
def update_state(data):
|
290 |
st.session_state.game_state.update({
|
291 |
'hex_grid': apply_game_of_life(data['hex_grid']),
|
292 |
-
'
|
293 |
-
'
|
294 |
-
'
|
295 |
-
'
|
296 |
-
'
|
297 |
-
'monster_y': data['monster_y'],
|
298 |
'story': data['story']
|
299 |
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
300 |
if data['player_id'] not in st.session_state.game_state['players']:
|
301 |
st.session_state.game_state['players'][data['player_id']] = 0
|
302 |
st.session_state.game_state['players'][data['player_id']] += 1
|
|
|
303 |
st.rerun()
|
304 |
|
305 |
-
# Initialize
|
306 |
-
for i in range(
|
307 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
import json
|
4 |
import math
|
5 |
|
6 |
+
# Set page config
|
7 |
+
st.set_page_config(page_title="HexCitySaga: Dungeon Crawl", layout="wide")
|
8 |
|
9 |
# Initialize game state
|
10 |
if 'game_state' not in st.session_state:
|
11 |
st.session_state.game_state = {
|
12 |
'hex_grid': [[{'type': 'empty', 'emoji': '', 'alive': False} for _ in range(12)] for _ in range(16)],
|
13 |
+
'heroes': {}, # {player_id: {x, y, hp, atk, def, xp, gear}}
|
14 |
+
'monsters': [], # [{x, y, type, hp, atk}]
|
15 |
+
'loot': [], # [{x, y, type}]
|
16 |
+
'exit': {'x': 15, 'y': 11}, # Goal to escape
|
17 |
+
'overlord': {'x': 14, 'y': 10, 'hp': 50, 'atk': 5, 'alive': True},
|
18 |
'players': {},
|
|
|
|
|
|
|
|
|
|
|
19 |
'story': [],
|
20 |
+
'drawn_cards': 0,
|
21 |
+
'turn': 0
|
22 |
}
|
23 |
|
24 |
# Constants
|
25 |
HEX_SIZE = 40
|
26 |
PLAYER_NAMES = ["SkyWalker", "ForestRanger", "CityBuilder", "MonsterTamer", "RailMaster"]
|
27 |
PLANTS = ["π±", "π²", "π³", "π΄", "π΅"]
|
28 |
+
LOOT = ["βοΈ", "π‘οΈ", "π°", "π"]
|
29 |
+
TRAPS = ["β‘", "π³οΈ"]
|
30 |
+
SUIT_PROPERTIES = {"Hearts": "heroic", "Diamonds": "treacherous", "Clubs": "fierce", "Spades": "enigmatic"}
|
31 |
SENTENCE_TEMPLATES = {
|
32 |
+
"Hero": "A {property} hero {word} ventured forth.",
|
33 |
+
"Monster": "A {property} {word} ambushed from the shadows!",
|
34 |
+
"Loot": "The party found a {property} {word} in the depths.",
|
35 |
+
"Trap": "A {property} {word} sprung upon them!",
|
36 |
+
"Puzzle": "An {property} puzzle blocked with {word}."
|
37 |
}
|
38 |
|
39 |
+
# Game of Life for dungeon decay
|
40 |
def apply_game_of_life(grid):
|
41 |
new_grid = [[cell.copy() for cell in row] for row in grid]
|
42 |
for i in range(len(grid)):
|
43 |
for j in range(len(grid[0])):
|
44 |
neighbors = count_neighbors(grid, i, j)
|
45 |
+
if grid[i][j]['type'] in ['plant', 'trap'] and grid[i][j]['alive']:
|
46 |
+
if neighbors < 2 or neighbors > 3:
|
|
|
47 |
new_grid[i][j]['alive'] = False
|
48 |
new_grid[i][j]['type'] = 'empty'
|
49 |
new_grid[i][j]['emoji'] = ''
|
50 |
+
elif grid[i][j]['type'] == 'empty' and neighbors == 3:
|
51 |
+
new_grid[i][j]['type'] = 'plant'
|
|
|
|
|
52 |
new_grid[i][j]['emoji'] = random.choice(PLANTS)
|
53 |
new_grid[i][j]['alive'] = True
|
54 |
return new_grid
|
|
|
64 |
count += 1
|
65 |
return count
|
66 |
|
67 |
+
# Card deck
|
68 |
def create_deck():
|
69 |
suits = ["Hearts", "Diamonds", "Clubs", "Spades"]
|
70 |
ranks = list(range(1, 14))
|
|
|
80 |
const HEX_SIZE = 40;
|
81 |
const SQRT_3 = Math.sqrt(3);
|
82 |
let hexGrid = [];
|
83 |
+
let heroes = {};
|
84 |
+
let monsters = [];
|
85 |
+
let loot = [];
|
86 |
+
let exit = {};
|
87 |
+
let overlord = {};
|
88 |
let playerId;
|
89 |
let story = [];
|
90 |
|
|
|
95 |
|
96 |
function updateFromState() {
|
97 |
hexGrid = JSON.parse(document.getElementById('hex_grid').innerHTML);
|
98 |
+
heroes = JSON.parse(document.getElementById('heroes').innerHTML);
|
99 |
+
monsters = JSON.parse(document.getElementById('monsters').innerHTML);
|
100 |
+
loot = JSON.parse(document.getElementById('loot').innerHTML);
|
101 |
+
exit = JSON.parse(document.getElementById('exit').innerHTML);
|
102 |
+
overlord = JSON.parse(document.getElementById('overlord').innerHTML);
|
|
|
103 |
playerId = document.getElementById('player_id').innerHTML;
|
104 |
story = JSON.parse(document.getElementById('story').innerHTML);
|
105 |
}
|
106 |
|
107 |
function draw() {
|
108 |
+
background(169, 169, 169); // Dungeon gray
|
109 |
drawHexGrid();
|
110 |
+
drawLoot();
|
111 |
+
drawHeroes();
|
112 |
+
drawMonsters();
|
113 |
+
drawExit();
|
114 |
+
drawOverlord();
|
115 |
drawStory();
|
|
|
|
|
116 |
}
|
117 |
|
118 |
function drawHexGrid() {
|
|
|
120 |
for (let j = 0; j < hexGrid[i].length; j++) {
|
121 |
let x = i * HEX_SIZE * 1.5;
|
122 |
let y = j * HEX_SIZE * SQRT_3 + (i % 2 ? HEX_SIZE * SQRT_3 / 2 : 0);
|
123 |
+
fill(hexGrid[i][j].type === 'wall' ? '#555' : hexGrid[i][j].alive ? '#90EE90' : '#A9A9A9');
|
124 |
stroke(0);
|
125 |
drawHex(x, y);
|
126 |
if (hexGrid[i][j].emoji) {
|
|
|
140 |
endShape(CLOSE);
|
141 |
}
|
142 |
|
143 |
+
function drawHeroes() {
|
144 |
+
Object.keys(heroes).forEach(h => {
|
145 |
+
let hero = heroes[h];
|
146 |
+
let x = hero.x * HEX_SIZE * 1.5;
|
147 |
+
let y = hero.y * HEX_SIZE * SQRT_3 + (hero.x % 2 ? HEX_SIZE * SQRT_3 / 2 : 0);
|
148 |
+
fill(0, 255, 0);
|
149 |
+
ellipse(x, y, HEX_SIZE * 0.8);
|
150 |
+
textSize(14);
|
151 |
+
text(h.slice(0, 2), x - 10, y + 5);
|
152 |
+
});
|
153 |
+
}
|
154 |
+
|
155 |
+
function drawMonsters() {
|
156 |
+
monsters.forEach(m => {
|
157 |
+
let x = m.x * HEX_SIZE * 1.5;
|
158 |
+
let y = m.y * HEX_SIZE * SQRT_3 + (m.x % 2 ? HEX_SIZE * SQRT_3 / 2 : 0);
|
159 |
+
fill(255, 0, 0);
|
160 |
ellipse(x, y, HEX_SIZE * 0.8);
|
161 |
textSize(20);
|
162 |
+
text(m.type === 'Godzilla' ? 'π¦' : 'π€', x - 10, y + 5);
|
163 |
});
|
164 |
}
|
165 |
|
166 |
+
function drawLoot() {
|
167 |
+
loot.forEach(l => {
|
168 |
+
let x = l.x * HEX_SIZE * 1.5;
|
169 |
+
let y = l.y * HEX_SIZE * SQRT_3 + (l.x % 2 ? HEX_SIZE * SQRT_3 / 2 : 0);
|
170 |
+
fill(255, 215, 0);
|
171 |
+
ellipse(x, y, HEX_SIZE * 0.6);
|
172 |
+
textSize(20);
|
173 |
+
text(l.type, x - 10, y + 5);
|
174 |
+
});
|
175 |
}
|
176 |
|
177 |
+
function drawExit() {
|
178 |
+
let x = exit.x * HEX_SIZE * 1.5;
|
179 |
+
let y = exit.y * HEX_SIZE * SQRT_3 + (exit.x % 2 ? HEX_SIZE * SQRT_3 / 2 : 0);
|
180 |
+
fill(0, 191, 255);
|
181 |
+
ellipse(x, y, HEX_SIZE * 0.8);
|
182 |
+
textSize(20);
|
183 |
+
text('πͺ', x - 10, y + 5);
|
184 |
+
}
|
185 |
+
|
186 |
+
function drawOverlord() {
|
187 |
+
if (overlord.alive) {
|
188 |
+
let x = overlord.x * HEX_SIZE * 1.5;
|
189 |
+
let y = overlord.y * HEX_SIZE * SQRT_3 + (overlord.x % 2 ? HEX_SIZE * SQRT_3 / 2 : 0);
|
190 |
+
fill(139, 0, 139);
|
191 |
+
ellipse(x, y, HEX_SIZE * 1.2);
|
192 |
+
textSize(20);
|
193 |
+
text('π', x - 10, y + 5);
|
194 |
+
}
|
195 |
}
|
196 |
|
197 |
function drawStory() {
|
|
|
205 |
function mousePressed() {
|
206 |
let i = Math.floor(mouseX / (HEX_SIZE * 1.5));
|
207 |
let j = Math.floor((mouseY - (i % 2 ? HEX_SIZE * SQRT_3 / 2 : 0)) / (HEX_SIZE * SQRT_3));
|
208 |
+
if (i >= 0 && i < hexGrid.length && j >= 0 && j < hexGrid[0].length) {
|
209 |
+
let hero = heroes[playerId];
|
210 |
+
if (hexGrid[i][j].type === 'empty' && Math.abs(hero.x - i) <= 1 && Math.abs(hero.y - j) <= 1) {
|
211 |
+
moveHero(i, j);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
212 |
}
|
213 |
}
|
214 |
}
|
215 |
|
216 |
function keyPressed() {
|
217 |
+
let hero = heroes[playerId];
|
218 |
+
if (key === 'w' || key === 'W') moveHero(hero.x, hero.y - 1);
|
219 |
+
if (key === 's' || key === 'S') moveHero(hero.x, hero.y + 1);
|
220 |
+
if (key === 'a' || key === 'A') moveHero(hero.x - 1, hero.y);
|
221 |
+
if (key === 'd' || key === 'D') moveHero(hero.x + 1, hero.y);
|
222 |
+
if (key === 'q' || key === 'Q') moveHero(hero.x - 1, hero.y - 1);
|
223 |
+
if (key === 'e' || key === 'E') moveHero(hero.x + 1, hero.y - 1);
|
224 |
+
}
|
225 |
+
|
226 |
+
function moveHero(newX, newY) {
|
227 |
+
if (newX < 0 || newX >= hexGrid.length || newY < 0 || newY >= hexGrid[0].length) return;
|
228 |
+
let hero = heroes[playerId];
|
229 |
+
let cell = hexGrid[newX][newY];
|
230 |
+
if (cell.type === 'empty' || cell.type === 'plant') {
|
231 |
+
hero.x = newX;
|
232 |
+
hero.y = newY;
|
233 |
+
handleInteractions();
|
234 |
+
updateState();
|
235 |
+
}
|
236 |
+
}
|
237 |
+
|
238 |
+
function handleInteractions() {
|
239 |
+
let hero = heroes[playerId];
|
240 |
+
// Loot
|
241 |
+
let lootIdx = loot.findIndex(l => l.x === hero.x && l.y === hero.y);
|
242 |
+
if (lootIdx !== -1) {
|
243 |
+
let item = loot[lootIdx].type;
|
244 |
+
if (item === 'βοΈ') hero.atk += 2;
|
245 |
+
if (item === 'π‘οΈ') hero.def += 2;
|
246 |
+
if (item === 'π°') hero.xp += 10;
|
247 |
+
if (item === 'π') hero.gear.push('π');
|
248 |
+
loot.splice(lootIdx, 1);
|
249 |
+
addStory('Loot', item);
|
250 |
+
}
|
251 |
+
// Trap
|
252 |
+
if (hexGrid[hero.x][hero.y].type === 'trap') {
|
253 |
+
hero.hp -= random(1, 5);
|
254 |
+
addStory('Trap', hexGrid[hero.x][hero.y].emoji);
|
255 |
+
}
|
256 |
+
// Monster combat
|
257 |
+
let monsterIdx = monsters.findIndex(m => m.x === hero.x && m.y === hero.y);
|
258 |
+
if (monsterIdx !== -1) {
|
259 |
+
let monster = monsters[monsterIdx];
|
260 |
+
let damage = Math.max(0, hero.atk - 1);
|
261 |
+
monster.hp -= damage;
|
262 |
+
hero.hp -= Math.max(0, monster.atk - hero.def);
|
263 |
+
if (monster.hp <= 0) {
|
264 |
+
monsters.splice(monsterIdx, 1);
|
265 |
+
hero.xp += 10;
|
266 |
+
addStory('Monster', monster.type + ' slain');
|
267 |
+
}
|
268 |
}
|
269 |
+
// Overlord combat
|
270 |
+
if (overlord.alive && hero.x === overlord.x && hero.y === overlord.y) {
|
271 |
+
let damage = Math.max(0, hero.atk - 2);
|
272 |
+
overlord.hp -= damage;
|
273 |
+
hero.hp -= Math.max(0, overlord.atk - hero.def);
|
274 |
+
if (overlord.hp <= 0) {
|
275 |
+
overlord.alive = false;
|
276 |
+
hero.xp += 50;
|
277 |
+
addStory('Monster', 'Overlord defeated');
|
278 |
+
}
|
279 |
+
}
|
280 |
+
// Exit
|
281 |
+
if (hero.x === exit.x && hero.y === exit.y && hero.gear.includes('π')) {
|
282 |
+
alert('Victory! You escaped the Hex Dungeon!');
|
283 |
+
resetGame();
|
284 |
+
}
|
285 |
+
// Level up
|
286 |
+
if (hero.xp >= hero.level * 20) {
|
287 |
+
hero.level += 1;
|
288 |
+
hero.hp += 5;
|
289 |
+
hero.atk += 1;
|
290 |
+
hero.def += 1;
|
291 |
+
addStory('Hero', 'leveled up');
|
292 |
}
|
|
|
293 |
}
|
294 |
|
295 |
function addStory(type, word) {
|
296 |
let cardIndex = st.session_state.drawn_cards % 52;
|
297 |
let card = JSON.parse(document.getElementById('deck').innerHTML)[cardIndex];
|
298 |
let suit = card[0];
|
299 |
+
let sentence = `A ${SUIT_PROPERTIES[suit]} event: ${type.toLowerCase()} ${word}`;
|
300 |
story.push(sentence);
|
301 |
st.session_state.drawn_cards += 1;
|
302 |
}
|
|
|
307 |
headers: { 'Content-Type': 'application/json' },
|
308 |
body: JSON.stringify({
|
309 |
hex_grid: hexGrid,
|
310 |
+
heroes: heroes,
|
311 |
+
monsters: monsters,
|
312 |
+
loot: loot,
|
313 |
+
exit: exit,
|
314 |
+
overlord: overlord,
|
|
|
315 |
story: story,
|
316 |
player_id: playerId
|
317 |
})
|
|
|
322 |
# UI Components
|
323 |
if 'player_id' not in st.session_state:
|
324 |
st.session_state.player_id = random.choice(PLAYER_NAMES)
|
325 |
+
st.session_state.game_state['heroes'][st.session_state.player_id] = {
|
326 |
+
'x': 0, 'y': 0, 'hp': 20, 'atk': 3, 'def': 1, 'xp': 0, 'level': 1, 'gear': []
|
327 |
+
}
|
328 |
|
329 |
+
player_id = st.sidebar.selectbox("Choose Hero", PLAYER_NAMES, index=PLAYER_NAMES.index(st.session_state.player_id))
|
330 |
st.session_state.player_id = player_id
|
331 |
|
332 |
+
st.sidebar.subheader("π‘οΈ Hero Stats")
|
333 |
+
hero = st.session_state.game_state['heroes'].get(player_id, {})
|
334 |
+
st.sidebar.write(f"HP: {hero.get('hp', 20)} | ATK: {hero.get('atk', 3)} | DEF: {hero.get('def', 1)}")
|
335 |
+
st.sidebar.write(f"Level: {hero.get('level', 1)} | XP: {hero.get('xp', 0)}")
|
336 |
+
st.sidebar.write(f"Gear: {', '.join(hero.get('gear', [])) or 'None'}")
|
|
|
337 |
|
338 |
st.sidebar.subheader("π Scores")
|
339 |
for p, s in st.session_state.game_state['players'].items():
|
340 |
st.sidebar.write(f"{p}: {s}")
|
341 |
|
342 |
+
# Game HTML
|
343 |
game_html = f"""
|
344 |
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.2/p5.min.js"></script>
|
345 |
<div id="hex_grid" style="display:none">{json.dumps(st.session_state.game_state['hex_grid'])}</div>
|
346 |
+
<div id="heroes" style="display:none">{json.dumps(st.session_state.game_state['heroes'])}</div>
|
347 |
+
<div id="monsters" style="display:none">{json.dumps(st.session_state.game_state['monsters'])}</div>
|
348 |
+
<div id="loot" style="display:none">{json.dumps(st.session_state.game_state['loot'])}</div>
|
349 |
+
<div id="exit" style="display:none">{json.dumps(st.session_state.game_state['exit'])}</div>
|
350 |
+
<div id="overlord" style="display:none">{json.dumps(st.session_state.game_state['overlord'])}</div>
|
|
|
351 |
<div id="player_id" style="display:none">{player_id}</div>
|
|
|
352 |
<div id="story" style="display:none">{json.dumps(st.session_state.game_state['story'])}</div>
|
353 |
<div id="deck" style="display:none">{json.dumps(st.session_state.deck)}</div>
|
354 |
<script>{p5js_code}</script>
|
355 |
"""
|
356 |
|
357 |
# Main layout
|
358 |
+
st.title("HexCitySaga: Dungeon Crawl")
|
359 |
+
st.write(f"Hero: {player_id}. Use WASD/QE to move. Goal: Escape (πͺ) with π or slay the Overlord (π).")
|
360 |
st.components.v1.html(game_html, height=500)
|
361 |
|
362 |
+
# State update handler with Overlord AI
|
363 |
def update_state(data):
|
364 |
st.session_state.game_state.update({
|
365 |
'hex_grid': apply_game_of_life(data['hex_grid']),
|
366 |
+
'heroes': data['heroes'],
|
367 |
+
'monsters': data['monsters'],
|
368 |
+
'loot': data['loot'],
|
369 |
+
'exit': data['exit'],
|
370 |
+
'overlord': data['overlord'],
|
|
|
371 |
'story': data['story']
|
372 |
})
|
373 |
+
# Random events
|
374 |
+
if random.random() < 0.1:
|
375 |
+
spawn = {'x': random.randint(0, 15), 'y': random.randint(0, 11)}
|
376 |
+
if random.random() < 0.5:
|
377 |
+
st.session_state.game_state['monsters'].append({'x': spawn['x'], 'y': spawn['y'], 'type': random.choice(['Godzilla', 'GiantRobot']), 'hp': 10, 'atk': 2})
|
378 |
+
st.session_state.game_state['story'].append(f"A fierce ambush: Monster at ({spawn['x']}, {spawn['y']})!")
|
379 |
+
else:
|
380 |
+
st.session_state.game_state['loot'].append({'x': spawn['x'], 'y': spawn['y'], 'type': random.choice(LOOT)})
|
381 |
+
st.session_state.game_state['story'].append(f"A treacherous find: Loot at ({spawn['x']}, {spawn['y']})!")
|
382 |
+
# Traps
|
383 |
+
if random.random() < 0.05:
|
384 |
+
tx, ty = random.randint(0, 15), random.randint(0, 11)
|
385 |
+
if st.session_state.game_state['hex_grid'][tx][ty]['type'] == 'empty':
|
386 |
+
st.session_state.game_state['hex_grid'][tx][ty] = {'type': 'trap', 'emoji': random.choice(TRAPS), 'alive': True}
|
387 |
+
# Overlord move
|
388 |
+
if st.session_state.game_state['overlord']['alive']:
|
389 |
+
closest_hero = min(st.session_state.game_state['heroes'].items(), key=lambda h: abs(h[1]['x'] - st.session_state.game_state['overlord']['x']) + abs(h[1]['y'] - st.session_state.game_state['overlord']['y']))
|
390 |
+
dx = 1 if closest_hero[1]['x'] > st.session_state.game_state['overlord']['x'] else -1 if closest_hero[1]['x'] < st.session_state.game_state['overlord']['x'] else 0
|
391 |
+
dy = 1 if closest_hero[1]['y'] > st.session_state.game_state['overlord']['y'] else -1 if closest_hero[1]['y'] < st.session_state.game_state['overlord']['y'] else 0
|
392 |
+
new_x, new_y = st.session_state.game_state['overlord']['x'] + dx, st.session_state.game_state['overlord']['y'] + dy
|
393 |
+
if 0 <= new_x < 16 and 0 <= new_y < 12 and st.session_state.game_state['hex_grid'][new_x][new_y]['type'] == 'empty':
|
394 |
+
st.session_state.game_state['overlord']['x'], st.session_state.game_state['overlord']['y'] = new_x, new_y
|
395 |
if data['player_id'] not in st.session_state.game_state['players']:
|
396 |
st.session_state.game_state['players'][data['player_id']] = 0
|
397 |
st.session_state.game_state['players'][data['player_id']] += 1
|
398 |
+
st.session_state.game_state['turn'] += 1
|
399 |
st.rerun()
|
400 |
|
401 |
+
# Initialize dungeon
|
402 |
+
for i in range(16):
|
403 |
+
for j in range(12):
|
404 |
+
if random.random() < 0.1:
|
405 |
+
st.session_state.game_state['hex_grid'][i][j] = {'type': 'wall', 'emoji': 'π§±', 'alive': False}
|
406 |
+
|
407 |
+
def reset_game():
|
408 |
+
st.session_state.game_state = {
|
409 |
+
'hex_grid': [[{'type': 'empty', 'emoji': '', 'alive': False} for _ in range(12)] for _ in range(16)],
|
410 |
+
'heroes': {player_id: {'x': 0, 'y': 0, 'hp': 20, 'atk': 3, 'def': 1, 'xp': 0, 'level': 1, 'gear': []}},
|
411 |
+
'monsters': [],
|
412 |
+
'loot': [],
|
413 |
+
'exit': {'x': 15, 'y': 11},
|
414 |
+
'overlord': {'x': 14, 'y': 10, 'hp': 50, 'atk': 5, 'alive': True},
|
415 |
+
'players': {},
|
416 |
+
'story': [],
|
417 |
+
'drawn_cards': 0,
|
418 |
+
'turn': 0
|
419 |
+
}
|
420 |
+
for i in range(16):
|
421 |
+
for j in range(12):
|
422 |
+
if random.random() < 0.1:
|
423 |
+
st.session_state.game_state['hex_grid'][i][j] = {'type': 'wall', 'emoji': 'π§±', 'alive': False}
|
424 |
+
|
425 |
+
if st.sidebar.button("Reset Game"):
|
426 |
+
reset_game()
|