Spaces:
Running
Running
Update app.py
Browse files
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 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
40 |
}}
|
|
|
|
|
|
|
41 |
}}
|
42 |
|
43 |
-
//
|
44 |
-
|
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
|