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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -29
app.py CHANGED
@@ -8,40 +8,45 @@ def update_ui(code):
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
 
8
  {code}
9
  <script>
10
  // Ensure the game reinitializes when the code is updated
11
+ function reinitializeGame() {{
12
+ if (typeof gameLoop !== 'undefined') {{
13
+ clearInterval(gameLoop);
14
+ }}
15
+ let canvas = document.getElementById('gameCanvas');
16
+ let ctx = canvas.getContext('2d');
17
+ let snake = [{x: 10, y: 10}];
18
+ let food = {{x: 15, y: 15}};
19
+ let gameLoop;
20
 
21
+ function init() {{
22
+ gameLoop = setInterval(game, 100);
23
+ }}
24
 
25
+ function game() {{
26
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
27
+ snake.unshift({{x: snake[0].x, y: snake[0].y}});
28
+ snake[0].x += 1; // Simple movement for demonstration
29
+ if(snake[0].x > 39) snake[0].x = 0; // Wrap around
30
+ ctx.fillStyle = "green";
31
+ snake.forEach(part => {{
32
+ ctx.fillRect(part.x * 10, part.y * 10, 10, 10);
33
+ }});
34
+ ctx.fillStyle = "red";
35
+ ctx.fillRect(food.x * 10, food.y * 10, 10, 10);
36
+ if(snake[0].x == food.x && snake[0].y == food.y) {{
37
+ food.x = Math.floor(Math.random() * 40);
38
+ food.y = Math.floor(Math.random() * 40);
39
+ }} else {{
40
+ snake.pop();
41
+ }}
42
  }}
43
+
44
+ // Start the game
45
+ init();
46
  }}
47
 
48
+ // Call the reinitialize function to start the game
49
+ reinitializeGame();
50
  </script>
51
  </div>
52
  """, None