Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -55,6 +55,8 @@ class FactoryGame:
|
|
55 |
for _ in range(self.GRID_SIZE)]
|
56 |
if "last_update" not in st.session_state:
|
57 |
st.session_state.last_update = time.time()
|
|
|
|
|
58 |
|
59 |
def update_resources(self):
|
60 |
current_time = time.time()
|
@@ -95,6 +97,13 @@ class FactoryGame:
|
|
95 |
st.success(f"Placed {building.name}")
|
96 |
return True
|
97 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
def main():
|
99 |
st.set_page_config(page_title="Factory Game", layout="wide")
|
100 |
|
@@ -118,21 +127,28 @@ def main():
|
|
118 |
|
119 |
# Building selection
|
120 |
st.subheader("Buildings")
|
121 |
-
selected_building = None
|
122 |
for building_id, building in game.buildings.items():
|
123 |
if st.button(
|
124 |
f"{building.icon} {building.name}\n"
|
125 |
f"Cost: {', '.join(f'{cost} {res}' for res, cost in building.cost.items())}",
|
126 |
help=building.description,
|
127 |
-
key=f"building_{building_id}"
|
|
|
128 |
):
|
129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
130 |
|
131 |
with col1:
|
132 |
# Game grid
|
133 |
st.subheader("Factory Grid")
|
134 |
|
135 |
-
# Create a grid of buttons
|
|
|
136 |
for row in range(game.GRID_SIZE):
|
137 |
cols = st.columns(game.GRID_SIZE)
|
138 |
for col, column in enumerate(cols):
|
@@ -141,14 +157,14 @@ def main():
|
|
141 |
|
142 |
# Style the button based on what's there
|
143 |
label = building.icon if building else "⬜"
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
|
153 |
# Update resources periodically
|
154 |
game.update_resources()
|
|
|
55 |
for _ in range(self.GRID_SIZE)]
|
56 |
if "last_update" not in st.session_state:
|
57 |
st.session_state.last_update = time.time()
|
58 |
+
if "selected_building" not in st.session_state:
|
59 |
+
st.session_state.selected_building = None
|
60 |
|
61 |
def update_resources(self):
|
62 |
current_time = time.time()
|
|
|
97 |
st.success(f"Placed {building.name}")
|
98 |
return True
|
99 |
|
100 |
+
def select_building(building_id: str):
|
101 |
+
st.session_state.selected_building = building_id
|
102 |
+
|
103 |
+
def handle_cell_click(row: int, col: int, game: FactoryGame):
|
104 |
+
if st.session_state.selected_building:
|
105 |
+
game.place_building(row, col, st.session_state.selected_building)
|
106 |
+
|
107 |
def main():
|
108 |
st.set_page_config(page_title="Factory Game", layout="wide")
|
109 |
|
|
|
127 |
|
128 |
# Building selection
|
129 |
st.subheader("Buildings")
|
|
|
130 |
for building_id, building in game.buildings.items():
|
131 |
if st.button(
|
132 |
f"{building.icon} {building.name}\n"
|
133 |
f"Cost: {', '.join(f'{cost} {res}' for res, cost in building.cost.items())}",
|
134 |
help=building.description,
|
135 |
+
key=f"building_{building_id}",
|
136 |
+
type="secondary" if st.session_state.selected_building != building_id else "primary"
|
137 |
):
|
138 |
+
select_building(building_id)
|
139 |
+
|
140 |
+
# Currently selected building
|
141 |
+
if st.session_state.selected_building:
|
142 |
+
st.caption(f"Selected: {game.buildings[st.session_state.selected_building].name}")
|
143 |
+
else:
|
144 |
+
st.caption("Select a building to place")
|
145 |
|
146 |
with col1:
|
147 |
# Game grid
|
148 |
st.subheader("Factory Grid")
|
149 |
|
150 |
+
# Create a grid of buttons using containers for better layout
|
151 |
+
grid_container = st.container()
|
152 |
for row in range(game.GRID_SIZE):
|
153 |
cols = st.columns(game.GRID_SIZE)
|
154 |
for col, column in enumerate(cols):
|
|
|
157 |
|
158 |
# Style the button based on what's there
|
159 |
label = building.icon if building else "⬜"
|
160 |
+
with column:
|
161 |
+
if st.button(
|
162 |
+
label,
|
163 |
+
key=f"cell_{row}_{col}",
|
164 |
+
help=building.name if building else "Empty space",
|
165 |
+
use_container_width=True
|
166 |
+
):
|
167 |
+
handle_cell_click(row, col, game)
|
168 |
|
169 |
# Update resources periodically
|
170 |
game.update_resources()
|