-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMagicGameDesign.py
More file actions
278 lines (234 loc) · 12 KB
/
MagicGameDesign.py
File metadata and controls
278 lines (234 loc) · 12 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import sys
import json
import requests
import zipfile
import os
from io import BytesIO
from PIL import Image
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QLabel, QLineEdit, QPushButton, QFileDialog, QTextEdit, QMessageBox, QProgressBar, QInputDialog
from PyQt5.QtCore import QThread, pyqtSignal
# OpenAI and DALL-E setup
CHAT_API_URL = "https://api.openai.com/v1/chat/completions"
DALLE_API_URL = "https://api.openai.com/v1/images/generations"
API_KEY_FILE = "api_key.json"
HEADERS = {
"Authorization": "",
"Content-Type": "application/json"
}
class QuickActionThread(QThread):
progress = pyqtSignal(int, str)
finished = pyqtSignal(bytes, str)
def __init__(self, action, prompt, parent=None):
super().__init__(parent)
self.action = action
self.prompt = prompt
def run(self):
try:
if self.action == "game plan":
result = self.generate_game_plan()
else:
result = self.generate_content(self.prompt)
if isinstance(result, dict):
zip_data = self.create_zip(result)
self.finished.emit(zip_data, f"{self.action}.zip")
else:
self.finished.emit(None, result)
except Exception as e:
self.finished.emit(None, f"Error: {str(e)}")
def generate_content(self, prompt):
data = {
"model": "gpt-4",
"messages": [
{"role": "system", "content": f"You are a helpful assistant specializing in {self.action}."},
{"role": "user", "content": prompt}
]
}
try:
response = requests.post(CHAT_API_URL, headers=HEADERS, json=data)
response.raise_for_status()
response_data = response.json()
if "choices" not in response_data:
error_message = response_data.get("error", {}).get("message", "Unknown error")
return f"Error: {error_message}"
content_text = response_data["choices"][0]["message"]["content"]
return content_text
except requests.RequestException as e:
return f"Error: Unable to communicate with the OpenAI API."
def generate_game_plan(self):
game_plan = {}
user_prompt = self.prompt
try:
self.progress.emit(10, "Generating game concept...")
game_concept = self.generate_content(f"Invent a new 2D game concept with a detailed theme, setting, and unique features based on the following prompt: {user_prompt}. Ensure the game has WASD controls.")
game_plan['game_concept'] = game_concept
self.progress.emit(20, "Generating world concept...")
game_plan['world_concept'] = self.generate_content(f"Create a detailed world concept for the 2D game: {game_concept}")
self.progress.emit(30, "Generating character concepts...")
game_plan['character_concepts'] = self.generate_content(f"Create detailed character concepts for the player and enemies in the 2D game: {game_concept}")
self.progress.emit(40, "Generating plot...")
game_plan['plot'] = self.generate_content(f"Create a plot for the 2D game based on the world and characters of the game: {game_concept}")
self.progress.emit(50, "Generating dialogue...")
game_plan['dialogue'] = self.generate_content(f"Write some dialogue for the 2D game based on the plot of the game: {game_concept}")
self.progress.emit(60, "Generating images...")
game_plan['images'] = self.generate_images(game_concept, game_plan['character_concepts'], game_plan['world_concept'])
self.progress.emit(70, "Generating Unity scripts...")
game_plan['unity_scripts'] = self.generate_unity_scripts(game_concept, game_plan['character_concepts'], game_plan['world_concept'])
self.progress.emit(80, "Generating recap...")
game_plan['recap'] = self.generate_content(f"Recap the game plan for the 2D game: {game_concept}")
self.progress.emit(85, "Generating master document...")
game_plan['master_document'] = self.create_master_document(game_plan)
self.progress.emit(90, "Packaging into ZIP...")
return game_plan
except Exception as e:
return f"Error during game plan generation: {str(e)}"
def generate_images(self, game_concept, character_concepts, world_concept):
images = {}
descriptions = [
f"Full-body, hyper-realistic character for a 2D game, with no background, in Unreal Engine style, based on the character descriptions: {character_concepts}",
f"Full-body, hyper-realistic enemy character for a 2D game, with no background, in Unreal Engine style, based on the character descriptions: {character_concepts}",
f"High-quality game object for the 2D game, with no background, in Unreal Engine style, based on the world concept: {world_concept}",
f"High-quality game object for the 2D game, with no background, in Unreal Engine style, based on the world concept: {world_concept}",
f"High-quality game object for the 2D game, with no background, in Unreal Engine style, based on the world concept: {world_concept}",
f"High-quality level background for the 2D game, in Unreal Engine style, based on the world concept: {world_concept}"
]
for i, desc in enumerate(descriptions, start=1):
self.progress.emit(60 + i * 5, f"Generating image {i}...")
image_content = self.generate_image(desc)
if image_content:
images[f"image_{i}.png"] = image_content
else:
images[f"image_{i}.png"] = b""
return images
def generate_image(prompt, size="1024x1024"):
response = openai.Image.create(
prompt=prompt,
n=1,
size=size,
response_format="url"
)
image_url = response['data'][0]['url']
return image_url
def save_image(image_url, save_path):
image_response = requests.get(image_url)
image = Image.open(BytesIO(image_response.content))
image.save(save_path)
return save_path
def generate_unity_scripts(self, game_concept, character_concepts, world_concept):
scripts = {}
descriptions = [
f"Unity script for the player character in a 2D game with WASD controls and space bar to jump or shoot, based on the character descriptions: {character_concepts}",
f"Unity script for an enemy character in a 2D game with basic AI behavior, based on the character descriptions: {character_concepts}",
f"Unity script for a game object in a 2D game, based on the world concept: {world_concept}",
f"Unity script for a second game object in a 2D game, based on the world concept: {world_concept}",
f"Unity script for a third game object in a 2D game, based on the world concept: {world_concept}",
f"Unity script for the level background in a 2D game, based on the world concept: {world_concept}"
]
for i, desc in enumerate(descriptions, start=1):
scripts[f"script_{i}.cs"] = self.generate_content(desc)
return scripts
def create_master_document(self, game_plan):
master_doc = "Game Plan Master Document\n\n"
for key, value in game_plan.items():
if key == "images":
master_doc += f"{key.capitalize()}:\n"
for img_key in value:
master_doc += f" - {img_key}: See attached image.\n"
elif key == "unity_scripts":
master_doc += f"{key.replace('_', ' ').capitalize()}:\n"
for script_key in value:
master_doc += f" - {script_key}: See attached script.\n"
else:
master_doc += f"{key.replace('_', ' ').capitalize()}: See attached document.\n"
return master_doc
def create_zip(self, content_dict):
zip_buffer = BytesIO()
with zipfile.ZipFile(zip_buffer, 'w', zipfile.ZIP_DEFLATED) as zip_file:
for key, value in content_dict.items():
if isinstance(value, str):
zip_file.writestr(f"{key}.txt", value)
elif isinstance(value, bytes):
zip_file.writestr(key, value)
elif isinstance(value, dict):
for sub_key, sub_value in value.items():
if isinstance(sub_value, str):
zip_file.writestr(f"{key}/{sub_key}.txt", sub_value)
elif isinstance(sub_value, bytes):
zip_file.writestr(f"{key}/{sub_key}", sub_value)
zip_buffer.seek(0)
self.progress.emit(100, "ZIP package created.")
return zip_buffer.read()
class QuickActionsApp(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Quick Actions")
self.api_key = self.load_api_key()
if not self.api_key:
self.api_key = self.ask_api_key()
if not self.api_key:
QMessageBox.critical(self, "Error", "API key is required to proceed.")
sys.exit()
global HEADERS
HEADERS = {
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
}
# Main layout
self.main_widget = QWidget()
self.main_layout = QVBoxLayout()
self.main_widget.setLayout(self.main_layout)
self.setCentralWidget(self.main_widget)
self.prompt_label = QLabel("Enter topic/keywords:")
self.prompt_entry = QLineEdit()
self.main_layout.addWidget(self.prompt_label)
self.main_layout.addWidget(self.prompt_entry)
self.result_box = QTextEdit()
self.result_box.setReadOnly(True)
self.main_layout.addWidget(self.result_box)
self.progress_bar = QProgressBar()
self.main_layout.addWidget(self.progress_bar)
self.actions = [
"game plan"
]
for action in self.actions:
button = QPushButton(f"Generate {action.capitalize()}")
button.clicked.connect(lambda checked, a=action: self.handle_action(a))
self.main_layout.addWidget(button)
def load_api_key(self):
if os.path.exists(API_KEY_FILE):
with open(API_KEY_FILE, 'r') as file:
data = json.load(file)
return data.get('api_key')
return None
def ask_api_key(self):
api_key, ok = QInputDialog.getText(self, "API Key", "Please enter your OpenAI API key:", QLineEdit.Password)
if ok:
with open(API_KEY_FILE, 'w') as file:
json.dump({"api_key": api_key}, file)
return api_key
return None
def handle_action(self, action):
prompt = self.prompt_entry.text()
self.result_box.append(f"Generating {action}...")
self.progress_bar.setValue(0)
self.quick_action_thread = QuickActionThread(action, prompt)
self.quick_action_thread.progress.connect(self.update_progress)
self.quick_action_thread.finished.connect(self.handle_finished)
self.quick_action_thread.start()
def update_progress(self, value, message):
self.progress_bar.setValue(value)
self.result_box.append(message)
def handle_finished(self, zip_data, filename_or_error):
if zip_data:
options = QFileDialog.Options()
file_path, _ = QFileDialog.getSaveFileName(self, "Save ZIP", "", "Zip Files (*.zip);;All Files (*)", options=options)
if file_path:
with open(file_path, 'wb') as file:
file.write(zip_data)
self.result_box.append(f"{filename_or_error} generated and saved.")
else:
self.result_box.append(filename_or_error)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = QuickActionsApp()
window.show()
sys.exit(app.exec_())