无聊时写的,稍作修改就可以支持多人游戏的,实在是懒得写下去了
i键:上,j键:左,k键:下,l键:右
<html>
<style>
td {width:2pt;height:2pt;font-size:2pt;}
</style>
<script>
function pos(x,y) {
this.x = x;
this.y = y;
return this;
}
function snake(x,y) {
this.node = new array();
for (var i=0;i<20;i++)
this.node[i] = new pos(x,y);
this.direction = 0;
this.board = null;
this.setboard = function (board) {
this.board = board;
}
this.left = function () {
var c;
with (this)
if (board.check(node[0].x-1,node[0].y)) {
clear();
movenode();
node[0].x–;
c = board.getdot(node[0].x,node[0].y);
draw();
}
else
c = black;
this.direction = 2;
return c;
}
this.right = function () {
var c;
with (this)
if (board.check(node[0].x+1,node[0].y)) {
clear();
movenode();
node[0].x++;
c = board.getdot(node[0].x,node[0].y);
draw();
}
else
c = black;
this.direction = 4;
return c;
}
this.up = function () {
var c;
with (this)
if (board.check(node[0].x,node[0].y-1)) {
clear();
movenode();
node[0].y–;
c = board.getdot(node[0].x,node[0].y);
draw();
}
else
c = black;
this.direction = 1;
return c;
}
this.down = function () {
var c;
with (this)
if (board.check(node[0].x,node[0].y+1)) {
clear();
movenode();
node[0].y++;
c = board.getdot(node[0].x,node[0].y);
draw();
}
else
c = black;
this.direction = 3;
return c;
}
this.movenode = function () {
with (this)
for (var i=node.length-1;i>0;i–) {
node[i].x = node[i-1].x;
node[i].y = node[i-1].y;
}
}
this.draw = function () {
with (this)
board.drawdot(node[0].x, node[0].y);
}
this.clear = function () {
with (this) {
if (node.length>1)
if (node[node.length – 1].x == node[node.length – 2].x &&
node[node.length – 1].y == node[node.length – 2].y)
return;
board.cleardot(node[node.length – 1].x, node[node.length – 1].y);
}
}
this.move = function () {
var c;
with (this) {
if (direction==1)
c = up();
if (direction==2)
c = left();
if (direction==3)
c = down();
if (direction==4)
c = right();
}
return c;
}
}
function board(name,col,row) {
this.name = name;
this.obj = null;
this.col = col;
this.row = row;
this.draw = function () {
var i,j;
document.write(<table id= + this.name + border=0>);
for (j=0;j<this.row;j++) {
document.write(<tr>);
for (i=0;i<this.col;i++)
document.write(<td>&nbsp;</td>);
document.write(</tr>);
}
document.write(</table>);
this.obj = eval(this.name);
}
this.check = function (x,y) {
if (this.obj.rows[y].cells[x].style.background == black)
return false
else
return true;
}
this.getdot = function (x,y) {
return this.obj.rows[y].cells[x].style.background;
}
this.drawdot = function (x,y) {
this.obj.rows[y].cells[x].style.background = black;
}
this.cleardot = function (x,y) {
this.obj.rows[y].cells[x].style.background = white;
}
this.adddot = function () {
var x,y;
with (this) {
do {
x = math.ceil(math.random()*(col-3) + 1);
y = math.floor(math.random()*(row-3) + 1);
}
while (getdot(x,y) != white)
obj.rows[y].cells[x].style.background = red;
}
}
this.clear = function () {
var i,j;
for (j=0;j<this.row;j++)
for (i=0;i<this.col;i++)
if (i==0 || j==0 || i==this.col-1 || j==this.row-1)
this.obj.rows[j].cells[i].style.background = black;
else
this.obj.rows[j].cells[i].style.background = white;
for (i=0;i<10;i++)
this.adddot();
}
}
function keypress() {
if (event.keycode==105 && snake.direction!=1 && snake.direction!=3)
snake.direction=1;
if (event.keycode==106 && snake.direction!=2 && snake.direction!=4)
snake.direction=2;
if (event.keycode==107 && snake.direction!=1 && snake.direction!=3)
snake.direction=3;
if (event.keycode==108 && snake.direction!=2 && snake.direction!=4)
snake.direction=4;
}
var count=0;
function run() {
var c = snake.move();
if(c==black)
alert(game over!);
else {
if(c==red) {
count++;
for (var i=0;i<5;i++)
snake.node[snake.node.length] = new pos(snake.node[snake.node.length-1].x, snake.node[snake.node.length-1].y);
board.adddot();
score.value = count;
}
window.settimeout(run(),30);
}
}
</script>
<body>
score:<input name=score type=text value=0 disabled onfocus=this.blur()><br>
<script>
var board = new board(gameboard,50,40);
board.draw();
board.clear();
var snake = new snake(1,1);
snake.setboard(board);
snake.draw();
document.body.onkeypress = keypress;
window.settimeout(run(),10);
</script>
<body>
</html>
