awacke1 commited on
Commit
e4dc0c8
Β·
verified Β·
1 Parent(s): 5a10712

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +161 -0
app.py ADDED
@@ -0,0 +1,161 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import numpy as np
3
+ import time
4
+ from dataclasses import dataclass
5
+ from typing import Dict, List, Optional
6
+
7
+ @dataclass
8
+ class Building:
9
+ name: str
10
+ cost: Dict[str, int]
11
+ produces: str
12
+ color: str
13
+ icon: str
14
+ description: str
15
+
16
+ class FactoryGame:
17
+ def __init__(self):
18
+ self.GRID_SIZE = 8
19
+ self.buildings = {
20
+ "solar_panel": Building(
21
+ name="Solar Panel",
22
+ cost={"minerals": 10},
23
+ produces="energy",
24
+ color="#FEF08A",
25
+ icon="β˜€οΈ",
26
+ description="Produces energy from sunlight"
27
+ ),
28
+ "mineral_extractor": Building(
29
+ name="Mineral Extractor",
30
+ cost={"energy": 10},
31
+ produces="minerals",
32
+ color="#D1D5DB",
33
+ icon="⛏️",
34
+ description="Extracts minerals from the ground"
35
+ ),
36
+ "circuit_factory": Building(
37
+ name="Circuit Factory",
38
+ cost={"energy": 15, "minerals": 15},
39
+ produces="circuits",
40
+ color="#BBF7D0",
41
+ icon="πŸ”§",
42
+ description="Produces electronic circuits"
43
+ )
44
+ }
45
+
46
+ # Initialize session state if needed
47
+ if "resources" not in st.session_state:
48
+ st.session_state.resources = {
49
+ "energy": 50,
50
+ "minerals": 50,
51
+ "circuits": 0
52
+ }
53
+ if "grid" not in st.session_state:
54
+ st.session_state.grid = [[None for _ in range(self.GRID_SIZE)]
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()
61
+ elapsed = current_time - st.session_state.last_update
62
+
63
+ if elapsed >= 1.0: # Update every second
64
+ for row in range(self.GRID_SIZE):
65
+ for col in range(self.GRID_SIZE):
66
+ building_id = st.session_state.grid[row][col]
67
+ if building_id:
68
+ building = self.buildings[building_id]
69
+ st.session_state.resources[building.produces] += 1
70
+ st.session_state.last_update = current_time
71
+
72
+ def can_afford(self, building_id: str) -> bool:
73
+ building = self.buildings[building_id]
74
+ for resource, cost in building.cost.items():
75
+ if st.session_state.resources.get(resource, 0) < cost:
76
+ return False
77
+ return True
78
+
79
+ def place_building(self, row: int, col: int, building_id: str) -> bool:
80
+ if not self.can_afford(building_id):
81
+ st.error(f"Cannot afford {self.buildings[building_id].name}!")
82
+ return False
83
+
84
+ if st.session_state.grid[row][col] is not None:
85
+ st.error("Space already occupied!")
86
+ return False
87
+
88
+ # Deduct costs
89
+ building = self.buildings[building_id]
90
+ for resource, cost in building.cost.items():
91
+ st.session_state.resources[resource] -= cost
92
+
93
+ # Place building
94
+ st.session_state.grid[row][col] = building_id
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
+
101
+ game = FactoryGame()
102
+
103
+ # Title and description
104
+ st.title("🏭 Factory Game")
105
+ st.markdown("""
106
+ Build your factory empire! Place buildings, manage resources, and optimize production.
107
+ """)
108
+
109
+ # Layout columns
110
+ col1, col2 = st.columns([3, 1])
111
+
112
+ with col2:
113
+ # Resources display
114
+ st.subheader("Resources")
115
+ st.metric("⚑ Energy", f"{st.session_state.resources['energy']}")
116
+ st.metric("πŸͺ¨ Minerals", f"{st.session_state.resources['minerals']}")
117
+ st.metric("πŸ”Œ Circuits", f"{st.session_state.resources['circuits']}")
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
+ selected_building = building_id
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):
139
+ building_id = st.session_state.grid[row][col]
140
+ building = game.buildings.get(building_id) if building_id else None
141
+
142
+ # Style the button based on what's there
143
+ label = building.icon if building else "⬜"
144
+
145
+ if column.button(
146
+ label,
147
+ key=f"cell_{row}_{col}",
148
+ help=building.name if building else "Empty space"
149
+ ):
150
+ if selected_building:
151
+ game.place_building(row, col, selected_building)
152
+
153
+ # Update resources periodically
154
+ game.update_resources()
155
+
156
+ # Force a rerun every second to update resources
157
+ time.sleep(0.1)
158
+ st.experimental_rerun()
159
+
160
+ if __name__ == "__main__":
161
+ main()