smurfs / src /main.py
BladeSzaSza's picture
Update src/main.py
1132acb verified
"""
Main Entry Point - Gradio UI
"""
import gradio as gr
from game_world import SmurfWorld
from entities import LifeStage, BuildingType
from ai_behavior import Personality
from config import FPS
from logging_config import setup_logging
# Configure logging (enabled/disabled via config.LOGGING_ENABLED)
setup_logging()
class SmurfGame:
def __init__(self):
self.world = SmurfWorld()
self.running = True
def game_loop(self):
if self.running:
self.world.update()
return self.world.render()
def toggle_pause(self):
self.running = not self.running
return "Resume" if not self.running else "Pause"
def spawn_new_smurf(self):
smurf = self.world.spawn_smurf(life_stage=LifeStage.ADULT)
return f"Spawned {smurf.smurf_name}! Total: {len([s for s in self.world.smurfs if s.is_alive])}"
def get_stats(self) -> str:
alive = [s for s in self.world.smurfs if s.is_alive]
stats = f"""### Village Statistics
**Population:**
- πŸ‘₯ Alive: {len(alive)}
- πŸ‘Ά Babies: {len([s for s in alive if s.life_stage == LifeStage.BABY])}
- πŸ§’ Children: {len([s for s in alive if s.life_stage == LifeStage.CHILD])}
- πŸ§‘ Adults: {len([s for s in alive if s.life_stage == LifeStage.ADULT])}
- πŸ‘΄ Elders: {len([s for s in alive if s.life_stage == LifeStage.ELDER])}
- πŸͺ¦ Died: {len(self.world.graves)}
**Personalities:**
- πŸ”¨ Workers: {len([s for s in alive if s.personality == Personality.WORKER])}
- πŸ—ΊοΈ Explorers: {len([s for s in alive if s.personality == Personality.EXPLORER])}
- 😴 Lazy: {len([s for s in alive if s.personality == Personality.LAZY])}
- 🀝 Social: {len([s for s in alive if s.personality == Personality.SOCIAL])}
**Resources:**
- πŸ„ Mushrooms: {len([r for r in self.world.resources if r.resource_type == 'mushroom' and not r.is_depleted()])}
- 🫐 Berries:{len([r for r in self.world.resources if r.resource_type == 'berry' and not r.is_depleted()])}
- πŸͺ΅ Wood: {len([r for r in self.world.resources if r.resource_type == 'wood' and not r.is_depleted()])}
**Buildings:**
- Houses: {len([b for b in self.world.buildings if b.building_type == BuildingType.HOUSE])}
- Storage: {len([b for b in self.world.buildings if b.building_type == BuildingType.STORAGE])}
- Workshops: {len([b for b in self.world.buildings if b.building_type == BuildingType.WORKSHOP])}
- Town Hall: {len([b for b in self.world.buildings if b.building_type == BuildingType.TOWNHALL])}
**Village Score:** {self.world.village_score}
"""
return stats
game = SmurfGame()
# UI
with gr.Blocks(title="Smurf Village") as demo:
gr.Markdown("# πŸ„ Smurf Village - Complete Life Simulation")
gr.Markdown("**Watch your Smurfs live, work, socialize, age, and build a thriving village!**")
with gr.Row():
with gr.Column(scale=3):
game_display = gr.Image(label="Smurf Village World", height=600)
timer = gr.Timer(value=1/FPS)
with gr.Row():
pause_btn = gr.Button("Pause", scale=1)
spawn_btn = gr.Button("Spawn Adult Smurf", scale=1)
with gr.Column(scale=1):
status = gr.Textbox(label="Status", value="Village starting...")
stats_display = gr.Markdown(game.get_stats())
gr.Markdown("""
### Enjoy watching your village grow! πŸ„
""")
# Events
timer.tick(fn=game.game_loop, outputs=game_display, show_progress="hidden")
pause_btn.click(fn=game.toggle_pause, outputs=pause_btn)
spawn_btn.click(fn=game.spawn_new_smurf, outputs=status)
stats_timer = gr.Timer(value=2)
stats_timer.tick(fn=game.get_stats, outputs=stats_display, show_progress="hidden")
if __name__ == "__main__":
demo.launch()