Spaces:
Sleeping
Sleeping
File size: 5,849 Bytes
28e4a07 7c0109d 28e4a07 41f3637 28e4a07 93d90c5 28e4a07 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | 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; }
"""
demo = 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
)
demo.launch(server_name="0.0.0.0", server_port=7860)
|