练手的时候写的,后面写得好乱,8(
比较满意的是方块的储存和旋转,是我见过同类游戏中最简洁的
/********************************
俄罗斯方块 Version 1.0beta
2001年10月19日 By 黄砾(Stone) (bbs.online.jx.cn)
这段脚本可以免费使用于任何非商业用途。引用时请保留本段说明。
Tetris version 1.0beta
by Li Huang, October 19th 2001
*******************************/
//存储各方块的形状
var Shapes = new Array (
new Array (2,1,0,0,1,0,2,0,1,1),
new Array (2,1,0,0,1,0,1,1,2,1),
new Array (2,1,1,0,2,0,0,1,1,1),
new Array (1,1,0,0,1,0,0,1,1,1),
new Array (2,1,0,0,1,0,2,0,0,1),
new Array (2,1,0,0,1,0,2,0,2,1),
new Array (3,2,0,1,1,1,2,1,3,1));
function tetris(name,width,height) {
this.width = width;
this.height = height;
document.write('
|   | ');
this.Box = eval(name);
this.checkDot = function(x,y) {
if (x<0 || y<0 || x>=this.width || y>=this.height)
return false;
else if (this.Box.rows(y).cells(x).borderColorDark == '#ffffff')
return false;
else
return true;
}
this.dot = function(x,y) {
if (this.checkDot(x,y))
with (this.Box.rows(y).cells(x)) {
borderColorDark = '#FFFFFF';
borderColorLight = '#808080';
}
}
this.clearDot = function(x,y) {
with (this.Box.rows(y).cells(x)) {
borderColorDark = '#C0C0C0';
borderColorLight = '#C0C0C0';
}
}
this.eraseLine = function() {
var line = 0, ny=this.height-1;
for (var y=this.height-1;y>=0;y--) {
var erase=true;
for (var x=0;x if (this.checkDot(x,y)) { this.clearDot(x,ny); erase=false; } else this.dot(x,ny); if (!erase) ny--; else line++; } return line; } this.clear=function() { for (var y=0;y for (var x=0;x this.clearDot(x,y); } } function pos(x,y) { this.x = x; this.y = y; return this; } function block(obj,Shape) { this.x = 0; this.y = 1; this.obj = obj; this.shape = new Array(); for (var i=0;i this.shape[i>>1] = new pos(Shape[i],Shape[i+1]); this.setObj = function(obj) { this.obj = obj; } this.turn = function() { var i,tmp; with (this) { for (i=1;i tmp = shape[i].y; shape[i].y = shape[i].x; shape[i].x = shape[0].y - tmp; } tmp=shape[0].x; shape[0].x=shape[0].y; shape[0].y=tmp; if (!check()) { for (i=1;i tmp = shape[i].x; shape[i].x=shape[i].y; shape[i].y=shape[0].x-tmp; } tmp=shape[0].x; shape[0].x=shape[0].y; shape[0].y=tmp; } } } this.check = function() { with (this) for (var i=1;i if (!obj.checkDot(shape[i].x+x,shape[i].y+y)) return false; return true; } this.left = function() { with (this) { x--; if (!check()) x++; } } this.right = function() { with (this) { x++; if (!check()) x--; } } this.down = function() { with (this) { y++; if (!check()) { y--; return false; } } return true; } this.clear = function() { with (this) for (var i=1;i obj.clearDot(shape[i].x+x,shape[i].y+y); } this.draw = function() { with (this) for (var i=1;i obj.dot(shape[i].x+x,shape[i].y+y); } } var Hiscore=0, score=0, line=0, level=1, speed=1500, handle=NaN; function keyPlay() { Block.clear(); switch (window.event.keyCode) { case 73: case 105:Block.turn(); break; case 74: case 106:Block.left(); break; case 75: case 107:if (!isNaN(handle)) window.clearTimeout(handle); down(); break; case 76: case 108:Block.right(); } Block.draw(); } function keyWait() { if (window.event.keyCode==115 || window.event.keyCode==83) gameStart(); } function gameStart() { Message.style.visibility = "hidden"; score=0; line=0; level=1; speed=1500; Tetris.clear(); Preview.clear(); show(); Block = new block(Tetris,Shapes[Math.round(Math.random()*6)]); NextBlock = new block(Preview,Shapes[Math.round(Math.random()*6)]); Block.draw(); NextBlock.draw(); handle = window.setTimeout('down()',speed); document.body.onkeypress = keyPlay; } function gameOver() { handle = NaN; document.body.onkeypress = keyWait; Message.innerHTML = '俄罗斯方块 v1.0beta Message.style.visibility = 'visible'; } function show() { Show.innerHTML = ' Hiscore Score Level Line } function down() { Block.clear(); if (!Block.down()) { Block.draw(); var eraseLine = Tetris.eraseLine(); line += eraseLine; score+= 50*((1< if (score>Hiscore) Hiscore=score; if ((score>>9)>level) { level++; speed=Math.round(speed/1.3); if (speed<80) speed=80; } NextBlock.clear(); Block = NextBlock; Block.setObj(Tetris); show(); if (!Block.check()) { gameOver(); return; } NextBlock = new block(Preview,Shapes[Math.round(Math.random()*6)]); NextBlock.draw(); } Block.draw(); handle = window.setTimeout("down()",speed); } gameOver();
游戏结束!
按 S 键重新开始,
J,K,L键控制方块移动,
按 I 键控制方块旋转
2001-10-19 by 黄砾(stone)
http://bbs.online.jx.cn';
' + Hiscore + '
' + score + '
' + level + '
' + line + '
NEXT

