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|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
from crewai import Agent, LLM, Task, Crew, Process
|
| 4 |
+
from crewai_tools import SerperDevTool
|
| 5 |
+
|
| 6 |
+
serper_api_key = os.getenv("SERPER_API_KEY")
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
llm=LLM(
|
| 10 |
+
model="gemini/gemini-2.5-flash",
|
| 11 |
+
api_key=os.getenv("Gemini_API_key")
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
search_tool = SerperDevTool(api_key=serper_api_key)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
game_desiner = Agent(
|
| 19 |
+
role="Creative Game Designer",
|
| 20 |
+
goal="Come up with fun, feasible game concepts and detailed mechanics based on user idea",
|
| 21 |
+
backstory=(
|
| 22 |
+
"You are an experienced game designer."
|
| 23 |
+
"You excel at turning vague ideas into clear, exciting game designs including:"
|
| 24 |
+
"- core loop, rules, win/lose conditions"
|
| 25 |
+
"- basic entities (player, enemies, items)"
|
| 26 |
+
"- controls and feel"
|
| 27 |
+
"Keep it simple enough to implement in pure Python + Pygame in one file."
|
| 28 |
+
),
|
| 29 |
+
llm=llm,
|
| 30 |
+
verbose=True,
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
senior_python_developer = Agent(
|
| 34 |
+
role="Senior Python Game Developer",
|
| 35 |
+
goal="Write clean, working Python code (using Pygame) for the described game",
|
| 36 |
+
backstory=(
|
| 37 |
+
"You are a senior software engineer specialized in Python game development with Pygame."
|
| 38 |
+
"You write structured, readable code with:"
|
| 39 |
+
"- Proper game loop, event handling, drawing"
|
| 40 |
+
"- Comments explaining key parts"
|
| 41 |
+
"- Error handling where needed"
|
| 42 |
+
"You always produce a complete, runnable .py file."
|
| 43 |
+
),
|
| 44 |
+
llm=llm,
|
| 45 |
+
tools=[search_tool],
|
| 46 |
+
verbose=True,
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
qa_engineer = Agent(
|
| 50 |
+
role="QA Engineer & Code Reviewer",
|
| 51 |
+
goal="Test, review, and improve the code for bugs, playability, and completeness",
|
| 52 |
+
backstory=(
|
| 53 |
+
"You are a meticulous QA engineer and code reviewer."
|
| 54 |
+
"You carefully check:"
|
| 55 |
+
"- Does the code run without errors?"
|
| 56 |
+
"- Does it implement ALL the designed features?"
|
| 57 |
+
"- Is it fun/playable? Any obvious balance issues?"
|
| 58 |
+
"- Code style, variable names, comments"
|
| 59 |
+
"Suggest fixes or small improvements and output the FINAL improved code."
|
| 60 |
+
),
|
| 61 |
+
llm=llm,
|
| 62 |
+
verbose=True,
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
task_design = Task(
|
| 67 |
+
description=(
|
| 68 |
+
"Take the user's game idea: {game_idea}"
|
| 69 |
+
"1. Clarify and expand it into a fun, simple 2D game"
|
| 70 |
+
"2. Describe: objective, controls, entities, win/lose"
|
| 71 |
+
"3. Keep scope small (one level, basic mechanics)"
|
| 72 |
+
"Output format:"
|
| 73 |
+
"## Game Design Document\n"
|
| 74 |
+
"- Title: ...\n"
|
| 75 |
+
"- Genre: ...\n"
|
| 76 |
+
"- Objective: ...\n"
|
| 77 |
+
"- Controls: ...\n"
|
| 78 |
+
"- Entities: ...\n"
|
| 79 |
+
"- Mechanics: ..."
|
| 80 |
+
),
|
| 81 |
+
expected_output="A clear markdown game design document",
|
| 82 |
+
agent=game_desiner,
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
task_code = Task(
|
| 86 |
+
description=(
|
| 87 |
+
"Using the game design from the previous task"
|
| 88 |
+
"Write a COMPLETE, standalone Python script using Pygame that implements the game."
|
| 89 |
+
"- Include import pygame, sys, random (if needed)"
|
| 90 |
+
"- Full game loop, init, events, update, draw"
|
| 91 |
+
"- Make it runnable with python game.py"
|
| 92 |
+
"- Add simple comments"
|
| 93 |
+
"- The main game loop must be exposed in the python code, it should not be inside any function like main"
|
| 94 |
+
"- Final answer MUST be ONLY the Python code and Instructions on how to play the game"
|
| 95 |
+
),
|
| 96 |
+
expected_output="A complete runnable python script",
|
| 97 |
+
agent=senior_python_developer,
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
task_review = Task(
|
| 101 |
+
description=(
|
| 102 |
+
"Review the Python code from the previous task."
|
| 103 |
+
"1. Check for syntax/runtime errors"
|
| 104 |
+
"2. Verify it matches the design document"
|
| 105 |
+
"3. Test mentally: does it have init, loop, quit handling, drawing?"
|
| 106 |
+
"4. Suggest fixes/improvements if needed"
|
| 107 |
+
"5. Output the FINAL, improved, ready-to-run code"
|
| 108 |
+
"Your final answer MUST be ONLY the complete Python code along with the instructions on how to play the game"
|
| 109 |
+
),
|
| 110 |
+
expected_output="Final Polished, runnable pygame python script and instructions on how to play the game",
|
| 111 |
+
agent=qa_engineer,
|
| 112 |
+
context=[task_design, task_code],
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
game_crew = Crew(
|
| 117 |
+
agents=[game_desiner, senior_python_developer, qa_engineer],
|
| 118 |
+
tasks=[task_design, task_code, task_review],
|
| 119 |
+
process=Process.sequential,
|
| 120 |
+
verbose=True,
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
async def generate_game_code(game_idea: str) -> str:
|
| 125 |
+
"""Generates game code based on the provided game idea using CrewAI."""
|
| 126 |
+
if not game_idea:
|
| 127 |
+
return "Please enter a game idea to generate code."
|
| 128 |
+
try:
|
| 129 |
+
result = await game_crew.kickoff_async(inputs={"game_idea": game_idea})
|
| 130 |
+
return result
|
| 131 |
+
except Exception as e:
|
| 132 |
+
return f"An error occurred: {e}"
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
custom_css = """
|
| 136 |
+
body { background-color: #1a1a2e; color: #e0e0e0; }
|
| 137 |
+
.gradio-container { background-color: #16213e; border-radius: 10px; }
|
| 138 |
+
.panel-nav { background-color: #0f3460; }
|
| 139 |
+
.block-title { color: #e94560; }
|
| 140 |
+
.label { color: #e0e0e0; }
|
| 141 |
+
.input-textbox textarea { background-color: #0f3460; color: #e0e0e0; border: 1px solid #e94560; }
|
| 142 |
+
.output-textbox textarea { background-color: #0f3460; color: #e0e0e0; border: 1px solid #e94560; }
|
| 143 |
+
.gr-button { background-color: #e94560; color: white; border: none; padding: 10px 20px; border-radius: 5px; cursor: pointer; transition: background-color 0.3s ease; }
|
| 144 |
+
.gr-button:hover { background-color: #ba324d; }
|
| 145 |
+
"""
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
iface = gr.Interface(
|
| 149 |
+
fn=generate_game_code,
|
| 150 |
+
inputs=gr.Textbox(lines=2, label="Enter your game idea", placeholder="e.g., A fun endless runner where a character jumps over obstacles"),
|
| 151 |
+
outputs=gr.Textbox(lines=15, label="Generated Game Code and Instructions"),
|
| 152 |
+
title="<h1 style='color:#e94560;'>Game Creator AI</h1>",
|
| 153 |
+
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>",
|
| 154 |
+
css=custom_css,
|
| 155 |
+
allow_flagging="never"
|
| 156 |
+
)
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
iface.launch(debug=True, share=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
crewai
|
| 2 |
+
gradio
|
| 3 |
+
crewai_tools
|