broadfield-dev commited on
Commit
33b52f1
·
verified ·
1 Parent(s): a2201d6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -37
app.py CHANGED
@@ -6,6 +6,43 @@ def update_ui(code):
6
  return f"""
7
  <div style="border: 1px solid #ccc; padding: 10px; height: 400px; overflow: auto;">
8
  {code}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  </div>
10
  """, None
11
  except Exception as e:
@@ -26,7 +63,7 @@ def interface():
26
  inputs=code_input,
27
  outputs=[ui_preview, error_output]
28
  )
29
-
30
  # Add example section
31
  with gr.Accordion("HTML/CSS/JS Code Examples", open=False):
32
  with gr.Column():
@@ -63,42 +100,6 @@ def interface():
63
  #gameCanvas { border: 1px solid black; }
64
  </style>
65
  <canvas id="gameCanvas" width="400" height="400"></canvas>
66
- <script>
67
- let canvas = document.getElementById('gameCanvas');
68
- let ctx = canvas.getContext('2d');
69
- let snake = [{x: 10, y: 10}];
70
- let food = {x: 15, y: 15};
71
- let gameLoop;
72
-
73
- function init() {
74
- gameLoop = setInterval(game, 100);
75
- }
76
-
77
- function game() {
78
- ctx.clearRect(0, 0, canvas.width, canvas.height);
79
- snake.unshift({x: snake[0].x, y: snake[0].y});
80
- snake[0].x += 1; // Simple movement for demonstration
81
- if(snake[0].x > 39) snake[0].x = 0; // Wrap around
82
-
83
- ctx.fillStyle = "green";
84
- snake.forEach(part => {
85
- ctx.fillRect(part.x * 10, part.y * 10, 10, 10);
86
- });
87
-
88
- ctx.fillStyle = "red";
89
- ctx.fillRect(food.x * 10, food.y * 10, 10, 10);
90
-
91
- if(snake[0].x == food.x && snake[0].y == food.y) {
92
- food.x = Math.floor(Math.random() * 40);
93
- food.y = Math.floor(Math.random() * 40);
94
- } else {
95
- snake.pop();
96
- }
97
- }
98
-
99
- // Start the game
100
- init();
101
- </script>
102
  """, "Snake Game"),
103
  ]
104
 
 
6
  return f"""
7
  <div style="border: 1px solid #ccc; padding: 10px; height: 400px; overflow: auto;">
8
  {code}
9
+ <script>
10
+ // Ensure the game reinitializes when the code is updated
11
+ if (typeof gameLoop !== 'undefined') {{
12
+ clearInterval(gameLoop);
13
+ }}
14
+ let canvas = document.getElementById('gameCanvas');
15
+ let ctx = canvas.getContext('2d');
16
+ let snake = [{x: 10, y: 10}];
17
+ let food = {{x: 15, y: 15}};
18
+ let gameLoop;
19
+
20
+ function init() {{
21
+ gameLoop = setInterval(game, 100);
22
+ }}
23
+
24
+ function game() {{
25
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
26
+ snake.unshift({{x: snake[0].x, y: snake[0].y}});
27
+ snake[0].x += 1; // Simple movement for demonstration
28
+ if(snake[0].x > 39) snake[0].x = 0; // Wrap around
29
+ ctx.fillStyle = "green";
30
+ snake.forEach(part => {{
31
+ ctx.fillRect(part.x * 10, part.y * 10, 10, 10);
32
+ }});
33
+ ctx.fillStyle = "red";
34
+ ctx.fillRect(food.x * 10, food.y * 10, 10, 10);
35
+ if(snake[0].x == food.x && snake[0].y == food.y) {{
36
+ food.x = Math.floor(Math.random() * 40);
37
+ food.y = Math.floor(Math.random() * 40);
38
+ }} else {{
39
+ snake.pop();
40
+ }}
41
+ }}
42
+
43
+ // Start the game
44
+ init();
45
+ </script>
46
  </div>
47
  """, None
48
  except Exception as e:
 
63
  inputs=code_input,
64
  outputs=[ui_preview, error_output]
65
  )
66
+
67
  # Add example section
68
  with gr.Accordion("HTML/CSS/JS Code Examples", open=False):
69
  with gr.Column():
 
100
  #gameCanvas { border: 1px solid black; }
101
  </style>
102
  <canvas id="gameCanvas" width="400" height="400"></canvas>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
  """, "Snake Game"),
104
  ]
105