File size: 13,664 Bytes
bf05a7d |
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 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 |
import gradio as gr
import json
import random
import math
import numpy as np
# ================== ГЛОБАЛЬНЫЕ НАСТРОЙКИ ==================
UNIVERSE_SEED = 42
RESOURCE_TYPES = ['Iron', 'Copper', 'Gold', 'Water', 'Oxygen', 'Hydrogen', 'Silicon', 'Titanium']
CIVILIZATION_STAGES = [
{"title": "I. Биологический", "color": "#00cc44", "desc": "Углеродно-водные формы жизни..."},
{"title": "II. Техногенный", "color": "#00aaff", "desc": "Промышленная и цифровая революция..."},
{"title": "III. Постбиологический", "color": "#ff7700", "desc": "Переход к цифровому существованию..."},
{"title": "IV. Космический", "color": "#aa55ff", "desc": "Межзвездные путешествия..."},
{"title": "V. Универсальный", "color": "#ffff00", "desc": "Слияние с космическим разумом..."}
]
# ================== ГЕНЕРАТОР ВСЕЛЕННОЙ ==================
class UniverseGenerator:
def __init__(self, seed=UNIVERSE_SEED):
self.rng = random.Random(seed)
self.planet_types = ['Lava', 'Ocean', 'Desert', 'Ice', 'Jungle']
def generate_planet(self, index):
return {
'name': f"Planet-{chr(65 + index)}",
'type': self.rng.choice(self.planet_types),
'resources': self.rng.sample(RESOURCE_TYPES, self.rng.randint(2, 5)),
'size': self.rng.uniform(0.8, 1.5),
'habitability': round(self.rng.uniform(0.1, 0.9), 2)
}
def generate_system(self, index):
return {
'name': f"System-{index}",
'planets': [self.generate_planet(i) for i in range(self.rng.randint(3, 6))]
}
def generate_universe(self):
return {
'systems': [self.generate_system(i) for i in range(3)],
'civilization_stages': CIVILIZATION_STAGES
}
# ================== THREE.JS ШАБЛОНЫ ==================
CIVILIZATION_TEMPLATE = """
<!DOCTYPE html>
<html>
<head>
<title>Эволюция Цивилизаций</title>
<style>
body {{
margin: 0;
overflow: hidden;
background: #001133;
color: white;
font-family: Arial, sans-serif;
}}
#infoBox {{
position: absolute;
top: 20px;
left: 20px;
max-width: 350px;
background: rgba(0, 0, 20, 0.85);
padding: 20px;
border-radius: 15px;
border: 1px solid #4a6fff;
display: none;
}}
#title {{
position: absolute;
top: 20px;
width: 100%;
text-align: center;
font-size: 24px;
color: #4fc3f7;
text-shadow: 0 0 15px rgba(79, 195, 247, 0.8);
pointer-events: none;
}}
</style>
</head>
<body>
<div id="title">ЭВОЛЮЦИЯ ЦИВИЛИЗАЦИЙ ВО ВСЕЛЕННОЙ</div>
<div id="infoBox">
<h2 id="stageTitle"></h2>
<p id="stageDesc"></p>
</div>
<script src="https://cdn.jsdelivr.net/npm/three@0.132.2/build/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.132.2/examples/js/controls/OrbitControls.min.js"></script>
<script>
// Инициализация сцены
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x001122);
const camera = new THREE.PerspectiveCamera(60, window.innerWidth/window.innerHeight, 0.1, 2000);
const renderer = new THREE.WebGLRenderer({{ antialias: true }});
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// Настройка камеры и освещения
camera.position.set(0, 15, 50);
const controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
// Создание звездного поля
const starGeometry = new THREE.BufferGeometry();
const starPositions = new Float32Array(5000 * 3);
for (let i = 0; i < 5000; i++) {{
starPositions[i*3] = (Math.random() - 0.5) * 2000;
starPositions[i*3+1] = (Math.random() - 0.5) * 2000;
starPositions[i*3+2] = (Math.random() - 0.5) * 2000;
}}
starGeometry.setAttribute('position', new THREE.BufferAttribute(starPositions, 3));
const stars = new THREE.Points(starGeometry, new THREE.PointsMaterial({{ color: 0xffffff, size: 1.5 }}));
scene.add(stars);
// Создание сфер цивилизаций
const stages = {civilization_stages};
const spheres = [];
stages.forEach((stage, i) => {{
const geometry = new THREE.SphereGeometry(4, 32, 32);
const material = new THREE.MeshStandardMaterial({{
color: new THREE.Color(stage.color),
emissive: new THREE.Color(stage.color),
metalness: 0.7,
roughness: 0.2
}});
const sphere = new THREE.Mesh(geometry, material);
sphere.position.x = (i - stages.length/2) * 15;
sphere.userData = stage;
scene.add(sphere);
spheres.push(sphere);
}});
// Интерактивность
const raycaster = new THREE.Raycaster();
const mouse = new THREE.Vector2();
const infoBox = document.getElementById("infoBox");
window.addEventListener("click", (event) => {{
mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
raycaster.setFromCamera(mouse, camera);
const intersects = raycaster.intersectObjects(spheres);
if (intersects.length > 0) {{
const stage = intersects[0].object.userData;
document.getElementById("stageTitle").textContent = stage.title;
document.getElementById("stageDesc").textContent = stage.desc;
infoBox.style.display = "block";
}}
}});
// Анимация
function animate() {{
requestAnimationFrame(animate);
spheres.forEach((sphere, i) => {{
sphere.rotation.y += 0.01;
sphere.rotation.x += 0.005;
}});
controls.update();
renderer.render(scene, camera);
}}
animate();
// Адаптация под размер окна
window.addEventListener("resize", () => {{
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}});
</script>
</body>
</html>
"""
SPACE_EXPLORER_TEMPLATE = """
<!DOCTYPE html>
<html>
<head>
<title>Космический Исследователь</title>
<style>
body {{
margin: 0;
overflow: hidden;
background: #000;
font-family: Arial, sans-serif;
}}
#ui {{
position: absolute;
top: 20px;
left: 20px;
color: white;
background: rgba(0, 0, 0, 0.7);
padding: 15px;
border-radius: 10px;
width: 300px;
}}
.bar-container {{ width: 100%; background: #333; border-radius: 5px; margin: 5px 0; }}
.bar {{ height: 20px; border-radius: 5px; text-align: center; color: white; }}
.health-bar {{ background: linear-gradient(to right, #ff0000, #00ff00); }}
.fuel-bar {{ background: linear-gradient(to right, #0000ff, #00ffff); }}
#planet-info {{
position: absolute;
top: 20px;
right: 20px;
background: rgba(0, 0, 0, 0.7);
padding: 15px;
border-radius: 10px;
color: white;
display: none;
}}
</style>
</head>
<body>
<div id="ui">
<h3>Космический Корабль</h3>
<div>Здоровье: <span id="health-value">100</span>/100</div>
<div class="bar-container"><div id="health-bar" class="bar health-bar" style="width: 100%"></div></div>
<div>Топливо: <span id="fuel-value">100</span>/100</div>
<div class="bar-container"><div id="fuel-bar" class="bar fuel-bar" style="width: 100%"></div></div>
</div>
<div id="planet-info">
<h3 id="planet-name"></h3>
<div>Тип: <span id="planet-type"></span></div>
<div>Ресурсы: <span id="planet-resources"></span></div>
</div>
<script src="https://cdn.jsdelivr.net/npm/three@0.132.2/build/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.132.2/examples/js/controls/OrbitControls.min.js"></script>
<script>
// Инициализация сцены
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x000010);
const camera = new THREE.PerspectiveCamera(75, window.innerWidth/window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer({{ antialias: true }});
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// Освещение
const ambientLight = new THREE.AmbientLight(0x404040);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 1);
directionalLight.position.set(1, 1, 1);
scene.add(directionalLight);
// Корабль игрока
const shipGeometry = new THREE.ConeGeometry(1, 3, 8);
const shipMaterial = new THREE.MeshPhongMaterial({{ color: 0x00aaff }});
const ship = new THREE.Mesh(shipGeometry, shipMaterial);
ship.rotation.x = Math.PI / 2;
scene.add(ship);
// Планеты
const planets = [];
const universeData = {universe_data};
universeData.systems[0].planets.forEach((planet, i) => {{
const geometry = new THREE.SphereGeometry(planet.size, 32, 32);
const material = new THREE.MeshStandardMaterial({{
color: new THREE.Color(
planet.type === 'Lava' ? 0xff3300 :
planet.type === 'Ocean' ? 0x0066ff :
planet.type === 'Desert' ? 0xffcc00 :
0x888888
),
roughness: 0.8,
metalness: 0.2
}});
const planetMesh = new THREE.Mesh(geometry, material);
planetMesh.position.x = i * 15 - 30;
planetMesh.userData = planet;
scene.add(planetMesh);
planets.push(planetMesh);
}});
// Управление кораблем
const keys = {{}};
window.addEventListener('keydown', (e) => keys[e.key.toLowerCase()] = true);
window.addEventListener('keyup', (e) => keys[e.key.toLowerCase()] = false);
// Анимация
function animate() {{
requestAnimationFrame(animate);
// Движение корабля
if (keys['w']) ship.position.z -= 0.1;
if (keys['s']) ship.position.z += 0.1;
if (keys['a']) ship.position.x -= 0.1;
if (keys['d']) ship.position.x += 0.1;
// Обновление камеры
camera.position.x = ship.position.x;
camera.position.y = ship.position.y + 5;
camera.position.z = ship.position.z + 15;
camera.lookAt(ship.position);
renderer.render(scene, camera);
}}
animate();
// Адаптация под размер окна
window.addEventListener('resize', () => {{
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}});
</script>
</body>
</html>
"""
# ================== GRADIO ИНТЕРФЕЙС ==================
def create_civilization_demo():
universe = UniverseGenerator().generate_universe()
template = CIVILIZATION_TEMPLATE.replace("{civilization_stages}", json.dumps(universe["civilization_stages"]))
return template
def create_space_explorer_demo():
universe = UniverseGenerator().generate_universe()
template = SPACE_EXPLORER_TEMPLATE.replace("{universe_data}", json.dumps(universe))
return template
with gr.Blocks(title="Космические Игры", css=".gradio-container {background: #000033;}") as demo:
gr.Markdown("# 🌌 Две Космические Игры")
gr.Markdown("### Исследуйте эволюцию цивилизаций и космические просторы")
with gr.Tabs():
with gr.TabItem("Эволюция Цивилизаций"):
civ_html = gr.HTML(create_civilization_demo())
gr.Markdown("""
**Инструкции:**
- Кликните на сферы, чтобы узнать о стадиях развития цивилизаций
- Используйте мышь для вращения камеры
""")
with gr.TabItem("Космический Исследователь"):
space_html = gr.HTML(create_space_explorer_demo())
gr.Markdown("""
**Управление:**
- W/S: Движение вперед/назад
- A/D: Движение влево/вправо
- Мышь: Управление камерой
""")
gr.Markdown("### Общие игровые системы:")
with gr.Accordion("Технические детали", open=False):
gr.Markdown("""
**Общие компоненты:**
1. **Генерация вселенной**: Процедурное создание планет и систем
2. **Three.js рендеринг**: Реалистичное 3D отображение космоса
3. **Интерактивные элементы**: Взаимодействие с объектами через UI
4. **Физика движения**: Реалистичное управление космическим кораблем
""")
if __name__ == "__main__":
demo.launch() |