蛇を食いしん坊にする.6
5448 ワード
for(var row=0;row<20;row++){
document.write("<tr>");
for(var rank=0;rank<20;rank++){
document.write("<td></td>");
}
document.write("</tr>");
}
スコア:
/* */
var snake=new Array();
snake[0]=53;
snake[1]=54;
snake[2]=55;
/* */
var row=document.getElementsByTagName("tr");// tr
var arr=new Array();
for(var i=0;i<20;i++){
var rank=row[i].getElementsByTagName("td");
arr[i]=new Array();
for(var j=0;j<20;j++){
arr[i][j]=rank[j];
};
};
/*
var k=0;
for(var i=0;i<20;i++){
for(var j=0;j<20;j++){
arr[i][j].innerHTML=i+","+j+","+k;
k++;
};
};
*/
/* */
function snake_show(){
for(var i=0;i<snake.length;i++){
var num = snake[i];
var row_num=(~~(num/20));
var rank_num=(num-row_num*20);
arr[row_num][rank_num].style.backgroundColor="red";
};
}
function snake_show_2(){
var snake_tail=snake[snake.length-1]
var snake_tail_row_num=(~~(snake_tail/20));
var snake_tail_rank_num=(snake_tail-snake_tail_row_num*20);
arr[snake_tail_row_num][snake_tail_rank_num].style.backgroundColor="";
}
/* */
var food_num=(~~(Math.random()*200))-(0);
function food(){
var food_row_num=(~~(food_num/20));
var food_rank_num=(food_num-food_row_num*20);
arr[food_row_num][food_rank_num].style.backgroundColor="blue";
}
/* */
function direction(e){
switch (e){
case "up":
var num = snake[0]-20;
break;
case "left" :
var num = snake[0]-1;
break;
case "right" :
var num = snake[0]+1;
break;
case "down" :
var num = snake[0]+20;
break;
}
snake.unshift(num);
snake_show_2();
if(food_num==num){
food_num=(~~(Math.random()*200));
food();
}else{
snake.pop();
}
snake_show();
}
function score(){
document.getElementById("score").innerHTML=snake.length-3;
}
food();
snake_show();
//
console.log(snake);
var auto=setInterval("direction('left')",500);
document.onkeydown=function(event){
var e = event || window.event || arguments.callee.caller.arguments[0];
if(e && e.keyCode==38){//
window.clearInterval(auto);
direction("up");
auto=setInterval("direction('up')",500);
setInterval(score,500);
}else if(e && e.keyCode==37){
window.clearInterval(auto);//
direction("left");
auto=setInterval("direction('left')",500);
setInterval(score,500);
}else if(e && e.keyCode==39){//
window.clearInterval(auto);
direction("right");
auto=setInterval("direction('right')",500);
setInterval(score,500);
}else if(e && e.keyCode==40){//
window.clearInterval(auto);
direction("down");
auto=setInterval("direction('down')",500);
setInterval(score,500);
}
};
setInterval(score,500);