Copy for(i=0;i<currentBody.length;i++){//一開始的黑色
$("#"+currentBody[i]).css('background',snackColor)
};
function start(){
var starts = setInterval(function(){
if(direction=="right"){
if(currentBody[0]!=11&¤tBody[0]!=23&¤tBody[0]!=35&¤tBody[0]!=47&¤tBody[0]!=59&¤tBody[0]!=71&¤tBody[0]!=83&¤tBody[0]!=95){
current=currentBody[((currentBody.length)-1)];//a[3]為最後一元素但a.length為4,所以減1,current表示蛇陣列的尾巴id,記錄上一輪尾巴的坐標
var last = currentBody[(currentBody.length)-1];//在移動前記錄尾巴元素
for(i=(currentBody.length)-1;i>0;i--){///前進時後一個元素id護變為前一個id
currentBody[i]=currentBody[i-1]
};
cc = currentBody.slice(0);
delete cc[0];
cc.push(last);
currentBody[0]+=1;
if(($.inArray(currentBody[0],cc))!=-1){
if(currentBody[0]!=apple){
clearInterval(starts);
$('body').html("遊戲結束"+score1);
};
};
$("#"+(current)).css("background","white");
for(i=0;i<currentBody.length;i++){//蛇的陣列元素是黑色
$("#"+currentBody[i]).css('background',snackColor)
};
if(currentBody[0]==apple){
currentBody.push(apple);
score1++;
console.log("aa");
$('#score').html("你的分數為"+score1);
for(;;){//如果蘋果產生在蛇身體上,重新產生蘋果直到不在
apple = Math.floor((Math.random() * 95) + 1);
if(($.inArray(apple,currentBody))==-1){
break;
}
};
$("#"+(apple)).css('background','red');//讓蘋果的位置是紅色
};
}else{
clearInterval(starts);
$('body').html("遊戲結束"+score1);
};
};
if(direction=="left"){
if(currentBody[0]!=0&¤tBody[0]!=12&¤tBody[0]!=24&¤tBody[0]!=36&¤tBody[0]!=48&¤tBody[0]!=60&¤tBody[0]!=72&¤tBody[0]!=84){
current=currentBody[((currentBody.length)-1)];//a[3]為最後一元素但a.length為4,所以減1,current表示蛇陣列的尾巴id,記錄上一輪尾巴的坐標
var last = currentBody[(currentBody.length)-1];//在移動前記錄尾巴元素
for(i=(currentBody.length)-1;i>0;i--){///前進時後一個元素id護變為前一個id
currentBody[i]=currentBody[i-1]
};
cc = currentBody.slice(0);
delete cc[0];
//////////看是否撞到身體,使用var last記錄完後,把頭刪掉加上上次的尾巴
cc.push(last);
currentBody[0]-=1;
if(($.inArray(currentBody[0],cc))!=-1){
if(currentBody[0]!=apple){
clearInterval(starts);
$('body').html("遊戲結束"+score1);
};//////////看是否撞到身體,使用var last記錄完後,把頭刪掉加上上次的尾巴
};
$("#"+(current)).css("background","white");
for(i=0;i<currentBody.length;i++){//蛇的陣列元素是黑色
$("#"+currentBody[i]).css('background',snackColor)
};
if(currentBody[0]==apple){
currentBody.push(apple);
score1++;
$('#score').html("你的分數為"+score1);
for(;;){//如果蘋果產生在蛇身體上,重新產生蘋果直到不在
apple = Math.floor((Math.random() * 95) + 1);
if(($.inArray(apple,currentBody))==-1){
break;
}
};
$("#"+(apple)).css('background','red');//讓蘋果的位置是紅色
};
}else{
clearInterval(starts);
$('body').html("遊戲結束"+score1);
};
};
if(direction=="down"){
if(currentBody[0]!=84&¤tBody[0]!=85&¤tBody[0]!=86&¤tBody[0]!=87&¤tBody[0]!=88&¤tBody[0]!=89&¤tBody[0]!=90&¤tBody[0]!=91&¤tBody[0]!=92&¤tBody[0]!=93&¤tBody[0]!=94&¤tBody[0]!=95){
current=currentBody[((currentBody.length)-1)];//a[3]為最後一元素但a.length為4,所以減1,記錄上一輪尾巴的坐標
var last = currentBody[(currentBody.length)-1];//在移動前記錄尾巴元素
for(i=(currentBody.length)-1;i>0;i--){///前進時後一個元素id護變為前一個id
currentBody[i]=currentBody[i-1]
};
cc = currentBody.slice(0);
delete cc[0];
cc.push(last);
currentBody[0]+=12;
if(($.inArray(currentBody[0],cc))!=-1){
if(currentBody[0]!=apple){
clearInterval(starts);
$('body').html("遊戲結束得分為"+score1);
};
};
$("#"+(current)).css("background","white");
for(i=0;i<currentBody.length;i++){//蛇的陣列元素是黑色
$("#"+currentBody[i]).css('background',snackColor)
};
if(currentBody[0]==apple){
currentBody.push(apple);
score1++;
$('#score').html("你的分數為"+score1);
for(;;){//如果蘋果產生在蛇身體上,重新產生蘋果直到不在
apple = Math.floor((Math.random() * 95) + 1);
if(($.inArray(apple,currentBody))==-1){
break;
}
};
$("#"+(apple)).css('background','red');//讓蘋果的位置是紅色
};
}else{
clearInterval(starts);
$('body').html("遊戲結束"+score1);
};
};
if(direction=="up"){
if(currentBody[0]!=0&¤tBody[0]!=1&¤tBody[0]!=2&¤tBody[0]!=3&¤tBody[0]!=4&¤tBody[0]!=5&¤tBody[0]!=6&¤tBody[0]!=7&¤tBody[0]!=8&¤tBody[0]!=9&¤tBody[0]!=10&¤tBody[0]!=11){
/////////
current=currentBody[((currentBody.length)-1)];//a[3]為最後一元素但a.length為4,所以減1,記錄上一輪尾巴的坐標,讓他變白色
////////
var last = currentBody[(currentBody.length)-1];//在移動前記錄尾巴元素
for(i=(currentBody.length)-1;i>0;i--){///前進時後一個元素id護變為前一個id
currentBody[i]=currentBody[i-1]
};
cc = currentBody.slice(0);
delete cc[0];
cc.push(last);
currentBody[0]-=12;
if(($.inArray(currentBody[0],cc))!=-1){
if(currentBody[0]!=apple){
clearInterval(starts);
$('body').html("遊戲結束得分為"+score1);
};
};
//////
$("#"+(current)).css("background","white");
//////
for(i=0;i<currentBody.length;i++){//蛇的陣列元素是黑色
$("#"+currentBody[i]).css('background',snackColor)
};
//////
if(currentBody[0]==apple){
currentBody.push(apple);
score1++;
$('#score').html("你的分數為"+score1);
for(;;){//如果蘋果產生在蛇身體上,重新產生蘋果直到不在
apple = Math.floor((Math.random() * 95) + 1);
if(($.inArray(apple,currentBody))==-1){
break;
}
};
$("#"+(apple)).css('background','red');//讓蘋果的位置是紅色
};
//////
}else{
clearInterval(starts);
$('body').html("遊戲結束"+score1);
};
};
},300);
};