Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,46 +1,51 @@
|
|
1 |
import gradio as gr
|
2 |
|
3 |
def snake_game():
|
4 |
-
|
5 |
-
|
6 |
-
|
|
|
|
|
|
|
7 |
<script>
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
food.x
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
|
|
40 |
</script>
|
41 |
"""
|
42 |
|
43 |
with gr.Blocks() as demo:
|
44 |
-
gr.HTML(snake_game())
|
45 |
|
46 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
def snake_game():
|
4 |
+
return """
|
5 |
+
<style>
|
6 |
+
body { margin: 0; padding: 0; }
|
7 |
+
#gameCanvas { border: 1px solid black; }
|
8 |
+
</style>
|
9 |
+
<canvas id="gameCanvas" width="400" height="400"></canvas>
|
10 |
<script>
|
11 |
+
let canvas = document.getElementById('gameCanvas');
|
12 |
+
let ctx = canvas.getContext('2d');
|
13 |
+
let snake = [{x: 10, y: 10}];
|
14 |
+
let food = {x: 15, y: 15};
|
15 |
+
let gameLoop;
|
16 |
+
|
17 |
+
function init() {
|
18 |
+
gameLoop = setInterval(game, 100);
|
19 |
+
}
|
20 |
+
|
21 |
+
function game() {
|
22 |
+
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
23 |
+
snake.unshift({x: snake[0].x, y: snake[0].y});
|
24 |
+
snake[0].x += 1; // Simple movement for demonstration
|
25 |
+
if(snake[0].x > 39) snake[0].x = 0; // Wrap around
|
26 |
+
|
27 |
+
ctx.fillStyle = "green";
|
28 |
+
snake.forEach(part => {
|
29 |
+
ctx.fillRect(part.x * 10, part.y * 10, 10, 10);
|
30 |
+
});
|
31 |
+
|
32 |
+
ctx.fillStyle = "red";
|
33 |
+
ctx.fillRect(food.x * 10, food.y * 10, 10, 10);
|
34 |
+
|
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 |
"""
|
47 |
|
48 |
with gr.Blocks() as demo:
|
49 |
+
gr.HTML(snake_game(), elem_id="snake-game-block")
|
50 |
|
51 |
demo.launch()
|