-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake2.html
215 lines (189 loc) · 5.56 KB
/
snake2.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
<!doctype html>
<html>
<style>
.box {
background: #FFF;
width: 15px;
height: 15px;
border: 1px solid #000;
}
.snake {
background: #F00;
width: 15px;
height: 15px;
border: 1px solid white;
}
.food {
background: #0F0;
width: 15px;
height: 15px;
border: 1px solid white;
}
#test {
position: absolute;
left: 30px;
top: 30px;
}
a {
text-decoration: none;
}
.scoreSnake {
position: absolute;
left: 821px;
top: 89px;
}
</style>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<head>
<h1>Snake 2</h1>
</head>
<script type="text/javascript">
Array.prototype.indexOf = function (val) {
for (var i = 0; i < this.length; i++) {
if (this[i] == val) return i;
}
return -1;
};
Array.prototype.remove = function (val) {
var index = this.indexOf(val);
if (index > -1) {
this.splice(index, 1);
}
};
var speed = 400;
var row = 20;
var col = 20;
var body = [{'x': null, 'y': null}];
var foodx = null;
var foody = null;
var next_x;
var next_y;
var direction = 'PAUSE'; //u, d, l, r
var dtime;
var score=0;
function createTable(r, c) {
var o = document.body;
var table = document.createElement("table");
var tbody = document.createElement("tbody");
//table.border = 1;
table.setAttribute("class", "box");
//table.class = 'box';
for (var i = 0; i <= r; i++) {
var tr = document.createElement("tr");
for (var j = 0; j <= c; j++) {
var td = document.createElement("td");
var td_id = j + "." + i;
td_id = td_id;
td.innerHTML = ' ';
td.id = td_id;
td.setAttribute("class", "box");
tr.appendChild(td);
}
tbody.appendChild(tr);
}
table.appendChild(tbody);
o.appendChild(table);
}
function init_snake() {
//put snake//put food
while (body[0]['x'] == foodx && body[0]['y'] == foody) {//确保蛇头和食物不出现在同一点上
body[0]['x'] = Math.floor(Math.random() * row);
body[0]['y'] = Math.floor(Math.random() * col);
foodx = Math.floor(Math.random() * row);
foody = Math.floor(Math.random() * col);
}
document.getElementById(body[0]['x'] + '.' + body[0]['y']).setAttribute("class", 'snake');
document.getElementById(foodx + '.' + foody).setAttribute("class", 'food');
console.info('inti map ok');
}
function get_direction() {
document.onkeydown = function (e) {
//get key input
var ev = e || window.event;
code = ev.keyCode;
var new_direction;
if (code == 39 && direction != 'l'){
direction = 'r'
} else if (code == 37 && direction != 'r'){
direction = 'l'
} else if (code == 38 && direction != 'd') {
direction = 'u'
} else if (code == 40 && direction != 'u') {
direction = 'd'
} else {
//any key to pause
direction = 'PAUSE'
}
}
}
function create_food() {
var x_list = [];
var y_list = [];
for (var i = 0; i <= col; i++){
x_list.push(i);
}
for (var ii = 0; ii <= row; ii++){
y_list.push(ii);
}
//console.info(x_list, y_list);
for (var n in body) {
x_list.remove(body[n]['x']);
y_list.remove(body[n]['y']);
}
var x_index = Math.floor(Math.random() * 10) % x_list.length;
var y_index = Math.floor(Math.random() * 10) % y_list.length;
foodx = x_list[x_index];
foody = y_list[y_index];
//console.info(foodx, foody);
document.getElementById(foodx + '.' + foody).setAttribute('class', 'food');
}
function rush() {
var next_cls = document.getElementById(next_x + '.' + next_y).classList[0];
body.unshift({'x': next_x, 'y': next_y});
document.getElementById(next_x + '.' + next_y).setAttribute('class','snake');
if (next_cls == 'box') {
var tail = body.pop();
document.getElementById(tail['x'] + '.' + tail['y']).setAttribute('class','box');
} else if (next_cls == 'food') {
score += 100;
$('#score').text(score);
create_food();
}else{
}
}
function move() {
// console.info(direction);
next_x = body[0]['x'];
next_y = body[0]['y'];
if (direction == 'r') {
next_x = body[0]['x'] + 1;
}
if (direction == 'l') {
next_x = body[0]['x'] - 1;
}
if (direction == 'u') {
next_y = body[0]['y'] - 1;
}
if (direction == 'd') {
next_y = body[0]['y'] + 1;
}
rush();
}
function run() {
get_direction();
if (direction != 'PAUSE') {
move();
}
}
$(function () {
createTable(row, col);
init_snake();
//run game
dtime = setInterval("run()", speed);
});
</script>
<body>
</table>
<div class="scoreSnake"><span id="score" style="color:red;left:">0</span> <span>分</span></div>
</body>
</html>