Rashi-jan commited on
Commit
ef28c92
·
verified ·
1 Parent(s): 48b2a38

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +105 -0
app.py ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ # ----------------- SNAKE GAME HTML + JS ----------------- #
4
+ snake_game_html = """
5
+ <style>
6
+ #gameCanvas {
7
+ background-color: #222;
8
+ display: block;
9
+ margin: 1em auto;
10
+ border: 3px solid white;
11
+ border-radius: 8px;
12
+ }
13
+ #instructions {
14
+ text-align: center;
15
+ color: white;
16
+ font-family: Arial, sans-serif;
17
+ margin-top: 0.5em;
18
+ }
19
+ </style>
20
+
21
+ <canvas id="gameCanvas" width="400" height="400"></canvas>
22
+ <div id="instructions">Use Arrow Keys to move the snake. Eat the red squares!</div>
23
+
24
+ <script>
25
+ const canvas = document.getElementById('gameCanvas');
26
+ const ctx = canvas.getContext('2d');
27
+
28
+ const grid = 20;
29
+ let count = 0;
30
+ let snake = { x: 160, y: 160, dx: grid, dy: 0, cells: [], maxCells: 4 };
31
+ let apple = { x: 320, y: 320 };
32
+ let score = 0;
33
+
34
+ function getRandomInt(min, max) {
35
+ return Math.floor(Math.random() * (max - min)) + min;
36
+ }
37
+
38
+ document.addEventListener('keydown', function(e) {
39
+ if (e.key === 'ArrowLeft' && snake.dx === 0) { snake.dx = -grid; snake.dy = 0; }
40
+ if (e.key === 'ArrowUp' && snake.dy === 0) { snake.dx = 0; snake.dy = -grid; }
41
+ if (e.key === 'ArrowRight' && snake.dx === 0) { snake.dx = grid; snake.dy = 0; }
42
+ if (e.key === 'ArrowDown' && snake.dy === 0) { snake.dx = 0; snake.dy = grid; }
43
+ });
44
+
45
+ function gameLoop() {
46
+ requestAnimationFrame(gameLoop);
47
+ if (++count < 4) return;
48
+ count = 0;
49
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
50
+
51
+ snake.x += snake.dx;
52
+ snake.y += snake.dy;
53
+
54
+ // Wrap snake position
55
+ if (snake.x < 0) snake.x = canvas.width - grid;
56
+ if (snake.x >= canvas.width) snake.x = 0;
57
+ if (snake.y < 0) snake.y = canvas.height - grid;
58
+ if (snake.y >= canvas.height) snake.y = 0;
59
+
60
+ snake.cells.unshift({x: snake.x, y: snake.y});
61
+ if (snake.cells.length > snake.maxCells) snake.cells.pop();
62
+
63
+ // Draw apple
64
+ ctx.fillStyle = 'red';
65
+ ctx.fillRect(apple.x, apple.y, grid-1, grid-1);
66
+
67
+ // Draw snake
68
+ ctx.fillStyle = 'lime';
69
+ snake.cells.forEach((cell, index) => {
70
+ ctx.fillRect(cell.x, cell.y, grid-1, grid-1);
71
+
72
+ // Check collision with apple
73
+ if (cell.x === apple.x && cell.y === apple.y) {
74
+ snake.maxCells++;
75
+ score++;
76
+ apple.x = getRandomInt(0, 20) * grid;
77
+ apple.y = getRandomInt(0, 20) * grid;
78
+ }
79
+
80
+ // Check collision with self
81
+ for (let i = index + 1; i < snake.cells.length; i++) {
82
+ if (cell.x === snake.cells[i].x && cell.y === snake.cells[i].y) {
83
+ alert("Game Over! Score: " + score);
84
+ document.location.reload();
85
+ }
86
+ }
87
+ });
88
+
89
+ // Draw score
90
+ ctx.fillStyle = 'white';
91
+ ctx.font = '16px Arial';
92
+ ctx.fillText('Score: ' + score, 10, 20);
93
+ }
94
+
95
+ requestAnimationFrame(gameLoop);
96
+ </script>
97
+ """
98
+
99
+ # ----------------- GRADIO UI ----------------- #
100
+ with gr.Blocks() as demo:
101
+ gr.Markdown("## 🐍 Snake Game")
102
+ gr.Markdown("Control the snake with arrow keys and eat the red apples to grow!")
103
+ gr.HTML(snake_game_html)
104
+
105
+ demo.launch()