Spaces:
Runtime error
Runtime error
Add 2 files
Browse files
README.md
CHANGED
|
@@ -1,11 +1,10 @@
|
|
| 1 |
---
|
|
|
|
| 2 |
title: Snake Game
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
---
|
| 10 |
-
|
| 11 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
title: Snake Game
|
| 4 |
+
sdk: gradio
|
| 5 |
+
sdk_version: 3.39.0
|
| 6 |
+
app_file: app.py
|
| 7 |
+
emoji: π
|
| 8 |
+
colorFrom: green
|
| 9 |
+
colorTo: blue
|
| 10 |
+
---
|
|
|
|
|
|
app.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
|
| 4 |
+
# Define the game board as a 2D numpy array
|
| 5 |
+
board = np.zeros((20, 20))
|
| 6 |
+
|
| 7 |
+
# Define the snake as a list of coordinates
|
| 8 |
+
snake = [(10, 10)]
|
| 9 |
+
|
| 10 |
+
# Define the food as a list of coordinates
|
| 11 |
+
food = [(15, 15)]
|
| 12 |
+
|
| 13 |
+
def make_board():
|
| 14 |
+
board = np.zeros((20, 20))
|
| 15 |
+
for snake_pos in snake:
|
| 16 |
+
board[snake_pos] = 1
|
| 17 |
+
for food_pos in food:
|
| 18 |
+
board[food_pos] = 2
|
| 19 |
+
return board
|
| 20 |
+
|
| 21 |
+
def update_board():
|
| 22 |
+
global board, snake, food
|
| 23 |
+
|
| 24 |
+
# Move the snake in the direction of the arrow keys
|
| 25 |
+
if gr.inputs.arrow_up:
|
| 26 |
+
snake.append((snake[-1][0], snake[-1][1]-1))
|
| 27 |
+
if gr.inputs.arrow_down:
|
| 28 |
+
snake.append((snake[-1][0], snake[-1][1]+1))
|
| 29 |
+
if gr.inputs.arrow_left:
|
| 30 |
+
snake.append((snake[-1][0]-1, snake[-1][1]))
|
| 31 |
+
if gr.inputs.arrow_right:
|
| 32 |
+
snake.append((snake[-1][0]+1, snake[-1][1]))
|
| 33 |
+
|
| 34 |
+
# Update the food location
|
| 35 |
+
food = [(15, 15)]
|
| 36 |
+
|
| 37 |
+
# Update the board
|
| 38 |
+
board = make_board()
|
| 39 |
+
gr.outputs.board = board
|
| 40 |
+
|
| 41 |
+
gr.Interface("Snake Game", update_board, inputs=[
|
| 42 |
+
gr.Input(gr.Slider(0, 20, default_value=0), description="X"),
|
| 43 |
+
gr.Input(gr.Slider(0, 20, default_value=0), description="Y"),
|
| 44 |
+
gr.Input(gr.Slider(0, 20, default_value=0), description="Width"),
|
| 45 |
+
gr.Input(gr.Slider(0, 20, default_value=0), description="Height"),
|
| 46 |
+
gr.Input(gr.Button("Reset"), description="Reset"),
|
| 47 |
+
])
|
| 48 |
+
|
| 49 |
+
gr.Interface("Snake Game", update_board, inputs=[
|
| 50 |
+
gr.Input(gr.Slider(0, 20, default_value=0), description="X"),
|
| 51 |
+
gr.Input(gr.Slider(0, 20, default_value=0), description="Y"),
|
| 52 |
+
gr.Input(gr.Slider(0, 20, default_value=0), description="Width"),
|
| 53 |
+
gr.Input(gr.Slider(0, 20, default_value=0), description="Height"),
|
| 54 |
+
gr.Input(gr.Button("Reset"), description="Reset"),
|
| 55 |
+
]).launch()
|