好久没接触javascript,近日看了看,试试手
界面是参考 lxx8402的俄罗斯方块方法,觉得不错,借用了谢谢,不要p我
原码如下,写的仓促,也没有放积分,参数可以自己修改前面的默认值,
初次发布,恳请交流指教~~~*_*:
tcs.html:
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<title> new document </title>
<meta name="generator" content="editplus">
<meta name="author" content="">
<meta name="keywords" content="">
<meta name="description" content="">
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<style>
.btnup{
border-left:solid #ffffff 1px;
border-top:solid #ffffff 1px;
border-right:solid #828486 1px;
border-bottom:solid #828486 1px;
font-size: 9pt;
cursor:default
}
tr{
font-family:"宋体";
font-size: 9pt;
cursor: default
}
</style>
</head>
<script language="javascript">
<!--
var row = 25,col = 25,size=16;
var delay = 300;//移动延迟
var paused = false;
var end = false
var timerid;
var s = new snake();
var n = new nut();
var nutad = new array();
nutad[0] = -1;
nutad[1] = -1;
var color = new array(5);
color[0] = "#d0d0d0";
color[1] = "red";
color[2] = "green"
color[3] = "blue";
color[4] = "yellow";
var move_direction = new array();
var curr_direction = new array();
move_direction[0] = 1;
move_direction[1] = 0;
//========================================
//定义果实
function nut()
{
this.x = -1;
this.y = -1;
}
function nut_show()
{
if (this.x<0 || this.y < 0 )
return;
obj = document.all("main" + this.x + "#" + this.y);
obj.style.background = color[2];
}
function nut_create()
{
var inx = -1;
var iny = -1;
for(;;)
{
var cur = true;
inx = math.round(math.random() * (row-1));
iny = math.round(math.random() * (col-1));
for (var i = 0; i<s.items.length ; i++)
{
if (s.items[i].x == inx && s.items[i].y == iny)
{
cur = false;
break;
}
}
if (cur)
{
break;
}
}
this.x = inx;
this.y = iny;
nutad[0] = inx;
nutad[1] = iny;
this.show();
}
function nut_eated()
{
if (this.x<0 || this.y < 0)
return;
obj = document.all("main" + this.x + "#" + this.y);
obj.style.background = color[0];
}
nut.prototype.create = nut_create;
nut.prototype.show = nut_show;
nut.prototype.eated = nut_eated;
//========================================
//定义蛇体
function snake()
{
this.items = new array();
}
function snake_eat(nt)
{
var s_length = this.items.length;
this.items[s_length] = nt;
}
function snake_move(dir)
{
this.destroy();
var y = this.items[0].y;
var x = this.items[0].x;
var nx = x + dir[1];
var ny = y + dir[0];
if (nx <0 || nx >= row || ny <0 || ny >= col || crossed(nx,ny))
{
end = true;
return;
}
caneat(nx,ny);
for (var i = this.items.length-1 ; i>=0 ; i --)
{
if (i != 0)
{
this.items[i].move(this.items[i-1].x,this.items[i-1].y);
}
else
{
this.items[i].move(nx,ny);
}
}
this.reload();
}
function caneat(nx,ny)
{
if (nx == nutad[0] && ny == nutad[1])
{
var lst = s.items[s.items.length-1];
var nd = new snakenode(lst.x,lst.y);
s.eat(nd);
n.eated();
n.create();
}
}
//禁止穿越自身节点
function crossed( nx, ny )
{
for (var i = 0; i<s.items.length; i++)
{
if (s.items[i].x == nx && s.items[i].y == ny)
{
return true;
}
}
return false;