以下要用到jquery-2.1.1.min.js
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
.black{
background-color: black;
}
.bo{
width: 30px;
height: 30px;
}
.snake{
background-color: blue;
}
.food{
background-color: green;
}
</style>
</head>
<body>
<input type="button" value="star" id="kaishi">
<table border="1px">
<tbody id="tbody"></tbody>
</table>
<script src="jquery-2.1.1.min.js" type="text/javascript"></script>
<script>
var height=20;
var width=30;
var timer;
tab(height,width);
var snake= new Object();
snake.body=[[1,1]];
$("#kaishi").on("click",function(){
var a=Math.floor(Math.random()*height+1);
var b=Math.floor(Math.random()*width+1);
$("tbody").children().eq(a).children().eq(b).addClass("food");
snake.direction="right";
for(var i=0;i<snake.body.length;i++){
$("tbody").children().eq(snake.body[i][0]).children().eq(snake.body[i][1]).addClass("snake");
}
timer=setInterval(function(){
if(snake.direction=="right"){
if($("tbody").children().eq(snake.body[0][0]).children().eq(snake.body[0][1]).next().hasClass("black") || $("tbody").children().eq(snake.body[0][0]).children().eq(snake.body[0][1]).next().hasClass("snake")){
alert("GG");
window.clearInterval(timer);
}else{
var arr=[snake.body[0][0],snake.body[0][1]+1];
snake.body.unshift(arr);
$("tbody").children().eq(snake.body[0][0]).children().eq(snake.body[0][1]).addClass("snake");
$("tbody").children().eq(snake.body[snake.body.length-1][0]).children().eq(snake.body[snake.body.length-1][1]).removeClass("snake");
if(!$("tbody").children().eq(snake.body[0][0]).children().eq(snake.body[0][1]).hasClass("food")){
snake.body.pop();
}else{
$("tbody").children().eq(a).children().eq(b).removeClass("food");
a=Math.floor(Math.random()*height+1);
b=Math.floor(Math.random()*width+1);
while(true){
if(!$("tbody").children().eq(a).children().eq(b).hasClass("snake")){
$("tbody").children().eq(a).children().eq(b).addClass("food");
break;
}else{
a=Math.floor(Math.random()*height+1);
b=Math.floor(Math.random()*width+1);
}
}
}
}
}else if(snake.direction=="left"){
if($("tbody").children().eq(snake.body[0][0]).children().eq(snake.body[0][1]).prev().hasClass("black") || $("tbody").children().eq(snake.body[0][0]).children().eq(snake.body[0][1]).prev().hasClass("snake")){
alert("GG");
window.clearInterval(timer);
}else{
var arr=[snake.body[0][0],snake.body[0][1]-1];
snake.body.unshift(arr);
$("tbody").children().eq(snake.body[0][0]).children().eq(snake.body[0][1]).addClass("snake");
$("tbody").children().eq(snake.body[snake.body.length-1][0]).children().eq(snake.body[snake.body.length-1][1]).removeClass("snake");
if(!$("tbody").children().eq(snake.body[0][0]).children().eq(snake.body[0][1]).hasClass("food")){
snake.body.pop();
}else{
$("tbody").children().eq(a).children().eq(b).removeClass("food");
a=Math.floor(Math.random()*height+1);
b=Math.floor(Math.random()*width+1);
while(true){
if(!$("tbody").children().eq(a).children().eq(b).hasClass("snake")){
$("tbody").children().eq(a).children().eq(b).addClass("food");
break;
}else{
a=Math.floor(Math.random()*height+1);
b=Math.floor(Math.random()*width+1);
}
}
}
}
}else if(snake.direction=="top"){
if($("tbody").children().eq(snake.body[0][0]).prev().children().eq(snake.body[0][1]).hasClass("black") || $("tbody").children().eq(snake.body[0][0]).prev().children().eq(snake.body[0][1]).hasClass("snake")){
alert("GG");
window.clearInterval(timer);
}else{
var arr=[snake.body[0][0]-1,snake.body[0][1]];
snake.body.unshift(arr);
$("tbody").children().eq(snake.body[0][0]).children().eq(snake.body[0][1]).addClass("snake");
$("tbody").children().eq(snake.body[snake.body.length-1][0]).children().eq(snake.body[snake.body.length-1][1]).removeClass("snake");
if(!$("tbody").children().eq(snake.body[0][0]).children().eq(snake.body[0][1]).hasClass("food")){
snake.body.pop();
}else{
$("tbody").children().eq(a).children().eq(b).removeClass("food");
a=Math.floor(Math.random()*height+1);
b=Math.floor(Math.random()*width+1);
while(true){
if(!$("tbody").children().eq(a).children().eq(b).hasClass("snake")){
$("tbody").children().eq(a).children().eq(b).addClass("food");
break;
}else{
a=Math.floor(Math.random()*height+1);
b=Math.floor(Math.random()*width+1);
}
}
}
}
}else if(snake.direction=="bottom"){
if($("tbody").children().eq(snake.body[0][0]).next().children().eq(snake.body[0][1]).hasClass("black") || $("tbody").children().eq(snake.body[0][0]).next().children().eq(snake.body[0][1]).hasClass("snake")){
alert("game over");
window.clearInterval(timer);
}else{
var arr=[snake.body[0][0]+1,snake.body[0][1]];
snake.body.unshift(arr);
$("tbody").children().eq(snake.body[0][0]).children().eq(snake.body[0][1]).addClass("snake");
$("tbody").children().eq(snake.body[snake.body.length-1][0]).children().eq(snake.body[snake.body.length-1][1]).removeClass("snake");
if(!$("tbody").children().eq(snake.body[0][0]).children().eq(snake.body[0][1]).hasClass("food")){
snake.body.pop();
}else{
$("tbody").children().eq(a).children().eq(b).removeClass("food");
a=Math.floor(Math.random()*height+1);
b=Math.floor(Math.random()*width+1);
while(true){
if(!$("tbody").children().eq(a).children().eq(b).hasClass("snake")){
$("tbody").children().eq(a).children().eq(b).addClass("food");
break;
}else{
a=Math.floor(Math.random()*height+1);
b=Math.floor(Math.random()*width+1);
}
}
}
}
}
},150);
$(document).on("keydown",function(){
switch(event.keyCode){
case 37:
if(snake.direction=="left"||snake.direction=="right"){
return;
}else{
snake.direction="left";
}
break;
case 38:
if(snake.direction=="top"||snake.direction=="bottom"){
return;
}else{
snake.direction="top";
}
break;
case 39:
if(snake.direction=="left"||snake.direction=="right"){
return;
}else{
snake.direction="right";
}
break;
case 40:
if(snake.direction=="top"||snake.direction=="bottom"){
return;
}else{
snake.direction="bottom";
}
break;
}
})
})
function tab(height,width){
for(var i=1;i<=height+2;i++){
var tr=$("<tr></tr>");
for(var j=1;j<=width+2;j++){
if(i==1||i==height+2){
$(tr).append("<td class='black bo'></td>");
continue;
}
if(j==1||j==width+2){
$(tr).append("<td class='black bo'></td>");
continue;
}
$(tr).append("<td class='bo'></td>");
}
$("#tbody").append(tr);
}
}
</script>
</body>
</html>
结束了,开始玩贪吃蛇吧!