Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
from PIL import Image
|
4 |
+
import random
|
5 |
+
|
6 |
+
# Game state variables
|
7 |
+
ship_position = 50 # Ship's horizontal position (0-100)
|
8 |
+
enemies = [] # List of enemy positions
|
9 |
+
score = 0
|
10 |
+
|
11 |
+
# Function to generate a simple game screen as an image
|
12 |
+
def create_game_screen(ship_pos, enemy_list, score):
|
13 |
+
# Create a blank 200x200 image (black background)
|
14 |
+
img = np.zeros((200, 200, 3), dtype=np.uint8)
|
15 |
+
|
16 |
+
# Draw ship (white rectangle) at the bottom
|
17 |
+
ship_y = 180
|
18 |
+
img[ship_y:ship_y+10, max(0, ship_pos-5):min(200, ship_pos+5)] = [255, 255, 255]
|
19 |
+
|
20 |
+
# Draw enemies (red squares)
|
21 |
+
for enemy_x, enemy_y in enemy_list:
|
22 |
+
img[max(0, enemy_y-5):min(200, enemy_y+5), max(0, enemy_x-5):min(200, enemy_x+5)] = [255, 0, 0]
|
23 |
+
|
24 |
+
# Convert to PIL Image for Gradio
|
25 |
+
return Image.fromarray(img), f"Score: {score}"
|
26 |
+
|
27 |
+
# Function to update game state based on user action
|
28 |
+
def update_game(action):
|
29 |
+
global ship_position, enemies, score
|
30 |
+
|
31 |
+
# Move ship
|
32 |
+
if action == "Move Left" and ship_position > 10:
|
33 |
+
ship_position -= 10
|
34 |
+
elif action == "Move Right" and ship_position < 190:
|
35 |
+
ship_position += 10
|
36 |
+
|
37 |
+
# Shooting logic
|
38 |
+
elif action == "Shoot":
|
39 |
+
for i, (enemy_x, enemy_y) in enumerate(enemies[:]):
|
40 |
+
if abs(enemy_x - ship_position) < 15 and enemy_y > 150: # Rough hit detection
|
41 |
+
enemies.pop(i)
|
42 |
+
score += 10
|
43 |
+
break
|
44 |
+
|
45 |
+
# Spawn new enemies randomly
|
46 |
+
if random.random() < 0.3: # 30% chance to spawn an enemy
|
47 |
+
enemies.append([random.randint(0, 200), 0])
|
48 |
+
|
49 |
+
# Move enemies down
|
50 |
+
for enemy in enemies[:]:
|
51 |
+
enemy[1] += 10
|
52 |
+
if enemy[1] > 200: # Remove enemies that go off-screen
|
53 |
+
enemies.remove(enemy)
|
54 |
+
|
55 |
+
# Generate updated game screen
|
56 |
+
return create_game_screen(ship_position, enemies, score)
|
57 |
+
|
58 |
+
# Reset game state
|
59 |
+
def reset_game():
|
60 |
+
global ship_position, enemies, score
|
61 |
+
ship_position = 50
|
62 |
+
enemies = []
|
63 |
+
score = 0
|
64 |
+
return create_game_screen(ship_position, enemies, score)
|
65 |
+
|
66 |
+
# Gradio interface
|
67 |
+
with gr.Blocks(title="Blastar 1984 Simulator") as demo:
|
68 |
+
gr.Markdown("# Blastar 1984 Simulator")
|
69 |
+
gr.Markdown("A simplified version of Elon Musk's 1984 game. Move your ship and shoot enemies!")
|
70 |
+
|
71 |
+
# Display game screen and score
|
72 |
+
output_image = gr.Image(label="Game Screen")
|
73 |
+
output_text = gr.Textbox(label="Score")
|
74 |
+
|
75 |
+
# Buttons for controls
|
76 |
+
with gr.Row():
|
77 |
+
btn_left = gr.Button("Move Left")
|
78 |
+
btn_right = gr.Button("Move Right")
|
79 |
+
btn_shoot = gr.Button("Shoot")
|
80 |
+
btn_reset = gr.Button("Reset Game")
|
81 |
+
|
82 |
+
# Event handlers
|
83 |
+
btn_left.click(fn=update_game, inputs=gr.State(value="Move Left"), outputs=[output_image, output_text])
|
84 |
+
btn_right.click(fn=update_game, inputs=gr.State(value="Move Right"), outputs=[output_image, output_text])
|
85 |
+
btn_shoot.click(fn=update_game, inputs=gr.State(value="Shoot"), outputs=[output_image, output_text])
|
86 |
+
btn_reset.click(fn=reset_game, inputs=None, outputs=[output_image, output_text])
|
87 |
+
|
88 |
+
# Initial state
|
89 |
+
demo.load(fn=reset_game, inputs=None, outputs=[output_image, output_text])
|
90 |
+
|
91 |
+
# Launch the app (Hugging Face handles this automatically)
|
92 |
+
demo.launch()
|