broadfield-dev commited on
Commit
c94f5bc
·
verified ·
1 Parent(s): 3ab14b8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -36
app.py CHANGED
@@ -1,46 +1,51 @@
1
  import gradio as gr
2
 
3
  def snake_game():
4
- # This function will just return the HTML since no user input is needed for this simple demo
5
- return f"""
6
- <div id="gameCanvas" style="width:400px;height:400px;border:1px solid black;"></div>
 
 
 
7
  <script>
8
- // Insert the JavaScript code here (Remove the script tags as they're not needed within this context)
9
- let canvas = document.getElementById('gameCanvas');
10
- let ctx = canvas.getContext('2d');
11
- let snake = [{x: 10, y: 10}];
12
- let food = {{x: 15, y: 15}};
13
- let gameLoop;
14
-
15
- function init() {{
16
- gameLoop = setInterval(game, 100);
17
- }}
18
-
19
- function game() {{
20
- ctx.clearRect(0, 0, canvas.width, canvas.height);
21
- snake.unshift({{x: snake[0].x, y: snake[0].y}});
22
- snake[0].x += 1;
23
- ctx.fillStyle = "green";
24
- snake.forEach(part => {{
25
- ctx.fillRect(part.x * 10, part.y * 10, 10, 10);
26
- }});
27
-
28
- ctx.fillStyle = "red";
29
- ctx.fillRect(food.x * 10, food.y * 10, 10, 10);
30
-
31
- if(snake[0].x == food.x && snake[0].y == food.y) {{
32
- food.x = Math.floor(Math.random() * 40);
33
- food.y = Math.floor(Math.random() * 40);
34
- }} else {{
35
- snake.pop();
36
- }}
37
- }}
38
-
39
- init();
 
 
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()