File size: 10,540 Bytes
db57c27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st

# Set wide layout
st.set_page_config(layout="wide", page_title="Galaxian 3D Evolution", initial_sidebar_state="expanded")

# Sidebar instructions
st.sidebar.markdown("## Galaxian 3D Evolution Controls")
st.sidebar.markdown("""
- **Controls:**  
  - **WASD:** Move camera (forward, left, back, right)  
  - **QE:** Rotate camera up/down  
  - **Mouse:** Orbit camera (click and drag)  
  - **Arrow Keys:** Direct snake (Up, Down, Left, Right)  
  - **R:** Reset game  

- **Features:**  
  - 3D snake navigates a space grid.  
  - Eat alien food (👾) to grow and trigger L-system growth.  
  - Creatures exchange quine messages (hover to see).  
  - Avoid walls and self-collision.  

Enjoy the 3D Galaxian experience!
""")

# Three.js HTML/JavaScript code
html_code = r"""
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Galaxian 3D Evolution</title>
    <style>
        body { margin: 0; padding: 0; overflow: hidden; background: black; }
        canvas { display: block; }
    </style>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r134/three.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/examples/js/controls/OrbitControls.js"></script>
</head>
<body>
    <script>
        let scene, camera, renderer, controls, snake, food, creatures = [], messages = [];
        const gridSize = 20;
        const worldSize = 400;

        function init() {
            // Scene setup
            scene = new THREE.Scene();
            camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
            renderer = new THREE.WebGLRenderer();
            renderer.setSize(window.innerWidth, window.innerHeight);
            document.body.appendChild(renderer.domElement);

            // Orbit controls
            controls = new THREE.OrbitControls(camera, renderer.domElement);
            controls.enableDamping = true;
            controls.dampingFactor = 0.05;

            // Lighting
            const ambientLight = new THREE.AmbientLight(0x404040);
            scene.add(ambientLight);
            const directionalLight = new THREE.DirectionalLight(0xffffff, 0.5);
            directionalLight.position.set(1, 1, 1);
            scene.add(directionalLight);

            // Starfield
            const starsGeometry = new THREE.BufferGeometry();
            const starsMaterial = new THREE.PointsMaterial({ color: 0xffffff, size: 2 });
            const starPositions = new Float32Array(1000 * 3);
            for (let i = 0; i < 1000; i++) {
                starPositions[i * 3] = (Math.random() - 0.5) * worldSize;
                starPositions[i * 3 + 1] = (Math.random() - 0.5) * worldSize;
                starPositions[i * 3 + 2] = (Math.random() - 0.5) * worldSize;
            }
            starsGeometry.setAttribute('position', new THREE.BufferAttribute(starPositions, 3));
            const stars = new THREE.Points(starsGeometry, starsMaterial);
            scene.add(stars);

            // Initialize snake
            snake = [];
            const snakeMaterial = new THREE.MeshPhongMaterial({ color: 0x00ff00 });
            for (let i = 0; i < 3; i++) {
                const segment = new THREE.Mesh(new THREE.SphereGeometry(gridSize / 2, 32, 32), snakeMaterial);
                segment.position.set(-i * gridSize, 0, 0);
                snake.push(segment);
                scene.add(segment);
            }
            snake.direction = new THREE.Vector3(gridSize, 0, 0);

            // Food (alien)
            placeFood();

            // L-system creature
            createLSysCreature();

            // Camera position
            camera.position.set(0, 100, 200);
            camera.lookAt(0, 0, 0);

            animate();
        }

        function placeFood() {
            if (food) scene.remove(food);
            const foodGeometry = new THREE.DodecahedronGeometry(gridSize / 2);
            const foodMaterial = new THREE.MeshPhongMaterial({ color: 0xff00ff });
            food = new THREE.Mesh(foodGeometry, foodMaterial);
            food.position.set(
                (Math.floor(Math.random() * (worldSize / gridSize)) - worldSize / (2 * gridSize)) * gridSize,
                0,
                (Math.floor(Math.random() * (worldSize / gridSize)) - worldSize / (2 * gridSize)) * gridSize
            );
            scene.add(food);
        }

        function createLSysCreature() {
            const lSys = {
                axiom: "F",
                rules: { "F": "F[+F]F[-F]F" },
                angle: 25,
                length: gridSize,
                iterations: 3
            };
            let turtleString = lSys.axiom;
            for (let i = 0; i < lSys.iterations; i++) {
                turtleString = applyLSysRules(turtleString, lSys.rules);
            }
            const creatureMaterial = new THREE.MeshPhongMaterial({ color: 0x0000ff });
            const creature = new THREE.Group();
            let stack = [];
            let pos = new THREE.Vector3(worldSize / 4, 0, worldSize / 4);
            let dir = new THREE.Vector3(0, lSys.length, 0);

            for (let char of turtleString) {
                if (char === "F") {
                    const segment = new THREE.Mesh(new THREE.CylinderGeometry(2, 2, lSys.length, 16), creatureMaterial);
                    segment.position.copy(pos).add(dir.clone().multiplyScalar(0.5));
                    segment.quaternion.setFromUnitVectors(new THREE.Vector3(0, 1, 0), dir.clone().normalize());
                    creature.add(segment);
                    pos.add(dir);
                } else if (char === "+") {
                    dir.applyAxisAngle(new THREE.Vector3(0, 0, 1), lSys.angle * Math.PI / 180);
                } else if (char === "-") {
                    dir.applyAxisAngle(new THREE.Vector3(0, 0, 1), -lSys.angle * Math.PI / 180);
                } else if (char === "[") {
                    stack.push({ pos: pos.clone(), dir: dir.clone() });
                } else if (char === "]") {
                    const state = stack.pop();
                    pos = state.pos;
                    dir = state.dir;
                }
            }
            scene.add(creature);
            creatures.push(creature);
            sendQuineMessage(creature);
        }

        function applyLSysRules(str, rules) {
            return str.split("").map(char => rules[char] || char).join("");
        }

        function sendQuineMessage(sender) {
            const quine = "function q(){console.log('Message from creature: '+q.toString())}q()";
            const messageGeometry = new THREE.TextGeometry("Quine Msg", {
                font: new THREE.FontLoader().parse({}), // Placeholder: requires font file
                size: 10,
                height: 2
            });
            const messageMaterial = new THREE.MeshBasicMaterial({ color: 0xffff00 });
            const message = new THREE.Mesh(messageGeometry, messageMaterial);
            message.position.copy(sender.position).add(new THREE.Vector3(0, 50, 0));
            scene.add(message);
            messages.push({ mesh: message, ttl: 100 });
            setTimeout(() => creatures.forEach(c => c !== sender && listenToMessage(c, quine)), 1000);
        }

        function listenToMessage(creature, msg) {
            const response = new THREE.Mesh(
                new THREE.SphereGeometry(5, 32, 32),
                new THREE.MeshBasicMaterial({ color: 0xff0000 })
            );
            response.position.copy(creature.position).add(new THREE.Vector3(0, 30, 0));
            scene.add(response);
            setTimeout(() => scene.remove(response), 2000);
        }

        function updateSnake() {
            const head = snake[0];
            const newHead = head.clone();
            newHead.position.add(snake.direction);

            if (Math.abs(newHead.position.x) > worldSize / 2 || Math.abs(newHead.position.z) > worldSize / 2) {
                resetGame();
                return;
            }
            for (let i = 1; i < snake.length; i++) {
                if (newHead.position.distanceTo(snake[i].position) < gridSize) {
                    resetGame();
                    return;
                }
            }

            snake.unshift(newHead);
            scene.add(newHead);
            if (newHead.position.distanceTo(food.position) < gridSize) {
                placeFood();
                createLSysCreature();
            } else {
                scene.remove(snake.pop());
            }
        }

        function resetGame() {
            snake.forEach(seg => scene.remove(seg));
            snake = [];
            const snakeMaterial = new THREE.MeshPhongMaterial({ color: 0x00ff00 });
            for (let i = 0; i < 3; i++) {
                const segment = new THREE.Mesh(new THREE.SphereGeometry(gridSize / 2, 32, 32), snakeMaterial);
                segment.position.set(-i * gridSize, 0, 0);
                snake.push(segment);
                scene.add(segment);
            }
            snake.direction = new THREE.Vector3(gridSize, 0, 0);
            placeFood();
        }

        let moveCounter = 0;
        const moveDelay = 10;
        function animate() {
            requestAnimationFrame(animate);
            controls.update();
            moveCounter++;
            if (moveCounter >= moveDelay) {
                updateSnake();
                moveCounter = 0;
            }
            messages = messages.filter(m => {
                m.ttl--;
                if (m.ttl <= 0) scene.remove(m.mesh);
                return m.ttl > 0;
            });
            renderer.render(scene, camera);
        }

        document.addEventListener("keydown", (e) => {
            switch (e.key) {
                case "ArrowUp": snake.direction.set(0, 0, -gridSize); break;
                case "ArrowDown": snake.direction.set(0, 0, gridSize); break;
                case "ArrowLeft": snake.direction.set(-gridSize, 0, 0); break;
                case "ArrowRight": snake.direction.set(gridSize, 0, 0); break;
                case "r": resetGame(); break;
            }
        });

        window.addEventListener("resize", () => {
            camera.aspect = window.innerWidth / window.innerHeight;
            camera.updateProjectionMatrix();
            renderer.setSize(window.innerWidth, window.innerHeight);
        });

        init();
    </script>
</body>
</html>
"""

st.components.v1.html(html_code, height=700, scrolling=False)