-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebgl-snake.html
172 lines (157 loc) · 4.77 KB
/
webgl-snake.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>webgl入门</title>
<script src="./js/initShader.js"></script>
<style>
html,
body {
width: 100vw;
height: 100vh;
padding: 0;
margin: 0;
display: flex;
justify-content: center;
align-items: center;
}
body::after {
content: '';
display: block;
width: 1px;
height: 500px;
background-color: aqua;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
body::before {
content: '';
display: block;
width: 500px;
height: 1px;
background-color: darkviolet;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
</style>
</head>
<body>
<canvas id="draw1" width="400" height="400" style="background: pink;">浏览器不支持canvas</canvas>
</body>
</html>
<script type="module">
const draw1 = document.getElementById('draw1');
const gl = draw1.getContext('webgl');
const VERTEX_SHADER_SOURCE = `
attribute vec4 aPosition;
void main(){
gl_Position=aPosition;
gl_PointSize=10.0;
}
`; //顶点
const FRAGMENT_SHADER_SOURCE = `
precision mediump float;
uniform vec2 uColor;
void main(){
gl_FragColor=vec4(uColor.r,uColor.g,0.0,1.0);
}
`;
const program = initShader(gl, VERTEX_SHADER_SOURCE, FRAGMENT_SHADER_SOURCE);
const aPosition = gl.getAttribLocation(program, 'aPosition');//第二个参数是源码里attribute声明的变量名
const uColor = gl.getUniformLocation(program, 'uColor');
gl.uniform2f(uColor, 1.0, 1.0);
//蛇身长度
let points = [{ x: 0, y: 0 }];
//食物的坐标
const random = {
isConnect: true,//判断是否更新食物坐标
};
//初始移动速度
let originSpeed = 0.02;
//行动速度
let speed = originSpeed;
//当前移动方向
let direction = 'x';
//允许吃掉食物的误差范围
let base = 1.5;
document.onkeydown = (e) => {
switch (e.keyCode) {
case 37:
//左
direction = 'x';
speed = -originSpeed;
break;
case 38:
//上
direction = 'y';
speed = originSpeed;
break;
case 39:
//右
direction = 'x';
speed = originSpeed;
break;
case 40:
//下
direction = 'y';
speed = -originSpeed;
break;
}
}
let timer = null;
function start() {
timer = setInterval(() => {
//边界判断
if (points[0].x > 1.0 || points[0].x < -1.0 || points[0].y > 1.0 || points[0].y < -1.0) {
alert('游戏结束');
restart();
}
if (points[0].x > random.x - base * originSpeed
&& points[0].x < random.x + base * originSpeed
&& points[0].y > random.y - base * originSpeed
&& points[0].y < random.y + base * originSpeed) {
points.push({ x: random.x, y: random.y }); //吃进食物,蛇身边长
random.isConnect = true;
}
createRandom();
draw();
}, 100)
}
start();
function restart() {
clearInterval(timer);
points = [{ x: 0, y: 0 }];
speed = originSpeed;
direction = 'x';
start();
}
function draw() {
gl.vertexAttrib2f(aPosition, random.x, random.y);
gl.drawArrays(gl.POINTS, 0, 1);
let prePoint = { ...points[0] };//保留上一个段身体的方向
for (const i in points) {
if (i == 0) {
points[0][direction] += speed;
} else {
points[i] = { ...prePoint };
points[i][direction] -= speed;
prePoint = { ...points[i]};
}
gl.vertexAttrib2f(aPosition, points[i].x, points[i].y);
gl.drawArrays(gl.POINTS, 0, 1);
}
}
function createRandom() {
if (random.isConnect) {
random.x = Math.random() * 2 - 1;
random.y = Math.random() * 2 - 1;
random.isConnect = false;
}
}
</script>