Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import os | |
| from crewai import Agent, LLM, Task, Crew, Process | |
| from crewai_tools import SerperDevTool | |
| serper_api_key = os.getenv("SERPER_API_KEY") | |
| llm=LLM( | |
| model="gemini/gemini-2.5-flash", | |
| api_key=os.getenv("Gemini_API_key") | |
| ) | |
| search_tool = SerperDevTool(api_key=serper_api_key) | |
| game_desiner = Agent( | |
| role="Creative Game Designer", | |
| goal="Come up with fun, feasible game concepts and detailed mechanics based on user idea", | |
| backstory=( | |
| "You are an experienced game designer." | |
| "You excel at turning vague ideas into clear, exciting game designs including:" | |
| "- core loop, rules, win/lose conditions" | |
| "- basic entities (player, enemies, items)" | |
| "- controls and feel" | |
| "Keep it simple enough to implement in pure Python + Pygame in one file." | |
| ), | |
| llm=llm, | |
| verbose=True, | |
| ) | |
| senior_python_developer = Agent( | |
| role="Senior Python Game Developer", | |
| goal="Write clean, working Python code (using Pygame) for the described game", | |
| backstory=( | |
| "You are a senior software engineer specialized in Python game development with Pygame." | |
| "You write structured, readable code with:" | |
| "- Proper game loop, event handling, drawing" | |
| "- Comments explaining key parts" | |
| "- Error handling where needed" | |
| "You always produce a complete, runnable .py file." | |
| ), | |
| llm=llm, | |
| tools=[search_tool], | |
| verbose=True, | |
| ) | |
| qa_engineer = Agent( | |
| role="QA Engineer & Code Reviewer", | |
| goal="Test, review, and improve the code for bugs, playability, and completeness", | |
| backstory=( | |
| "You are a meticulous QA engineer and code reviewer." | |
| "You carefully check:" | |
| "- Does the code run without errors?" | |
| "- Does it implement ALL the designed features?" | |
| "- Is it fun/playable? Any obvious balance issues?" | |
| "- Code style, variable names, comments" | |
| "Suggest fixes or small improvements and output the FINAL improved code." | |
| ), | |
| llm=llm, | |
| verbose=True, | |
| ) | |
| task_design = Task( | |
| description=( | |
| "Take the user's game idea: {game_idea}" | |
| "1. Clarify and expand it into a fun, simple 2D game" | |
| "2. Describe: objective, controls, entities, win/lose" | |
| "3. Keep scope small (one level, basic mechanics)" | |
| "Output format:" | |
| "## Game Design Document\n" | |
| "- Title: ...\n" | |
| "- Genre: ...\n" | |
| "- Objective: ...\n" | |
| "- Controls: ...\n" | |
| "- Entities: ...\n" | |
| "- Mechanics: ..." | |
| ), | |
| expected_output="A clear markdown game design document", | |
| agent=game_desiner, | |
| ) | |
| task_code = Task( | |
| description=( | |
| "Using the game design from the previous task" | |
| "Write a COMPLETE, standalone Python script using Pygame that implements the game." | |
| "- Include import pygame, sys, random (if needed)" | |
| "- Full game loop, init, events, update, draw" | |
| "- Make it runnable with python game.py" | |
| "- Add simple comments" | |
| "- The main game loop must be exposed in the python code, it should not be inside any function like main" | |
| "- Final answer MUST be ONLY the Python code and Instructions on how to play the game" | |
| ), | |
| expected_output="A complete runnable python script", | |
| agent=senior_python_developer, | |
| ) | |
| task_review = Task( | |
| description=( | |
| "Review the Python code from the previous task." | |
| "1. Check for syntax/runtime errors" | |
| "2. Verify it matches the design document" | |
| "3. Test mentally: does it have init, loop, quit handling, drawing?" | |
| "4. Suggest fixes/improvements if needed" | |
| "5. Output the FINAL, improved, ready-to-run code" | |
| "Your final answer MUST be ONLY the complete Python code along with the instructions on how to play the game" | |
| ), | |
| expected_output="Final Polished, runnable pygame python script and instructions on how to play the game", | |
| agent=qa_engineer, | |
| context=[task_design, task_code], | |
| ) | |
| game_crew = Crew( | |
| agents=[game_desiner, senior_python_developer, qa_engineer], | |
| tasks=[task_design, task_code, task_review], | |
| process=Process.sequential, | |
| verbose=True, | |
| ) | |
| async def generate_game_code(game_idea: str) -> str: | |
| """Generates game code based on the provided game idea using CrewAI.""" | |
| if not game_idea: | |
| return "Please enter a game idea to generate code." | |
| try: | |
| result = await game_crew.kickoff_async(inputs={"game_idea": game_idea}) | |
| return result | |
| except Exception as e: | |
| return f"An error occurred: {e}" | |
| custom_css = """ | |
| body { background-color: #1a1a2e; color: #e0e0e0; } | |
| .gradio-container { background-color: #16213e; border-radius: 10px; } | |
| .panel-nav { background-color: #0f3460; } | |
| .block-title { color: #e94560; } | |
| .label { color: #e0e0e0; } | |
| .input-textbox textarea { background-color: #0f3460; color: #e0e0e0; border: 1px solid #e94560; } | |
| .output-textbox textarea { background-color: #0f3460; color: #e0e0e0; border: 1px solid #e94560; } | |
| .gr-button { background-color: #e94560; color: white; border: none; padding: 10px 20px; border-radius: 5px; cursor: pointer; transition: background-color 0.3s ease; } | |
| .gr-button:hover { background-color: #ba324d; } | |
| """ | |
| iface = gr.Interface( | |
| fn=generate_game_code, | |
| inputs=gr.Textbox(lines=2, label="Enter your game idea", placeholder="e.g., A fun endless runner where a character jumps over obstacles"), | |
| outputs=gr.Textbox(lines=15, label="Generated Game Code and Instructions"), | |
| title="<h1 style='color:#e94560;'>Game Creator AI</h1>", | |
| description="<p style='color:#e0e0e0;'>Enter your game idea below and let AI design, code, and review a simple Pygame project for you and gives the Python code and Instructions of the Game .</p>", | |
| css=custom_css, | |
| allow_flagging="never" | |
| ) | |
| iface.launch(debug=True, share=True) | |