-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtanchishe.html
216 lines (216 loc) · 9.79 KB
/
tanchishe.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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>贪吃蛇</title>
</head>
<style>
body {
margin: 0;
padding: 0;
}
.main {
width: 800px;
height: 400px;
margin: 50px auto;
}
.btn {
width: 100px;
height: 40px;
}
.map {
position: relative;
width: 800px;
height: 400px;
background: #ccc;
}
</style>
<body>
<div class="main">
<button class="btn" id="begin">开始游戏</button>
<div class="map" id="map"></div>
<script type="text/javascript">
var map = document.getElementById('map');
// 使用构造方法创建蛇,
function Snake()
{
// 设置蛇的宽、高、默认走的方向
this.width = 10;
this.height = 10;
this.direction = 'right';
// 记住蛇的状态,当吃完食物的时候,就要加一个,初始为3个小点为一个蛇,
this.body = [
{x:2, y:0}, // 蛇头,第一个点
{x:1, y:0}, // 蛇脖子,第二个点
{x:0, y:0} // 蛇尾,第三个点
];
// 显示蛇
this.display = function() {
// 创建蛇
for (var i=0; i<this.body.length; i++) {
if (this.body[i].x != null) { // 当吃到食物时,x==null,不能新建,不然会在0,0处新建一个
var s = document.createElement('div');
// 将节点保存到状态中,以便于后面删除
this.body[i].flag = s;
// 设置宽高
s.style.width = this.width + 'px';
s.style.height = this.height + 'px';
s.style.borderRadius = "50%";
s.style.background = "rgb(" + Math.floor(Math.random()*256) + "," + Math.floor(Math.random()*256) + "," + Math.floor(Math.random()*256) + ")";
// 设置位置
s.style.position = 'absolute';
s.style.left = this.body[i].x * this.width + 'px';
s.style.top = this.body[i].y * this.height + 'px';
// 添加进去
map.appendChild(s);
}
}
};
// 让蛇跑起来,后一个元素到前一个元素的位置
// 蛇头根据方向处理,所以i不能等于0
this.run = function() {
// 后一个元素到前一个元素的位置
for (var i=this.body.length-1; i>0; i--) {
this.body[i].x = this.body[i-1].x;
this.body[i].y = this.body[i-1].y;
}
// 根据方向处理蛇头
switch(this.direction)
{
case "left":
this.body[0].x -= 1;
break;
case "right":
this.body[0].x += 1;
break;
case "up":
this.body[0].y -= 1;
break;
case "down":
this.body[0].y += 1;
break;
}
// 判断是否出界,一蛇头判断,出界的话,
if (this.body[0].x < 0 || this.body[0].x > 79 || this.body[0].y < 0 || this.body[0].y > 39) {
clearInterval(timer); // 清除定时器,
alert("你瞎吗?撞死了!");
// 删除旧的
for (var i=0; i<this.body.length; i++) {
if (this.body[i].flag != null) { // 如果刚吃完就死掉,会加一个值为null的
map.removeChild(this.body[i].flag);
}
}
this.body = [ // 回到初始状态,
{x:2, y:0},
{x:1, y:0},
{x:0, y:0}
];
this.direction = 'right';
this.display(); // 显示初始状态
return false; // 结束
}
// 判断蛇头吃到食物,xy坐标重合,
if (this.body[0].x == food.x && this.body[0].y == food.y) {
// 蛇加一节,因为根据最后节点定,下面display时,会自动赋值的
this.body.push({x:null, y:null, flag: null});
// 清除食物,重新生成食物
map.removeChild(food.flag);
food.display();
}
// 吃到自己死亡,从第五个开始与头判断,因为前四个永远撞不到
for (var i=4; i<this.body.length; i++) {
if (this.body[0].x == this.body[i].x && this.body[0].y == this.body[i].y) {
clearInterval(timer); // 清除定时器,
alert("傻子!你怎么能吃自己呢?");
// 删除旧的
for (var i=0; i<this.body.length; i++) {
if (this.body[i].flag != null) { // 如果刚吃完就死掉,会加一个值为null的
map.removeChild(this.body[i].flag);
}
}
this.body = [ // 回到初始状态,
{x:2, y:0},
{x:1, y:0},
{x:0, y:0}
];
this.direction = 'right';
this.display(); // 显示初始状态
return false; // 结束
}
}
// 先删掉初始的蛇,在显示新蛇
for (var i=0; i<this.body.length; i++) {
if (this.body[i].flag != null) { // 当吃到食物时,flag是等于null,且不能删除
map.removeChild(this.body[i].flag);
}
}
// 重新显示蛇
this.display();
}
}
// 构造食物
function Food()
{
this.width = 10;
this.height = 10;
this.display = function() {
var f = document.createElement('div');
this.flag = f;
f.style.width = this.width + 'px';
f.style.height = this.height + 'px';
f.style.background = 'red';
f.style.borderRadius = '50%';
f.style.position = 'absolute';
this.x = Math.floor(Math.random()*80);
this.y = Math.floor(Math.random()*40);
f.style.left = this.x * this.width + 'px';
f.style.top = this.y * this.height + 'px';
map.appendChild(f);
}
}
var snake = new Snake();
var food = new Food();
snake.display(); // 初始化显示
food.display();
// 给body加按键事件,上下左右
document.body.onkeydown = function(e) {
// 有事件对象就用事件对象,没有就自己创建一个,兼容低版本浏览器
var ev = e || window.event;
switch(ev.keyCode)
{
case 38:
if (snake.direction != 'down') { // 不允许返回,向上的时候不能向下
snake.direction = "up";
}
break;
case 40:
if (snake.direction != "up") {
snake.direction = "down";
}
break;
case 37:
if (snake.direction != "right") {
snake.direction = "left";
}
break;
case 39:
if (snake.direction != "left") {
snake.direction = "right";
}
break;
}
};
// 点击开始时,动起来
var begin = document.getElementById('begin');
var timer;
begin.onclick = function() {
clearInterval(timer);
// timer = setInterval(snake.run(), 500); // 先执行run函数,把执行得到的结果,每500毫秒执行一次,不会在执行内部代码
timer = setInterval("snake.run()", 500); // 小技巧,每500毫秒执行字符串,字符串执行内部代码
};
</script>
</div>
</body>
</html>