-
Notifications
You must be signed in to change notification settings - Fork 0
/
script1.js
211 lines (192 loc) · 5.73 KB
/
script1.js
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
// Add your JavaScript below!
$(document).ready(function(){
var alive=true;
var score=0
var timer;
var fruitObj;
var snakeObj;
var timer2;
var isxj=false;
var NAME="Little Kitty";
var ima="dddddd.jpg";
var audio= new Howl({
urls: ['rw437.mp3'],
})
var death= new Howl({
urls: ['death.mp3'],
})
var dog= new Howl({
urls: ['dog.mp3'],
})
var moveBegin=function(){
timer=setTimeout(function(){
snakeMove(5,"right");
},1000/(score+1))
};
var snakeMove = function(speed,dir) {
switch(dir){
case "right":{
snakeObj.snakeleft +=speed;
}
break;
case "left":{
snakeObj.snakeleft -=speed;
}
break;
case "top":{
snakeObj.snakeTop -=speed;
}
break;
case "bottom":{
snakeObj.snakeTop +=speed;
}
break;
}
$('#snake').css("left",snakeObj.snakeleft+'px');
$('#snake').css("top", snakeObj.snakeTop +'px');
timer=setTimeout(function(){
snakeMove(10,dir);
},1000/(score+1));
check();
}
var check=function(){
if(ishit()){
if(isxj){
dog.play();
clearTimeout(timer);
alert(NAME+" meets a dog, and gets bitten! \nYou got him "+score+" rats.");
alive=false;
return
}
score+=1;
$('#snake').width(20+10*score+"px");
$('#snake').height(20+10*score+"px");
fruitObj.remove();
makefruit();
audio.play();
$('#score').html(score);
$('#speed').html(score+1);
}
if(isdead()){
death.play();
clearTimeout(timer);
alert(NAME+" ran into the wall and gets headache! \nYou got him "+score+" rats.");
alive=false;
}
}
var makefruit=function(){
fruitObj = $('<div id="fruit">'+'</div>');
$('#gamefield').append(fruitObj);
$('#fruit').width("20px");
$('#fruit').height("20px");
$('#fruit').css("position","absolute");
do
{fruitObj.fruitLeft = Math.floor(Math.random()*380);
fruitObj.fruitTop = Math.floor(Math.random()*380);
}while(ishit());
$('#fruit').css("left",fruitObj.fruitLeft+'px');
$('#fruit').css("top",fruitObj.fruitTop+'px');
isxj=(Math.random()>0.7);
if(isxj)
{$('#fruit').css("background-image",'url(ssss.jpg)');
if(timer2) clearTimeout(timer2);
timer2=setTimeout(function(){
fruitObj.remove();
makefruit();
},5000);
}
}
var makesnake = function(){
snakeObj = $('<div id="snake">'+'</div>');
$('#gamefield').append(snakeObj);
$('#snake').width(20+10*score+"px");
$('#snake').height(20+10*score+"px");
$('#snake').css("background-image","url("+ima+")");
$('#snake').css("position","absolute");
snakeObj.snakeleft = Math.floor(Math.random()*380);
snakeObj.snakeTop = Math.floor(Math.random()*380);
$('#snake').css("left",snakeObj.snakeleft+'px');
$('#snake').css("top", snakeObj.snakeTop +'px');
};
var ishit=function(){
fruitObj.fruitRight = fruitObj.fruitLeft+20;
fruitObj.fruitBottom = fruitObj.fruitTop +20;
snakeObj.snakeRight = snakeObj.snakeleft+20+10*score;
snakeObj.snakeBottom = snakeObj.snakeTop +20+10*score;
if( fruitObj.fruitRight>snakeObj.snakeleft&& fruitObj.fruitLeft<snakeObj.snakeRight&&fruitObj.fruitTop<snakeObj.snakeBottom&&fruitObj.fruitBottom>snakeObj.snakeTop){
return true;
}
return false;
};
var isdead=function(){
snakeObj.snakeRight = snakeObj.snakeleft+20+10*score;
snakeObj.snakeBottom = snakeObj.snakeTop +20+10*score;
if (snakeObj.snakeRight>400|| snakeObj.snakeleft<0||snakeObj.snakeBottom>400|| snakeObj.snakeTop<0) {
return true;
}
return false;
};
var clearall=function(){
score=0;
if (timer) {
clearTimeout(timer);
};
if (fruitObj) {
fruitObj.remove();
};
if (snakeObj) {
snakeObj.remove();
};
if(timer2){
clearTimeout(timer2);
isxj=false;
}
}
var begin=function(){
clearall();
makesnake();
makefruit();
moveBegin();
alive=true;
death.play();
$('#next').html(NAME+" gets stronger and faster when he catches a rats");
$('#next').append("<p>Don't touch the wall!!</p>");
$('#next').append("<p>Attention at dogs!!</p>");
}
$('#biaoti').html(NAME+"'s history");
$('#next').html("Can you help "+NAME+" to get his rats");
$(document).keydown(function(key){
switch(parseInt(key.which,10)){
case 27:
$(document).abort();
break;
case 32:
begin();
break;
case 38:
if (alive){
clearTimeout(timer);
snakeMove(10,"top");
}
break;
case 40:
if (alive){
clearTimeout(timer);
snakeMove(10,"bottom");
}
break;
case 37:
if (alive){
clearTimeout(timer);
snakeMove(10,"left");
}
break;
case 39:
if (alive){
clearTimeout(timer);
snakeMove(10,"right");
}
break;
}
});
});