awacke1 commited on
Commit
c1b75cc
Β·
verified Β·
1 Parent(s): 45f7f6d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -39
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import streamlit as st
2
  from streamlit.components.v1 import html
3
 
4
- # Define the enhanced p5.js sketch code as a string
5
  p5js_code = """
6
  let gridSize = 40;
7
  let grid = [];
@@ -11,6 +11,7 @@ let monsterMode = false;
11
  let currentMonster = 'Godzilla';
12
  let monsterX, monsterY;
13
  let angle = PI / 6; // 3D perspective angle
 
14
 
15
  function setup() {
16
  createCanvas(800, 600);
@@ -27,6 +28,7 @@ function setup() {
27
  train.y = 5 * gridSize;
28
  monsterX = width / 2;
29
  monsterY = height / 2;
 
30
  }
31
 
32
  function draw() {
@@ -35,6 +37,8 @@ function draw() {
35
  drawBuildings();
36
  drawTrain();
37
  if (monsterMode) drawMonster();
 
 
38
  }
39
 
40
  function drawGrid() {
@@ -42,7 +46,7 @@ function drawGrid() {
42
  for (let j = 0; j < grid[i].length; j++) {
43
  let x = i * gridSize;
44
  let y = j * gridSize;
45
- let z = j * gridSize * sin(angle); // 3D depth effect
46
  fill(grid[i][j] === 'track' ? 100 : 150, 200, 150);
47
  stroke(0);
48
  beginShape();
@@ -63,23 +67,23 @@ function drawBuildings() {
63
  fill(b.color);
64
  noStroke();
65
  beginShape();
66
- if (b.type === 'Residential') { // House with chimney
67
  vertex(x + 10, y + 30 - z); vertex(x + 20, y + 10 - z); vertex(x + 30, y + 30 - z);
68
  vertex(x + 25, y + 30 - z); vertex(x + 25, y + 35 - z); vertex(x + 15, y + 35 - z);
69
  vertex(x + 15, y + 30 - z); vertex(x + 17, y + 25 - z); vertex(x + 23, y + 25 - z);
70
- } else if (b.type === 'Commercial') { // Skyscraper with windows
71
  vertex(x + 10, y + 35 - z); vertex(x + 15, y + 15 - z); vertex(x + 25, y + 15 - z);
72
  vertex(x + 30, y + 35 - z); vertex(x + 27, y + 35 - z); vertex(x + 27, y + 20 - z);
73
  vertex(x + 13, y + 20 - z); vertex(x + 13, y + 35 - z);
74
- } else if (b.type === 'Industrial') { // Factory with smokestack
75
  vertex(x + 5, y + 35 - z); vertex(x + 15, y + 20 - z); vertex(x + 25, y + 20 - z);
76
  vertex(x + 35, y + 35 - z); vertex(x + 30, y + 35 - z); vertex(x + 30, y + 15 - z);
77
  vertex(x + 33, y + 15 - z); vertex(x + 33, y + 35 - z);
78
- } else if (b.type === 'School') { // School with flagpole
79
  vertex(x + 5, y + 35 - z); vertex(x + 10, y + 20 - z); vertex(x + 30, y + 20 - z);
80
  vertex(x + 35, y + 35 - z); vertex(x + 25, y + 35 - z); vertex(x + 25, y + 10 - z);
81
  vertex(x + 27, y + 10 - z); vertex(x + 27, y + 35 - z);
82
- } else if (b.type === 'PowerPlant') { // Power plant with turbines
83
  vertex(x + 5, y + 35 - z); vertex(x + 15, y + 15 - z); vertex(x + 25, y + 15 - z);
84
  vertex(x + 35, y + 35 - z); vertex(x + 30, y + 35 - z); vertex(x + 30, y + 25 - z);
85
  vertex(x + 20, y + 25 - z); vertex(x + 20, y + 35 - z);
@@ -109,13 +113,13 @@ function drawMonster() {
109
  fill(255, 0, 0);
110
  noStroke();
111
  beginShape();
112
- if (currentMonster === 'Godzilla') { // Detailed Godzilla with spines
113
  vertex(monsterX, monsterY + 40 - mz); vertex(monsterX + 10, monsterY + 20 - mz);
114
  vertex(monsterX + 20, monsterY - mz); vertex(monsterX + 30, monsterY + 20 - mz);
115
  vertex(monsterX + 40, monsterY + 40 - mz); vertex(monsterX + 35, monsterY + 50 - mz);
116
  vertex(monsterX + 25, monsterY + 60 - mz); vertex(monsterX + 15, monsterY + 50 - mz);
117
  vertex(monsterX + 20, monsterY + 40 - mz); vertex(monsterX + 25, monsterY + 30 - mz);
118
- } else if (currentMonster === 'GiantRobot') { // Detailed Robot with joints
119
  vertex(monsterX, monsterY + 40 - mz); vertex(monsterX + 10, monsterY + 20 - mz);
120
  vertex(monsterX + 15, monsterY - mz); vertex(monsterX + 25, monsterY - mz);
121
  vertex(monsterX + 30, monsterY + 20 - mz); vertex(monsterX + 40, monsterY + 40 - mz);
@@ -132,25 +136,49 @@ function drawMonster() {
132
  function mousePressed() {
133
  let i = floor(mouseX / gridSize);
134
  let j = floor(mouseY / gridSize);
135
- if (grid[i][j] === 'empty' && !monsterMode) {
136
- let types = ['Residential', 'Commercial', 'Industrial', 'School', 'PowerPlant'];
137
- let colors = [[0, 200, 0], [0, 0, 200], [200, 200, 0], [200, 0, 200], [100, 100, 100]];
138
- let idx = floor(random(5));
139
- buildings.push({ x: i, y: j, type: types[idx], color: colors[idx] });
140
- grid[i][j] = 'building';
 
 
 
141
  }
142
  }
143
 
144
  function keyPressed() {
145
- if (key === 'm' || key === 'M') monsterMode = !monsterMode;
146
- if (key === 'g' || key === 'G') currentMonster = 'Godzilla';
147
- if (key === 'r' || key === 'R') currentMonster = 'GiantRobot';
148
- if (key === 't' || key === 'T') train.dir *= -1;
 
 
 
 
 
 
 
 
 
 
 
 
149
  }
150
 
151
- window.toggleMonsterMode = function() { monsterMode = !monsterMode; };
152
- window.setMonster = function(monster) { currentMonster = monster; };
153
- window.reverseTrain = function() { train.dir *= -1; };
 
 
 
 
 
 
 
 
 
154
  """
