-
Notifications
You must be signed in to change notification settings - Fork 0
/
she.html
187 lines (180 loc) · 6.88 KB
/
she.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
<html>
<head>
<title>贪吃蛇</title>
<script src="http://sandbox.runjs.cn/uploads/rs/92/ohr6hjmq/jQuery.js" type="text/javascript"></script>
<style type="text/css">
.show{
background-color:Gray;
}
span{
width:5px;
height:5px;
background-color:Gray;
border:1px solid black;
display:block;
float:left;
color:white;
}
.select{
background-color:white;
border:1px solid white;
}
</style>
</head>
<script>
//http://runjs.cn/code/naffbozh
var col = 50;
var row = 50;
var SnackList = new Array;
var GoDir = "→"//方向
var speed = 100; //速度
var timergo;
var x;
var y;
var integral = 0;
function Start() {
GoDir = "→";
$("#show").html("");
integral = 0;
$("#dintegral").html("积分:0");
SnackList = new Array();
clearInterval(timergo);
for (var i = 0; i < row; i++) {//布局
for (var j = 0; j < col; j++) {
$("#show").append("<span col='" + j + "' row='" + i + "' ></span>");
}
}
LoadSnack(); //初始加载食物
xy(0, 49); //生成随机食物
$(document).keyup(function (event) {
switch (event.which) {
case 37: //左
if (GoDir != "→") {
GoDir = "←";
}
break;
case 38: //上
if (GoDir != "↓") {
GoDir = "↑";
}
break;
case 39: //右
if (GoDir != "←") {
GoDir = "→";
}
break;
case 40: //下
if (GoDir != "↑") {
GoDir = "↓";
}
break;
default:
}
});
timergo = setInterval(function () {
var gox = SnackList[0].row;
var goy = SnackList[0].col;
switch (GoDir) {
case "←": //左
if ((parseInt(SnackList[0].col, 10) - 1) >= 0) {
SnackList[0].col = parseInt(SnackList[0].col, 10) - 1;
} else {
alert("游戏结束撞到左面");
clearInterval(timergo);
return;
}
break;
case "↑": //上
if ((parseInt(SnackList[0].row, 10) - 1) >= 0) {
SnackList[0].row = parseInt(SnackList[0].row, 10) - 1;
} else {
alert("游戏结束撞到上面");
clearInterval(timergo);
return;
}
break;
case "→": //右
if ((parseInt(SnackList[0].col, 10) + 1) < 50) {
SnackList[0].col = parseInt(SnackList[0].col, 10) + 1;
} else {
alert("游戏结束撞到右面");
clearInterval(timergo);
return;
}
break;
case "↓": //下
if ((parseInt(SnackList[0].row, 10) + 1) < 50) {
SnackList[0].row = parseInt(SnackList[0].row, 10) + 1;
} else {
alert("游戏结束撞到下面");
clearInterval(timergo);
return;
}
break;
default:
} //行走
$("#show [row='" + gox + "'][col='" + goy + "']").removeClass("select");
$("#show [row='" + SnackList[0].row + "'][col='" + SnackList[0].col + "']").removeClass("select").addClass("select");
for (var i = 1; i < SnackList.length; i++) {
var nextx = SnackList[i].row;
var nexty = SnackList[i].col;
SnackList[i].row = gox;
SnackList[i].col = goy;
$("#show [row='" + nextx + "'][col='" + nexty + "']").removeClass("select");
$("#show [row='" + SnackList[i].row + "'][col='" + SnackList[i].col + "']").removeClass("select").addClass("select");
if (SnackList[0].row == SnackList[i].row && SnackList[0].col == SnackList[i].col) {
alert("游戏结束咬到了自己");
clearInterval(timergo);
return;
}
gox = nextx;
goy = nexty;
}
//是否吃到食物
if (SnackList[0].row == x && SnackList[0].col == y) {
var Snacks = new Array();
Snacks["row"] = gox;
Snacks["col"] = goy;
SnackList.push(Snacks);
xy(0, 49); //生成新食物
integral++;
$("#dintegral").html("积分:" + integral);
}
}, speed);
}
function LoadSnack() {
for (var i = 25; i >= 20; i--) {
$("#show [row='24'][col='" + i + "']").removeClass("select").addClass("select");
var Snacks = new Array();
Snacks["row"] = 24;
Snacks["col"] = i;
SnackList.push(Snacks);
}
}
var xy = function (min, max) {
var RetNum = "";
var IsOk = true;
while (IsOk) {
x = Math.floor(min + Math.random() * (max - min));
y = Math.floor(min + Math.random() * (max - min));
RetNum = "[row='" + x + "'][col='" + y + "']";
if ($("#show " + RetNum).hasClass("select")) {
if ($("#show .select").length == $("#show").children().length) {
IsOk = false;
RetNum = "NO";
}
} else {
IsOk = false;
}
}
if (RetNum == "NO") {
alert("挑战成功");
} else {
$("#show " + RetNum).removeClass("select").addClass("select");
}
}</script>
<body>
<input type="button" value="开始" onclick="Start()" /><div id="dintegral"></div>
<div id="show" style="width: 350px; height: 350px;"></div>
</body>
</html>