Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -6,48 +6,6 @@ def update_ui(code):
|
|
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 |
-
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
|
53 |
except Exception as e:
|
@@ -105,6 +63,39 @@ def interface():
|
|
105 |
#gameCanvas { border: 1px solid black; }
|
106 |
</style>
|
107 |
<canvas id="gameCanvas" width="400" height="400"></canvas>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
""", "Snake Game"),
|
109 |
]
|
110 |
|
|
|
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:
|
|
|
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 |
+
ctx.fillStyle = "green";
|
83 |
+
snake.forEach(part => {
|
84 |
+
ctx.fillRect(part.x * 10, part.y * 10, 10, 10);
|
85 |
+
});
|
86 |
+
ctx.fillStyle = "red";
|
87 |
+
ctx.fillRect(food.x * 10, food.y * 10, 10, 10);
|
88 |
+
if(snake[0].x == food.x && snake[0].y == food.y) {
|
89 |
+
food.x = Math.floor(Math.random() * 40);
|
90 |
+
food.y = Math.floor(Math.random() * 40);
|
91 |
+
} else {
|
92 |
+
snake.pop();
|
93 |
+
}
|
94 |
+
}
|
95 |
+
|
96 |
+
// Start the game
|
97 |
+
init();
|
98 |
+
</script>
|
99 |
""", "Snake Game"),
|
100 |
]
|
101 |
|