155
 
156
  # Full HTML content with embedded p5.js
@@ -193,27 +221,14 @@ html_content = f"""
193
  st.title("SimCity 2000 with Monster Mode")
194
  st.write("Click to place buildings. Use keys: M (monster), G (Godzilla), R (Robot), T (train reverse).")
195
 
196
- # Sidebar with emoji-grouped controls
197
  with st.sidebar:
198
  st.header("Controls")
199
-
200
- # Building Controls
201
  st.subheader("🏑 Buildings")
202
- st.write("Click on the grid to randomly place buildings (Residential, Commercial, Industrial, School, Power Plant).")
203
-
204
- # Monster Controls
205
  st.subheader("πŸ‘Ύ Monsters")
206
- if st.button("Toggle Monster Mode (M)", key="monster_toggle"):
207
- st.markdown('<script>toggleMonster()</script>', unsafe_allow_html=True)
208
- monster_choice = st.selectbox("Choose Monster", ["Godzilla (G)", "Giant Robot (R)"], index=0)
209
- if monster_choice.startswith("Godzilla"):
210
- st.markdown('<script>setMonster("Godzilla")</script>', unsafe_allow_html=True)
211
- else:
212
- st.markdown('<script>setMonster("GiantRobot")</script>', unsafe_allow_html=True)
213
-
214
- # Train Controls
215
  st.subheader("πŸš‚ Train")
216
- if st.button("Reverse Train (T)", key="train_reverse"):
217
- st.markdown('<script>reverseTrain()</script>', unsafe_allow_html=True)
218
 
219
  html(html_content, height=700)
 
1
  import streamlit as st
2
  from streamlit.components.v1 import html
3
 
4
+ # Define the p5.js sketch code as a string
5
  p5js_code = """
