未完成的JS作品之一:用面向对象思想写的虫吃豆游戏
发表于:2007-06-30来源:作者:点击数:
标签:
无聊时写的,稍作修改就可以支持多人 游戏 的,实在是懒得写下去了 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,
无聊时写的,稍作修改就可以支持多人
游戏的,实在是懒得写下去了
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>
原文转自:http://www.ltesting.net