let timre = setInterval(start_snake,200);
let lis = document.getElementsByTagName(
"li"
);
function
start_snake(){
let left=parseFloat(lis[0].style.left);
let top= parseFloat(lis[0].style.top);
switch
(direction) {
case
"left"
:left = (left-20);
break
;
case
"up"
:top = (top-20);
break
;
case
"right"
:left = (left+20);
break
;
case
"down"
:top = (top+20);
break
;
default
:;
}
if
(left<0 || left>800-20 || top<0 || top>600-20){
window.clearInterval(timre);
alert(
"亲,您Game Over"
);
return
;
}
for
(
var
i=lis.length-1;i>0;i--){
lis[i].style.left = lis[i-1].style.left;
lis[i].style.top = lis[i-1].style.top;
}
lis[0].style.left = left+
"px"
;
lis[0].style.top = top+
"px"
;
if
(left == food_left && top == food_top){
eat();
}
}
function
eat() {
$(
"js_bg"
).removeChild($(
"foodDiv"
));
js_food();
let li = document.createElement(
"li"
);
$(
"js_bg"
).appendChild(li);
}