Spaces:
Running
Running
Update index.html
Browse files- index.html +87 -82
index.html
CHANGED
@@ -10,108 +10,113 @@
|
|
10 |
</style>
|
11 |
</head>
|
12 |
<body>
|
13 |
-
<
|
14 |
<script>
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
if (!gl) {
|
19 |
-
alert('WebGL not supported in this browser');
|
20 |
-
throw new Error('WebGL not supported');
|
21 |
-
}
|
22 |
-
|
23 |
-
// Vertex shader
|
24 |
-
const vsSource = `
|
25 |
-
attribute vec4 aVertexPosition;
|
26 |
-
uniform mat4 uModelViewMatrix;
|
27 |
-
uniform mat4 uProjectionMatrix;
|
28 |
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
}
|
32 |
-
`;
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
}
|
39 |
-
`;
|
40 |
-
|
41 |
-
// Compile shader
|
42 |
-
function loadShader(gl, type, source) {
|
43 |
-
const shader = gl.createShader(type);
|
44 |
-
gl.shaderSource(shader, source);
|
45 |
-
gl.compileShader(shader);
|
46 |
-
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
|
47 |
-
alert('An error occurred compiling the shaders: ' + gl.getShaderInfoLog(shader));
|
48 |
-
gl.deleteShader(shader);
|
49 |
-
return null;
|
50 |
-
}
|
51 |
-
return shader;
|
52 |
-
}
|
53 |
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
function initShaderProgram(gl, vsSource, fsSource) {
|
59 |
-
const vertexShader = loadShader(gl, gl.VERTEX_SHADER, vsSource);
|
60 |
-
const fragmentShader = loadShader(gl, gl.FRAGMENT_SHADER, fsSource);
|
61 |
-
|
62 |
-
const shaderProgram = gl.createProgram();
|
63 |
-
gl.attachShader(shaderProgram, vertexShader);
|
64 |
-
gl.attachShader(shaderProgram, fragmentShader);
|
65 |
-
gl.linkProgram(shaderProgram);
|
66 |
-
|
67 |
-
if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
|
68 |
-
alert('Unable to initialize the shader program: ' + gl.getProgramInfoLog(shaderProgram));
|
69 |
-
return null;
|
70 |
-
}
|
71 |
|
72 |
-
|
|
|
|
|
73 |
}
|
74 |
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
// Here we would define positions on a 3D sphere for snake and food
|
83 |
}
|
84 |
|
85 |
-
function
|
86 |
-
|
87 |
-
|
88 |
-
// Move the sphere (rotation for interaction)
|
89 |
-
angle += 0.1;
|
90 |
-
const modelViewMatrix = mat4.create();
|
91 |
-
mat4.rotate(modelViewMatrix, modelViewMatrix, angle * Math.PI / 180, [0, 1, 0]);
|
92 |
|
93 |
-
|
|
|
|
|
|
|
|
|
|
|
94 |
}
|
95 |
|
96 |
-
// Game logic
|
97 |
function update() {
|
98 |
-
// Move snake
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
}
|
100 |
|
101 |
-
|
102 |
-
|
|
|
|
|
|
|
|
|
103 |
update();
|
104 |
-
|
105 |
-
requestAnimationFrame(render);
|
106 |
}
|
107 |
|
108 |
-
|
109 |
-
|
110 |
|
111 |
-
//
|
112 |
-
|
113 |
-
function
|
114 |
-
|
|
|
|
|
115 |
}
|
116 |
</script>
|
117 |
</body>
|
|
|
10 |
</style>
|
11 |
</head>
|
12 |
<body>
|
13 |
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
|
14 |
<script>
|
15 |
+
let camera, scene, renderer;
|
16 |
+
let snake, food;
|
17 |
+
let angle = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
+
function init() {
|
20 |
+
// Camera setup
|
21 |
+
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
|
22 |
+
camera.position.z = 5;
|
23 |
+
|
24 |
+
// Scene setup
|
25 |
+
scene = new THREE.Scene();
|
26 |
+
|
27 |
+
// Sphere (world)
|
28 |
+
const geometry = new THREE.SphereGeometry(2, 32, 32);
|
29 |
+
const material = new THREE.MeshBasicMaterial({ color: 0x00ff00, wireframe: true });
|
30 |
+
scene.add(new THREE.Mesh(geometry, material));
|
31 |
+
|
32 |
+
// Snake setup
|
33 |
+
snake = [];
|
34 |
+
for (let i = 0; i < 3; i++) {
|
35 |
+
const snakePart = new THREE.Mesh(new THREE.SphereGeometry(0.1), new THREE.MeshBasicMaterial({ color: 0xff0000 }));
|
36 |
+
snakePart.position.set(0, 0, i * 0.2 - 0.4);
|
37 |
+
snake.push(snakePart);
|
38 |
+
scene.add(snakePart);
|
39 |
}
|
|
|
40 |
|
41 |
+
// Food setup
|
42 |
+
food = new THREE.Mesh(new THREE.SphereGeometry(0.1), new THREE.MeshBasicMaterial({ color: 0x0000ff }));
|
43 |
+
setRandomFoodPosition();
|
44 |
+
scene.add(food);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
+
// Renderer setup
|
47 |
+
renderer = new THREE.WebGLRenderer();
|
48 |
+
renderer.setSize(window.innerWidth, window.innerHeight);
|
49 |
+
document.body.appendChild(renderer.domElement);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
+
// Event listeners for rotation
|
52 |
+
document.addEventListener('mousemove', onMouseMove);
|
53 |
+
document.addEventListener('touchmove', onTouchMove, { passive: false });
|
54 |
}
|
55 |
|
56 |
+
function setRandomFoodPosition() {
|
57 |
+
const radius = 1.9;
|
58 |
+
const phi = Math.acos(2 * Math.random() - 1);
|
59 |
+
const theta = 2 * Math.PI * Math.random();
|
60 |
+
food.position.x = radius * Math.sin(phi) * Math.cos(theta);
|
61 |
+
food.position.y = radius * Math.sin(phi) * Math.sin(theta);
|
62 |
+
food.position.z = radius * Math.cos(phi);
|
|
|
63 |
}
|
64 |
|
65 |
+
function onMouseMove(e) {
|
66 |
+
angle -= e.movementX * 0.001;
|
67 |
+
}
|
|
|
|
|
|
|
|
|
68 |
|
69 |
+
function onTouchMove(e) {
|
70 |
+
e.preventDefault();
|
71 |
+
const touch = e.touches[0];
|
72 |
+
if (touch) {
|
73 |
+
angle -= touch.movementX * 0.001;
|
74 |
+
}
|
75 |
}
|
76 |
|
|
|
77 |
function update() {
|
78 |
+
// Move snake
|
79 |
+
for (let i = snake.length - 1; i > 0; i--) {
|
80 |
+
snake[i].position.copy(snake[i - 1].position);
|
81 |
+
}
|
82 |
+
|
83 |
+
// Move head in direction (simple forward movement here)
|
84 |
+
const head = snake[0];
|
85 |
+
const direction = head.position.clone().normalize().multiplyScalar(0.05);
|
86 |
+
head.position.add(direction);
|
87 |
+
|
88 |
+
// Check for food
|
89 |
+
if (head.position.distanceTo(food.position) < 0.2) {
|
90 |
+
const newPart = new THREE.Mesh(new THREE.SphereGeometry(0.1), new THREE.MeshBasicMaterial({ color: 0xff0000 }));
|
91 |
+
newPart.position.copy(snake[snake.length - 1].position);
|
92 |
+
snake.push(newPart);
|
93 |
+
scene.add(newPart);
|
94 |
+
setRandomFoodPosition();
|
95 |
+
}
|
96 |
+
|
97 |
+
// Keep snake on sphere surface
|
98 |
+
head.position.normalize().multiplyScalar(1.9);
|
99 |
}
|
100 |
|
101 |
+
function animate() {
|
102 |
+
requestAnimationFrame(animate);
|
103 |
+
|
104 |
+
// Rotate the scene for interaction
|
105 |
+
scene.rotation.y = angle;
|
106 |
+
|
107 |
update();
|
108 |
+
renderer.render(scene, camera);
|
|
|
109 |
}
|
110 |
|
111 |
+
init();
|
112 |
+
animate();
|
113 |
|
114 |
+
// Resize handling
|
115 |
+
window.addEventListener('resize', onWindowResize, false);
|
116 |
+
function onWindowResize() {
|
117 |
+
camera.aspect = window.innerWidth / window.innerHeight;
|
118 |
+
camera.updateProjectionMatrix();
|
119 |
+
renderer.setSize(window.innerWidth, window.innerHeight);
|
120 |
}
|
121 |
</script>
|
122 |
</body>
|