Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
-
# app.py - Versión corregida
|
| 2 |
import gradio as gr
|
| 3 |
import time
|
| 4 |
import math
|
| 5 |
-
from PIL import Image
|
| 6 |
import io
|
| 7 |
from game_engine import GauchoCharacter, PhysicsEngine, WorldGenerator, Vector2D
|
| 8 |
|
|
@@ -13,33 +13,61 @@ class CosmicGauchoGame:
|
|
| 13 |
self.physics = PhysicsEngine()
|
| 14 |
self.world = WorldGenerator()
|
| 15 |
self.current_chunk = None
|
|
|
|
|
|
|
| 16 |
self.load_initial_world()
|
| 17 |
|
| 18 |
def load_initial_world(self):
|
| 19 |
self.current_chunk = self.world.generate_chunk('pampa', 16)
|
| 20 |
|
| 21 |
-
def update_game_state(self,
|
| 22 |
-
#
|
| 23 |
acceleration = Vector2D(0, 0)
|
|
|
|
| 24 |
|
| 25 |
-
if
|
| 26 |
-
acceleration.x = -
|
| 27 |
-
|
| 28 |
-
acceleration.x =
|
| 29 |
-
|
| 30 |
-
acceleration.y = -
|
| 31 |
-
|
| 32 |
-
acceleration.y =
|
| 33 |
|
| 34 |
self.player.velocity = self.physics.apply_momentum(
|
| 35 |
self.player.velocity,
|
| 36 |
acceleration,
|
| 37 |
-
dt=0.
|
| 38 |
)
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
def render_frame(self) -> Image.Image:
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
return img
|
| 44 |
|
| 45 |
# Clase mejorada con optimizaciones
|
|
@@ -48,62 +76,88 @@ class EnhancedCosmicGaucho(CosmicGauchoGame):
|
|
| 48 |
super().__init__()
|
| 49 |
self.last_frame_time = time.time()
|
| 50 |
|
| 51 |
-
def
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
|
| 59 |
def create_game_interface():
|
| 60 |
game = EnhancedCosmicGaucho()
|
| 61 |
|
| 62 |
-
with gr.Blocks(
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
with gr.Row():
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
|
|
|
| 76 |
with gr.Row():
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
-
gr.Markdown("
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
interactive=False
|
| 85 |
)
|
| 86 |
|
| 87 |
-
def
|
| 88 |
-
game.update_game_state(
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
|
|
|
| 99 |
interface.load(
|
| 100 |
-
fn=
|
| 101 |
-
outputs=[game_display,
|
| 102 |
-
every=0.033
|
| 103 |
)
|
| 104 |
|
| 105 |
return interface
|
| 106 |
|
| 107 |
if __name__ == "__main__":
|
| 108 |
interface = create_game_interface()
|
| 109 |
-
interface.launch(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py - Versión corregida para HuggingFace Spaces
|
| 2 |
import gradio as gr
|
| 3 |
import time
|
| 4 |
import math
|
| 5 |
+
from PIL import Image, ImageDraw
|
| 6 |
import io
|
| 7 |
from game_engine import GauchoCharacter, PhysicsEngine, WorldGenerator, Vector2D
|
| 8 |
|
|
|
|
| 13 |
self.physics = PhysicsEngine()
|
| 14 |
self.world = WorldGenerator()
|
| 15 |
self.current_chunk = None
|
| 16 |
+
self.game_width = 800
|
| 17 |
+
self.game_height = 600
|
| 18 |
self.load_initial_world()
|
| 19 |
|
| 20 |
def load_initial_world(self):
|
| 21 |
self.current_chunk = self.world.generate_chunk('pampa', 16)
|
| 22 |
|
| 23 |
+
def update_game_state(self, movement: str = "none"):
|
| 24 |
+
# Sistema de movimiento simplificado
|
| 25 |
acceleration = Vector2D(0, 0)
|
| 26 |
+
speed = 3.0
|
| 27 |
|
| 28 |
+
if movement == "left":
|
| 29 |
+
acceleration.x = -speed
|
| 30 |
+
elif movement == "right":
|
| 31 |
+
acceleration.x = speed
|
| 32 |
+
elif movement == "up":
|
| 33 |
+
acceleration.y = -speed
|
| 34 |
+
elif movement == "down":
|
| 35 |
+
acceleration.y = speed
|
| 36 |
|
| 37 |
self.player.velocity = self.physics.apply_momentum(
|
| 38 |
self.player.velocity,
|
| 39 |
acceleration,
|
| 40 |
+
dt=0.1
|
| 41 |
)
|
| 42 |
+
|
| 43 |
+
# Actualizar posición con límites de pantalla
|
| 44 |
+
new_x = self.player.position.x + self.player.velocity.x
|
| 45 |
+
new_y = self.player.position.y + self.player.velocity.y
|
| 46 |
+
|
| 47 |
+
self.player.position.x = max(50, min(new_x, self.game_width - 50))
|
| 48 |
+
self.player.position.y = max(50, min(new_y, self.game_height - 50))
|
| 49 |
|
| 50 |
def render_frame(self) -> Image.Image:
|
| 51 |
+
# Crear canvas del juego
|
| 52 |
+
img = Image.new('RGB', (self.game_width, self.game_height), (20, 40, 60))
|
| 53 |
+
draw = ImageDraw.Draw(img)
|
| 54 |
+
|
| 55 |
+
# Dibujar fondo de pampa cósmica
|
| 56 |
+
for i in range(0, self.game_width, 100):
|
| 57 |
+
for j in range(0, self.game_height, 100):
|
| 58 |
+
color = (30 + i//10, 50 + j//10, 80)
|
| 59 |
+
draw.rectangle([i, j, i+90, j+90], fill=color, outline=(100, 150, 200))
|
| 60 |
+
|
| 61 |
+
# Dibujar al gaucho (círculo simple por ahora)
|
| 62 |
+
player_x = int(self.player.position.x)
|
| 63 |
+
player_y = int(self.player.position.y)
|
| 64 |
+
draw.ellipse([player_x-20, player_y-20, player_x+20, player_y+20],
|
| 65 |
+
fill=(255, 200, 100), outline=(200, 150, 50), width=3)
|
| 66 |
+
|
| 67 |
+
# Dibujar sombrero gaucho
|
| 68 |
+
draw.ellipse([player_x-15, player_y-25, player_x+15, player_y-15],
|
| 69 |
+
fill=(100, 50, 0), outline=(80, 40, 0), width=2)
|
| 70 |
+
|
| 71 |
return img
|
| 72 |
|
| 73 |
# Clase mejorada con optimizaciones
|
|
|
|
| 76 |
super().__init__()
|
| 77 |
self.last_frame_time = time.time()
|
| 78 |
|
| 79 |
+
def get_game_status(self):
|
| 80 |
+
return {
|
| 81 |
+
"position": f"({self.player.position.x:.1f}, {self.player.position.y:.1f})",
|
| 82 |
+
"velocity": f"({self.player.velocity.x:.1f}, {self.player.velocity.y:.1f})",
|
| 83 |
+
"cristales": sum(self.player.inventory['cristales'].values()),
|
| 84 |
+
"habilidades": sum(self.player.abilities.values())
|
| 85 |
+
}
|
| 86 |
|
| 87 |
def create_game_interface():
|
| 88 |
game = EnhancedCosmicGaucho()
|
| 89 |
|
| 90 |
+
with gr.Blocks(
|
| 91 |
+
title="🎮 El Gaucho Cósmico - Preservando la Identidad Argentina 🇦🇷",
|
| 92 |
+
theme=gr.themes.Monochrome()
|
| 93 |
+
) as interface:
|
| 94 |
+
|
| 95 |
+
gr.Markdown("""
|
| 96 |
+
# 🌟 El Gaucho Cósmico
|
| 97 |
+
### Aventura Épica de Identidad Cultural Argentina
|
| 98 |
+
*Año 2088 - La Pampa Cósmica te necesita, che!*
|
| 99 |
+
""")
|
| 100 |
|
| 101 |
with gr.Row():
|
| 102 |
+
with gr.Column(scale=2):
|
| 103 |
+
game_display = gr.Image(
|
| 104 |
+
label="🌌 Pampa Cósmica",
|
| 105 |
+
interactive=False,
|
| 106 |
+
width=800,
|
| 107 |
+
height=600
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
with gr.Row():
|
| 111 |
+
left_btn = gr.Button("⬅️ Izquierda", variant="secondary")
|
| 112 |
+
up_btn = gr.Button("⬆️ Arriba", variant="secondary")
|
| 113 |
+
down_btn = gr.Button("⬇️ Abajo", variant="secondary")
|
| 114 |
+
right_btn = gr.Button("➡️ Derecha", variant="secondary")
|
| 115 |
+
|
| 116 |
+
with gr.Column(scale=1):
|
| 117 |
+
gr.Markdown("### 🎯 Estado del Gaucho")
|
| 118 |
+
status_display = gr.JSON(label="📊 Datos del Personaje")
|
| 119 |
|
| 120 |
+
gr.Markdown("### 🗣️ Sabiduría Gaucha")
|
| 121 |
+
wisdom_display = gr.Textbox(
|
| 122 |
+
value="'Che, en esta pampa cósmica, la tradición y la tecnología van de la mano!'",
|
| 123 |
+
label="💭 Reflexión Cultural",
|
| 124 |
interactive=False
|
| 125 |
)
|
| 126 |
|
| 127 |
+
def move_and_update(direction):
|
| 128 |
+
game.update_game_state(direction)
|
| 129 |
+
return game.render_frame(), game.get_game_status()
|
| 130 |
+
|
| 131 |
+
# Conectar botones de movimiento
|
| 132 |
+
left_btn.click(
|
| 133 |
+
fn=lambda: move_and_update("left"),
|
| 134 |
+
outputs=[game_display, status_display]
|
| 135 |
+
)
|
| 136 |
+
right_btn.click(
|
| 137 |
+
fn=lambda: move_and_update("right"),
|
| 138 |
+
outputs=[game_display, status_display]
|
| 139 |
+
)
|
| 140 |
+
up_btn.click(
|
| 141 |
+
fn=lambda: move_and_update("up"),
|
| 142 |
+
outputs=[game_display, status_display]
|
| 143 |
+
)
|
| 144 |
+
down_btn.click(
|
| 145 |
+
fn=lambda: move_and_update("down"),
|
| 146 |
+
outputs=[game_display, status_display]
|
| 147 |
+
)
|
| 148 |
|
| 149 |
+
# Cargar estado inicial
|
| 150 |
interface.load(
|
| 151 |
+
fn=lambda: (game.render_frame(), game.get_game_status()),
|
| 152 |
+
outputs=[game_display, status_display]
|
|
|
|
| 153 |
)
|
| 154 |
|
| 155 |
return interface
|
| 156 |
|
| 157 |
if __name__ == "__main__":
|
| 158 |
interface = create_game_interface()
|
| 159 |
+
interface.launch(
|
| 160 |
+
share=True,
|
| 161 |
+
show_error=True,
|
| 162 |
+
debug=True
|
| 163 |
+
)
|