Spaces:
Running
Running
Update index.html
Browse files- index.html +118 -19
index.html
CHANGED
@@ -1,19 +1,118 @@
|
|
1 |
-
<!
|
2 |
-
<html>
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<!DOCTYPE html>
|
2 |
+
<html lang="en">
|
3 |
+
<head>
|
4 |
+
<meta charset="UTF-8">
|
5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6 |
+
<title>3D Sphere Snake Game</title>
|
7 |
+
<style>
|
8 |
+
body { margin: 0; overflow: hidden; }
|
9 |
+
canvas { display: block; }
|
10 |
+
</style>
|
11 |
+
</head>
|
12 |
+
<body>
|
13 |
+
<canvas id="gameCanvas"></canvas>
|
14 |
+
<script>
|
15 |
+
// Setup the WebGL context
|
16 |
+
const canvas = document.getElementById('gameCanvas');
|
17 |
+
const gl = canvas.getContext('webgl') || canvas.getContext('experimental-webgl');
|
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 |
+
void main() {
|
30 |
+
gl_Position = uProjectionMatrix * uModelViewMatrix * aVertexPosition;
|
31 |
+
}
|
32 |
+
`;
|
33 |
+
|
34 |
+
// Fragment shader
|
35 |
+
const fsSource = `
|
36 |
+
void main() {
|
37 |
+
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); // Red color for snake
|
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 |
+
// Create shader program
|
55 |
+
const shaderProgram = initShaderProgram(gl, vsSource, fsSource);
|
56 |
+
|
57 |
+
// Initialize shader program with given vertex and fragment shaders
|
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 |
+
return shaderProgram;
|
73 |
+
}
|
74 |
+
|
75 |
+
// Setup buffers, matrices, etc.
|
76 |
+
let snakeSegments = [];
|
77 |
+
let foodPosition = [0, 0, 0];
|
78 |
+
let angle = 0;
|
79 |
+
|
80 |
+
function initBuffers() {
|
81 |
+
// Set up initial snake and food
|
82 |
+
// Here we would define positions on a 3D sphere for snake and food
|
83 |
+
}
|
84 |
+
|
85 |
+
function drawScene() {
|
86 |
+
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
|
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 |
+
// Draw snake, food, etc. on the sphere
|
94 |
+
}
|
95 |
+
|
96 |
+
// Game logic
|
97 |
+
function update() {
|
98 |
+
// Move snake, check for food collision, update food if eaten, etc.
|
99 |
+
}
|
100 |
+
|
101 |
+
// Animation loop
|
102 |
+
function render() {
|
103 |
+
update();
|
104 |
+
drawScene();
|
105 |
+
requestAnimationFrame(render);
|
106 |
+
}
|
107 |
+
|
108 |
+
// Start rendering loop
|
109 |
+
render();
|
110 |
+
|
111 |
+
// Handle user interaction for rotating the sphere
|
112 |
+
document.addEventListener('mousemove', onMouseMove);
|
113 |
+
function onMouseMove(e) {
|
114 |
+
// Implement rotation of the sphere based on mouse movement
|
115 |
+
}
|
116 |
+
</script>
|
117 |
+
</body>
|
118 |
+
</html>
|