无聊时写的,稍作修改就可以支持多人游戏的,实在是懒得写下去了
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('');
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
关键词:未完成的JS作品之一:用面向对象思想写的虫吃豆游戏