import gradio as gr import numpy as np # Define the game board as a 2D numpy array board = np.zeros((20, 20)) # Define the snake as a list of coordinates snake = [(10, 10)] # Define the food as a list of coordinates food = [(15, 15)] def make_board(): board = np.zeros((20, 20)) for snake_pos in snake: board[snake_pos] = 1 for food_pos in food: board[food_pos] = 2 return board def update_board(): global board, snake, food # Move the snake in the direction of the arrow keys if gr.inputs.arrow_up: snake.append((snake[-1][0], snake[-1][1]-1)) if gr.inputs.arrow_down: snake.append((snake[-1][0], snake[-1][1]+1)) if gr.inputs.arrow_left: snake.append((snake[-1][0]-1, snake[-1][1])) if gr.inputs.arrow_right: snake.append((snake[-1][0]+1, snake[-1][1])) # Update the food location food = [(15, 15)] # Update the board board = make_board() gr.outputs.board = board gr.Interface("Snake Game", update_board, inputs=[ gr.Input(gr.Slider(0, 20, default_value=0), description="X"), gr.Input(gr.Slider(0, 20, default_value=0), description="Y"), gr.Input(gr.Slider(0, 20, default_value=0), description="Width"), gr.Input(gr.Slider(0, 20, default_value=0), description="Height"), gr.Input(gr.Button("Reset"), description="Reset"), ]) gr.Interface("Snake Game", update_board, inputs=[ gr.Input(gr.Slider(0, 20, default_value=0), description="X"), gr.Input(gr.Slider(0, 20, default_value=0), description="Y"), gr.Input(gr.Slider(0, 20, default_value=0), description="Width"), gr.Input(gr.Slider(0, 20, default_value=0), description="Height"), gr.Input(gr.Button("Reset"), description="Reset"), ]).launch()