6
  let gridSize = 40;
7
  let grid = [];
 
11
  let currentMonster = 'Godzilla';
12
  let monsterX, monsterY;
13
  let angle = PI / 6; // 3D perspective angle
14
+ let debugText = "Loading...";
15
 
16
  function setup() {
17
  createCanvas(800, 600);
 
28
  train.y = 5 * gridSize;
29
  monsterX = width / 2;
30
  monsterY = height / 2;
31
+ debugText = "Setup complete. Click to build, use M/G/R/T keys.";
32
  }
33
 
34
  function draw() {
 
37
  drawBuildings();
38
  drawTrain();
39
  if (monsterMode) drawMonster();
40
+ fill(0);
41
+ text(debugText, 10, 20); // Debug feedback
42
  }
43
 
44
  function drawGrid() {
 
46
  for (let j = 0; j < grid[i].length; j++) {
47
  let x = i * gridSize;
48
  let y = j * gridSize;
49
+ let z = j * gridSize * sin(angle);
50
  fill(grid[i][j] === 'track' ? 100 : 150, 200, 150);
51
  stroke(0);
52
  beginShape();
 
67
  fill(b.color);
68
  noStroke();
69
  beginShape();
70
+ if (b.type === 'Residential') {
71
  vertex(x + 10, y + 30 - z); vertex(x + 20, y + 10 - z); vertex(x + 30, y + 30 - z);
72
  vertex(x + 25, y + 30 - z); vertex(x + 25, y + 35 - z); vertex(x + 15, y + 35 - z);
73
  vertex(x + 15, y + 30 - z); vertex(x + 17, y + 25 - z); vertex(x + 23, y + 25 - z);
74
+ } else if (b.type === 'Commercial') {
75
  vertex(x + 10, y + 35 - z); vertex(x + 15, y + 15 - z); vertex(x + 25, y + 15 - z);
76
  vertex(x + 30, y + 35 - z); vertex(x + 27, y + 35 - z); vertex(x + 27, y + 20 - z);
77
  vertex(x + 13, y + 20 - z); vertex(x + 13, y + 35 - z);
78
+ } else if (b.type === 'Industrial') {
79
  vertex(x + 5, y + 35 - z); vertex(x + 15, y + 20 - z); vertex(x + 25, y + 20 - z);
80
  vertex(x + 35, y + 35 - z); vertex(x + 30, y + 35 - z); vertex(x + 30, y + 15 - z);
81
  vertex(x + 33, y + 15 - z); vertex(x + 33, y + 35 - z);
82
+ } else if (b.type === 'School') {
83
  vertex(x + 5, y + 35 - z); vertex(x + 10, y + 20 - z); vertex(x + 30, y + 20 - z);
84
  vertex(x + 35, y + 35 - z); vertex(x + 25, y + 35 - z); vertex(x + 25, y + 10 - z);
85
  vertex(x + 27, y + 10 - z); vertex(x + 27, y + 35 - z);
86
+ } else if (b.type === 'PowerPlant') {
87
  vertex(x + 5, y + 35 - z); vertex(x + 15, y + 15 - z); vertex(x + 25, y + 15 - z);
88
  vertex(x + 35, y + 35 - z); vertex(x + 30, y + 35 - z); vertex(x + 30, y + 25 - z);
89
  vertex(x + 20, y + 25 - z); vertex(x + 20, y + 35 - z);
 
113
  fill(255, 0, 0);
114
  noStroke();
115
  beginShape();
116
+ if (currentMonster === 'Godzilla') {
117
  vertex(monsterX, monsterY + 40 - mz); vertex(monsterX + 10, monsterY + 20 - mz);
118
  vertex(monsterX + 20, monsterY - mz); vertex(monsterX + 30, monsterY + 20 - mz);
119
  vertex(monsterX + 40, monsterY + 40 - mz); vertex(monsterX + 35, monsterY + 50 - mz);
120
  vertex(monsterX + 25, monsterY + 60 - mz); vertex(monsterX + 15, monsterY + 50 - mz);
121
  vertex(monsterX + 20, monsterY + 40 - mz); vertex(monsterX + 25, monsterY + 30 - mz);
122
+ } else if (currentMonster === 'GiantRobot') {
123
  vertex(monsterX, monsterY + 40 - mz); vertex(monsterX + 10, monsterY + 20 - mz);
124
  vertex(monsterX + 15, monsterY - mz); vertex(monsterX + 25, monsterY - mz);
125
  vertex(monsterX + 30, monsterY + 20 - mz); vertex(monsterX + 40, monsterY + 40 - mz);
 
136
  function mousePressed() {
137
  let i = floor(mouseX / gridSize);
138
  let j = floor(mouseY / gridSize);
139
+ if (i >= 0 && i < grid.length && j >= 0 && j < grid[0].length) {
140
+ if (grid[i][j] === 'empty' && !monsterMode) {
141
+ let types = ['Residential', 'Commercial', 'Industrial', 'School', 'PowerPlant'];
142
+ let colors = [[0, 200, 0], [0, 0, 200], [200, 200, 0], [200, 0, 200], [100, 100, 100]];
143
+ let idx = floor(random(5));
144
+ buildings.push({ x: i, y: j, type: types[idx], color: colors[idx] });
145
+ grid[i][j] = 'building';
146
+ debugText = `Placed ${types[idx]} at (${i}, ${j})`;
147
+ }
148
  }
149
  }
150
 
151
  function keyPressed() {
152
+ if (key === 'm' || key === 'M') {
153
+ monsterMode = !monsterMode;
154
+ debugText = `Monster Mode: ${monsterMode}`;
155
+ }
156
+ if (key === 'g' || key === 'G') {
157
+ currentMonster = 'Godzilla';
158
+ debugText = "Monster set to Godzilla";
159
+ }
160
+ if (key === 'r' || key === 'R') {
161
+ currentMonster = 'GiantRobot';
162
+ debugText = "Monster set to Giant Robot";
163
+ }
164
+ if (key === 't' || key === 'T') {
165
+ train.dir *= -1;
166
+ debugText = "Train direction reversed";
167
+ }
168
  }
169
 
170
+ window.toggleMonsterMode = function() {
171
+ monsterMode = !monsterMode;
172
+ debugText = `Monster Mode: ${monsterMode}`;
173
+ };
174
+ window.setMonster = function(monster) {
175
+ currentMonster = monster;
176
+ debugText = `Monster set to ${monster}`;
177
+ };
178
+ window.reverseTrain = function() {
179
+ train.dir *= -1;
180
+ debugText = "Train direction reversed";
181
+ };
182
  """
183
 
184
  # Full HTML content with embedded p5.js
 
221
  st.title("SimCity 2000 with Monster Mode")
222
  st.write("Click to place buildings. Use keys: M (monster), G (Godzilla), R (Robot), T (train reverse).")
223
 
224
+ # Sidebar with emoji-grouped info (no JS calls)
225
  with st.sidebar:
226
  st.header("Controls")
 
 
227
  st.subheader("🏑 Buildings")
228
+ st.write("Click to randomly place: Residential, Commercial, Industrial, School, Power Plant")
 
 
229
  st.subheader("πŸ‘Ύ Monsters")
230
+ st.write("M: Toggle Monster Mode\nG: Godzilla\nR: Giant Robot")
 
 
 
 
 
 
 
 
231
  st.subheader("πŸš‚ Train")
232
+ st.write("T: Reverse Train Direction")
 
233
 
234
  html(html_content, height=700)