javascript贪吃蛇 昨晚下班没事,写了个javascript贪吃蛇,欢迎拍砖。代码这里贴不下,放在bloghttp://blog.csdn.net/sunxing007/archive/2009/05/14/4187038.aspx 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 以前用.NET 和 VB写过. JS还没试过 修改了一下:1、加入colCount、rowCount控制高宽;2、动态创建td、tr,,不用写在html里;3、兼容Firefox。<html><head> <title>贪吃蛇 Snake v1.0</title><style> body{ } table{ border-collapse: collapse; border:solid #333 1px; } td{ height: 10px; width: 10px; font-size: 0px; } .filled{ background-color:blue; }</style></head><script> function $(id){return document.getElementById(id);} /** * javascript贪吃蛇 v1.0 * author: sunxing007 * 05/14/2009 * 转载请保留 author: sunxing007 谢谢 **/ //贪吃蛇类 var Snake = { tbl: null, /** * body: 蛇身,数组放蛇的每一节, * 数据结构{x:x0, y:y0, color:color0}, * x,y表示坐标,color表示颜色 **/ body: [], //当前移动的方向,取值0,1,2,3, 分别表示向上,右,下,左, 按键盘方向键可以改变它 direction: 0, //定时器 timer: null, //速度 speed: 200, //行数 rowCount: 50, //列数 colCount: 50, //初始化 init: function(){ var colors = ['red','orange','yellow','green','blue','purple','#ccc']; this.tbl = $("main"); var x = 0; var y = 0; var colorIndex = 0; //产生初始移动方向 this.direction = Math.floor(Math.random()*4); for(var row=0;row<this.rowCount;row++){ var tr=this.tbl.insertRow(-1); for(var col=0;col<this.colCount;col++) { var td=tr.insertCell(-1); } } //产生20个松散节点 for(var i=0; i<20; i++){ x = Math.floor(Math.random()*this.colCount); y = Math.floor(Math.random()*this.rowCount); colorIndex = Math.floor(Math.random()*7); if(!this.isCellFilled(x,y)){ this.tbl.rows[y].cells[x].style.backgroundColor = colors[colorIndex]; } } //产生蛇头 while(true){ x = Math.floor(Math.random()*this.colCount); y = Math.floor(Math.random()*this.rowCount); if(!this.isCellFilled(x,y)){ this.tbl.rows[y].cells[x].style.backgroundColor = "black"; this.body.push({x:x,y:y,color:'black'}); break; } } //alert(this.body[0].x); //添加键盘事件 document.onkeydown= function(e){ if (!e)e=window.event; switch(e.keyCode | e.which | e.charCode){ case 37:{//left //阻止蛇倒退走 if(Snake.direction==1){ break; } Snake.direction = 3; break; } case 38:{//up if(Snake.direction==2){ break; } Snake.direction = 0; break; } case 39:{//right if(Snake.direction==3){ break; } Snake.direction = 1; break; } case 40:{//down if(Snake.direction==0){ break; } Snake.direction = 2; break; } } } }, //移动 move: function(){ this.timer = setInterval(function(){ Snake.erase(); Snake.moveOneStep(); Snake.paint(); }, this.speed); }, //移动一节身体 moveOneStep: function(){ if(this.checkNextStep()==-1){ clearInterval(this.timer); alert("Game over!\nPress F5 to Restart."); /** $('btn').disabled = false; $('btn').onclick = function(){ Snake.restart(); }; **/ return; } if(this.checkNextStep()==1){ var _point = this.getNextPos(); var _x = _point.x; var _y = _point.y; var _color = this.getColor(_x,_y); this.body.unshift({x:_x,y:_y,color:_color}); return; } //window.status = this.toString(); var point = this.getNextPos(); var color = this.body[0].color; this.body.pop(); this.body.unshift({x:point.x,y:point.y,color:color}); //调试 //window.status = this.toString(); }, //探寻下一步将走到什么地方 getNextPos: function(){ var x = this.body[0].x; var y = this.body[0].y; var color = this.body[0].color; //向上 if(this.direction==0){ y--; } //向右 else if(this.direction==1){ x++; } //向下 else if(this.direction==2){ y++; } //向左 else{ x--; } //返回一个坐标 return {x:x,y:y}; }, //检查将要移动到的下一步是什么 checkNextStep: function(){ var point = this.getNextPos(); var x = point.x; var y = point.y; if(x<0||x>=this.colCount||y<0||y>=this.rowCount){ return -1;//触边界,游戏结束 } for(var i=0; i<this.body.length; i++){ if(this.body[i].x==x&&this.body[i].y==y){ return -1;//碰到自己的身体 } } if(this.isCellFilled(x,y)){ return 1;//有东西 } return 0;//空地 }, //擦除蛇身 erase: function(){ for(var i=0; i<this.body.length; i++){ this.eraseDot(this.body[i].x, this.body[i].y); } }, //绘制蛇身 paint: function(){ for(var i=0; i<this.body.length; i++){ this.paintDot(this.body[i].x, this.body[i].y,this.body[i].color); } }, //擦除一节 eraseDot: function(x,y){ this.tbl.rows[y].cells[x].style.backgroundColor = ""; }, paintDot: function(x,y,color){ this.tbl.rows[y].cells[x].style.backgroundColor = color; }, //得到一个坐标上的颜色 getColor: function(x,y){ return this.tbl.rows[y].cells[x].style.backgroundColor; }, //用于调试 toString: function(){ var str = ""; for(var i=0; i<this.body.length; i++){ str += "x:" + this.body[i].x + " y:" + this.body[i].y + " color:" + this.body[i].color + " - "; } return str; }, //检查一个坐标点有没有被填充 isCellFilled: function(x,y){ if(this.tbl.rows[y].cells[x].style.backgroundColor == ""){ return false; } return true; }, restart: function(){ if(this.timer){ clearInterval(this.timer); } for(var i=0; i<this.tbl.rows.length;i++){ for(var j=0; j<this.tbl.rows.length; i++){ this.tbl.rows[i].cells[j].style.backgroundColor = ""; } } this.body = []; this.init(); } //; };</script><body onload="Snake.init();">/*******************************************<br />* javascript贪吃蛇 v1.0<br />* author: sunxing007<br />* 05/14/2009<br />* 转载请保留 author: sunxing007 字样,谢谢<br />*******************************************/<br /><button id="btn" onclick="Snake.move();this.disabled=true;">Begin</button><table id="main" width="500" border="1" cellspacing="0" cellpadding="0"></table></body></html> 老大你太有才了。 看来你的勋章不是白拿的。我见过有人积分很高, 不过愿意把别人源代码看完的人不多!佩服你。thank you! 简直太棒了这东西,没想到JAVASCRIPT也能发挥到这种地步。不过我在按上下键的时候网页的垂直滚动条也会跟着动,还有就是如果能增加个暂停就更棒了 !楼主和改代码的兄弟都是我等学习的榜样啊 。 1、firefox不支持event;2、如果食物出现的位置和蛇的身体重叠,那么食物将消失。最后会出现没有食物的情况。 代码修改求教 jquery疑问,大神们,求解... jquery 判断鼠标移动方向 急,javascript的先后顺序问题? (提交另一个frame内form问题)为什么 Form 的 onsubmit 事件不能执行? javascript小问题 关于javascript 的Frame问题 opera浏览器的困惑~! 昨天遇到的一个js和activeX有关的奇怪问题 我的问题总是太多,多多麻烦各位啦·#¥¥ 很奇怪的问题,这一句放在页面就有效,放在js文件就无效 DIV的显示
JS还没试过
修改了一下:
1、加入colCount、rowCount控制高宽;
2、动态创建td、tr,,不用写在html里;
3、兼容Firefox。<html>
<head>
<title>贪吃蛇 Snake v1.0</title>
<style>
body{
}
table{
border-collapse: collapse;
border:solid #333 1px;
}
td{
height: 10px;
width: 10px;
font-size: 0px;
}
.filled{
background-color:blue;
}
</style>
</head>
<script>
function $(id){return document.getElementById(id);}
/**
* javascript贪吃蛇 v1.0
* author: sunxing007
* 05/14/2009
* 转载请保留 author: sunxing007 谢谢
**/
//贪吃蛇类
var Snake = {
tbl: null,
/**
* body: 蛇身,数组放蛇的每一节,
* 数据结构{x:x0, y:y0, color:color0},
* x,y表示坐标,color表示颜色
**/
body: [],
//当前移动的方向,取值0,1,2,3, 分别表示向上,右,下,左, 按键盘方向键可以改变它
direction: 0,
//定时器
timer: null,
//速度
speed: 200,
//行数
rowCount: 50,
//列数
colCount: 50,
//初始化
init: function(){
var colors = ['red','orange','yellow','green','blue','purple','#ccc'];
this.tbl = $("main");
var x = 0;
var y = 0;
var colorIndex = 0;
//产生初始移动方向
this.direction = Math.floor(Math.random()*4);
for(var row=0;row<this.rowCount;row++){
var tr=this.tbl.insertRow(-1);
for(var col=0;col<this.colCount;col++) {
var td=tr.insertCell(-1);
}
}
//产生20个松散节点
for(var i=0; i<20; i++){
x = Math.floor(Math.random()*this.colCount);
y = Math.floor(Math.random()*this.rowCount);
colorIndex = Math.floor(Math.random()*7);
if(!this.isCellFilled(x,y)){
this.tbl.rows[y].cells[x].style.backgroundColor = colors[colorIndex];
}
}
//产生蛇头
while(true){
x = Math.floor(Math.random()*this.colCount);
y = Math.floor(Math.random()*this.rowCount);
if(!this.isCellFilled(x,y)){
this.tbl.rows[y].cells[x].style.backgroundColor = "black";
this.body.push({x:x,y:y,color:'black'});
break;
}
}
//alert(this.body[0].x);
//添加键盘事件
document.onkeydown= function(e){
if (!e)e=window.event;
switch(e.keyCode | e.which | e.charCode){
case 37:{//left
//阻止蛇倒退走
if(Snake.direction==1){
break;
}
Snake.direction = 3;
break;
}
case 38:{//up
if(Snake.direction==2){
break;
}
Snake.direction = 0;
break;
}
case 39:{//right
if(Snake.direction==3){
break;
}
Snake.direction = 1;
break;
}
case 40:{//down
if(Snake.direction==0){
break;
}
Snake.direction = 2;
break;
}
}
}
},
//移动
move: function(){
this.timer = setInterval(function(){
Snake.erase();
Snake.moveOneStep();
Snake.paint();
}, this.speed);
},
//移动一节身体
moveOneStep: function(){
if(this.checkNextStep()==-1){
clearInterval(this.timer);
alert("Game over!\nPress F5 to Restart.");
/**
$('btn').disabled = false;
$('btn').onclick = function(){
Snake.restart();
};
**/
return;
}
if(this.checkNextStep()==1){
var _point = this.getNextPos();
var _x = _point.x;
var _y = _point.y;
var _color = this.getColor(_x,_y);
this.body.unshift({x:_x,y:_y,color:_color});
return;
}
//window.status = this.toString();
var point = this.getNextPos();
var color = this.body[0].color;
this.body.pop();
this.body.unshift({x:point.x,y:point.y,color:color});
//调试
//window.status = this.toString();
},
//探寻下一步将走到什么地方
getNextPos: function(){
var x = this.body[0].x;
var y = this.body[0].y;
var color = this.body[0].color;
//向上
if(this.direction==0){
y--;
}
//向右
else if(this.direction==1){
x++;
}
//向下
else if(this.direction==2){
y++;
}
//向左
else{
x--;
}
//返回一个坐标
return {x:x,y:y};
},
//检查将要移动到的下一步是什么
checkNextStep: function(){
var point = this.getNextPos();
var x = point.x;
var y = point.y;
if(x<0||x>=this.colCount||y<0||y>=this.rowCount){
return -1;//触边界,游戏结束
}
for(var i=0; i<this.body.length; i++){
if(this.body[i].x==x&&this.body[i].y==y){
return -1;//碰到自己的身体
}
}
if(this.isCellFilled(x,y)){
return 1;//有东西
}
return 0;//空地
},
//擦除蛇身
erase: function(){
for(var i=0; i<this.body.length; i++){
this.eraseDot(this.body[i].x, this.body[i].y);
}
},
//绘制蛇身
paint: function(){
for(var i=0; i<this.body.length; i++){
this.paintDot(this.body[i].x, this.body[i].y,this.body[i].color);
}
},
//擦除一节
eraseDot: function(x,y){
this.tbl.rows[y].cells[x].style.backgroundColor = "";
},
paintDot: function(x,y,color){
this.tbl.rows[y].cells[x].style.backgroundColor = color;
},
//得到一个坐标上的颜色
getColor: function(x,y){
return this.tbl.rows[y].cells[x].style.backgroundColor;
},
//用于调试
toString: function(){
var str = "";
for(var i=0; i<this.body.length; i++){
str += "x:" + this.body[i].x + " y:" + this.body[i].y + " color:" + this.body[i].color + " - ";
}
return str;
},
//检查一个坐标点有没有被填充
isCellFilled: function(x,y){
if(this.tbl.rows[y].cells[x].style.backgroundColor == ""){
return false;
}
return true;
},
restart: function(){
if(this.timer){
clearInterval(this.timer);
}
for(var i=0; i<this.tbl.rows.length;i++){
for(var j=0; j<this.tbl.rows.length; i++){
this.tbl.rows[i].cells[j].style.backgroundColor = "";
}
}
this.body = [];
this.init();
}
//;
};
</script>
<body onload="Snake.init();">
/*******************************************<br />
* javascript贪吃蛇 v1.0<br />
* author: sunxing007<br />
* 05/14/2009<br />
* 转载请保留 author: sunxing007 字样,谢谢<br />
*******************************************/<br />
<button id="btn" onclick="Snake.move();this.disabled=true;">Begin</button>
<table id="main" width="500" border="1" cellspacing="0" cellpadding="0"></table>
</body>
</html>
佩服你。
thank you!
2、如果食物出现的位置和蛇的身体重叠,那么食物将消失。最后会出现没有食物的情况。