俄罗斯方块javascript版本

本文介绍了一个使用JavaScript编写的俄罗斯方块游戏源代码。该游戏可在浏览器中运行,支持键盘控制方块移动与旋转,实现了游戏得分及方块消除等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这个俄罗斯方块游戏是用javascript脚本实现的,只需要将代码拷贝到文本文件中,然后将文件更名为eluosi.html. 用IE或者FireFox打开即可。

<HTML>

<META content="text/html; charset=gb2312" http-equiv=Content-Type>

<HEAD>


<title>¶íÂÞ˹·½¿é</title>
<style>
<!--
BODY
{
}
.MB
{
    BACKGROUND-COLOR: firebrick;
    CURSOR: default;
    HEIGHT: 22px;
    WIDTH: 22px
}
.SB
{
    BACKGROUND-COLOR: slategray;
    CURSOR: default;
    HEIGHT: 22px;
    WIDTH: 22px
}
.BK
{
    BACKGROUND-COLOR: white;
    CURSOR: default;
    HEIGHT: 22px;
    WIDTH: 22px
}
.GT
{
    BORDER-BOTTOM: deepskyblue thin solid;
    BORDER-LEFT: deepskyblue thin solid;
    BORDER-RIGHT: deepskyblue thin solid;
    BORDER-TOP: deepskyblue thin solid;
    CURSOR: default
}
-->
</style>

<script language="javascript">
<!--

//const KEY_LEFT  = 37;
//const KEY_UP    = 38;
//const KEY_RIGHT = 39;
//const KEY_DOWN  = 40;
//
//const BOARD_ROWS = 16;
//const BOARD_COLS = 10;
//const SMALL_BOARD_ROWS = 4;
//const SMALL_BOARD_COLS = 4;
//
//const BK_BRICK = "BK";
//const MB_BRICK = "MB";
//const SB_BRICK = "SB";

 

var KEY_LEFT  = 37;
var KEY_UP    = 38;
var KEY_RIGHT = 39;
var KEY_DOWN  = 40;

var BOARD_ROWS = 16;
var BOARD_COLS = 10;
var SMALL_BOARD_ROWS = 4;
var SMALL_BOARD_COLS = 4;

var BK_BRICK = "BK";
var MB_BRICK = "MB";
var SB_BRICK = "SB";


//Define the Constructor of Brick object.
function Brick(x, y, bBrick)
{
 // Attributes
 this.x = x;
 this.y = y;
 this.bBrick = bBrick;
}

Brick.Copy = function (x, y, b)
{
 var b = new Brick(x, y, b.bBrick);
 return b;
}

function Metrix_Heper ()
{
}

Metrix_Heper.CreateMetrix = function (x)
{
 var arr = new Array(x);
 for (var i = 0; i < x; i++)
 {
  arr[i] = new Array(x);
 }
 return arr;
}

function Metrix(x)
{  
 this.InitializeMetrixWith = function (value)
 {
  for (var i = 0; i < this.dimensionNum; i++)
  {
   for (var j = 0; j < this.dimensionNum; j++)
   {
    this.metrixArr[i][j] = Brick.Copy(i, j, value);
   }
  }
 }
 
 //Constructor
 this.dimensionNum = x;
 this.metrixArr = Metrix_Heper.CreateMetrix(x); 
}

//Define the Constructor of Bar object.
function Bar(board)
{
 // Attributes
 this.board = board;
 this.Rows = null;
 this.metrix = null;
 
 this.RandomShapeCreate = function ()
 {
  this.metrix = new Metrix(4);
  
  //Initialize Metrix

  
  //Generate random number, according to this number to generate a shape.
  var randNum = Math.floor(Math.random()*20) % 7;
  switch (randNum)
  {
   case 0:
    this.metrix = new Metrix(4);
    this.metrix.InitializeMetrixWith(new Brick(0, 0, false));
    
    for (var i = 0;i < 4; i++)
    {
     this.metrix.metrixArr[i][2].bBrick = true;
    } 
    break;
   case 1:
    this.metrix = new Metrix(3);
    this.metrix.InitializeMetrixWith(new Brick(0, 0, false));
    
    for (var i = 0;i < 3; i++)
    {                                                //  ***
     this.metrix.metrixArr[0][i].bBrick = true;   //  0*0
    }                                                //  000
    this.metrix.metrixArr[1][1].bBrick = true;      
    break;
   case 2:
    this.metrix = new Metrix(2);
    this.metrix.InitializeMetrixWith(new Brick(0, 0, false));
    
       this.metrix.metrixArr[0][0].bBrick = true;  //  **
       this.metrix.metrixArr[0][1].bBrick = true;  //  **
       this.metrix.metrixArr[1][0].bBrick = true; 
       this.metrix.metrixArr[1][1].bBrick = true; 
       break;
   case 3:
    this.metrix = new Metrix(3);
    this.metrix.InitializeMetrixWith(new Brick(0, 0, false));

    this.metrix.metrixArr[2][1].bBrick = true;  //  O0*
       this.metrix.metrixArr[0][2].bBrick = true;  //  0**
       this.metrix.metrixArr[1][1].bBrick = true;  //  0*0
       this.metrix.metrixArr[1][2].bBrick = true; 
       break;
   case 4:
    this.metrix = new Metrix(3);
    this.metrix.InitializeMetrixWith(new Brick(0, 0, false));
    
    for (var i = 0;i < 3; i++)
    {
     this.metrix.metrixArr[0][i].bBrick = true;  //***
    }                                             //*
    this.metrix.metrixArr[1][0].bBrick = true;
    break;
   case 5:
    this.metrix = new Metrix(3);
    this.metrix.InitializeMetrixWith(new Brick(0, 0, false));
    
    for (var i = 0;i < 3; i++)
    {
     this.metrix.metrixArr[0][i].bBrick = true;  //***
    }                                             //00*
    this.metrix.metrixArr[1][2].bBrick = true;
    break;
   default:
    this.metrix = new Metrix(3);
    this.metrix.InitializeMetrixWith(new Brick(0, 0, false));

    this.metrix.metrixArr[0][1].bBrick = true;  //  O**
       this.metrix.metrixArr[0][2].bBrick = true;  //  **0
       this.metrix.metrixArr[1][0].bBrick = true;  //  000
       this.metrix.metrixArr[1][1].bBrick = true; 
       break;      
  }
  
 }
 
 this.CheckCanDraw = function ()
 {
  for (var i = 0; i < this.metrix.dimensionNum; i++)
  {
   for (var j = 0; j < this.metrix.dimensionNum; j++)
   {
    b = this.metrix.metrixArr[i][j];
    if (b.bBrick)
    {
     if (this.board.IsBrick(b.x, b.y))
     {
      this.board.bRunning = false;
     }
    }
   } 
  }
       
 }

 //Draw shape on board.
 this.Draw = function(brickType)
 {
  for (var i = 0; i < this.metrix.dimensionNum; i++)
  {
   for (var j = 0; j < this.metrix.dimensionNum; j++)
   {
    b = this.metrix.metrixArr[i][j];
    if (b.bBrick)
    {
     this.board.setClass(b.x, b.y, brickType);
    }
   } 
  }
 }
 
 //Move shape to left
 this.MoveLeft = function ()
 {
  if (this._CanMoveLeft())
  {
   this.Draw(BK_BRICK);
   
   for (var i = 0; i < this.metrix.dimensionNum; i++)
   {
    for (var j = 0; j < this.metrix.dimensionNum; j++)
    {
     b = this.metrix.metrixArr[i][j];
     b.y = b.y - 1;
    }
  
   }
   
   this.Draw(MB_BRICK);
  }
 }
 
 //Move shape to right.
 this.MoveRight = function ()
 {
  if (this._CanMoveRight())
  {
   this.Draw(BK_BRICK);
   
   for (var i = 0; i < this.metrix.dimensionNum; i++)
   {
    for (var j = 0; j < this.metrix.dimensionNum; j++)
    {
     b = this.metrix.metrixArr[i][j];
     b.y = b.y + 1;
    }
  
   }
   
   this.Draw(MB_BRICK);
  }
 }
 
 //Move shape to down.
 this.MoveDown = function ()
 {
  if (this._CanMoveDown())
  {
   this.Draw(BK_BRICK);
   
   for (var i = 0; i < this.metrix.dimensionNum; i++)
   {
    for (var j = 0; j < this.metrix.dimensionNum; j++)
    {
     b = this.metrix.metrixArr[i][j];
     b.x = b.x + 1;
    }
  
   }
   
   this.Draw(MB_BRICK);
   
   return true;
  }
  else
  {
   //Delete all the line that complete a whole line.
   this.board.DeleteLine();
   
   return false;
  }
 }
 
 //Turn shape.
 this.Turn = function ()
 {
  var arr = Metrix_Heper.CreateMetrix(this.metrix.dimensionNum);
  var i;
  var j;
  
  //Generate the new shape.
  for (i = 0; i < this.metrix.dimensionNum; i++)
  {
   for (j = 0; j < this.metrix.dimensionNum; j++)
   {
    var col = this.metrix.dimensionNum - 1 - i;
    arr[j][col] = this.metrix.metrixArr[i][j].bBrick;
   }
  }
  
  //erasure the current shape on board for edge detection.
  this.Draw(BK_BRICK);
  
  for (i = 0; i < this.metrix.dimensionNum; i++)
  {
   for (j = 0; j < this.metrix.dimensionNum; j++)
   {
    b = this.metrix.metrixArr[i][j];
    if (arr[i][j])
    {
     if (this.board.IsEdge(b.x, b.y))
     {
      //if cannot turn, Draw back.
      this.Draw(MB_BRICK);
      return ;
     }
     else
     {
      if (this.board.IsBrick(b.x, b.y))
      {
       //if cannot turn, Draw back.
       this.Draw(MB_BRICK);
       return;
      }
     }
    }
   }
 
  }  
  
  //Turn shape, and draw it to board.
  for (i = 0; i < this.metrix.dimensionNum; i++)
  {
   for (j = 0; j < this.metrix.dimensionNum; j++)
   {
    this.metrix.metrixArr[i][j].bBrick = arr[i][j];
   }
  }
  this.Draw(MB_BRICK);
 }
 
 this._GetLeftBrick = function (row)
 {
  for (var i = 0; i < this.metrix.dimensionNum; i++)
  {
   var b = this.metrix.metrixArr[row][i];
   if (b.bBrick)
   {
    return b;
   }
  }
  
  return null;
 }
 
 this._GetRightBrick = function (row)
 {
  for (var i = this.metrix.dimensionNum - 1; i >= 0; i--)
  {
   var b = this.metrix.metrixArr[row][i];
   if (b.bBrick)
   {
    return b;
   }
  }
  
  return null;
 }
 
 this._GetBottomBrick = function (col)
 {
  for (var i = this.metrix.dimensionNum - 1; i >= 0; i--)
  {
   var b = this.metrix.metrixArr[i][col];
   if (b.bBrick)
   {
    return b;
   }
  }
  
  return null;
 }
 
 this._CanMoveLeft = function ()
 {
  
  for (var i = 0; i < this.metrix.dimensionNum; i++)
  {
   var b = this._GetLeftBrick(i);
   if (b != null)
   {
    if (this.board.IsLeftEdge(b.x, b.y - 1) || this.board.IsBrick(b.x, b.y - 1))
    {
     return false;
    }
   }
  }
  
  return true;
 }
 
 this._CanMoveRight = function ()
 {
  for (var i = 0; i < this.metrix.dimensionNum; i++)
  {
   var b = this._GetRightBrick(i);
   if (b != null)
   {
    if (this.board.IsRightEdge(b.x, b.y + 1) || this.board.IsBrick(b.x, b.y + 1))
    {
     return false;
    }
   }
  }
  
  return true;
 }
 
 this._CanMoveDown = function ()
 {
  for (var i = 0; i < this.metrix.dimensionNum; i++)
  {
   var b = this._GetBottomBrick(i);
   if (b != null)
   {
    if (this.board.IsBottomEdge(b.x + 1, b.y) || this.board.IsBrick(b.x + 1, b.y))
    {
     return false;
    }
   }
  }  
  
  return true;
 } 
 
}

//Define Board class.
function Board(lable, rows, cols)
{
 this.lableId = lable;
 this.nRows = rows;
 this.nCols = cols;
 this.score = 0;
 this.bRunning = false;
 
 this.getClass = function (x,y)
 {
  var GameBar = document.getElementById(this.lableId);
  return GameBar.rows[x].cells[y].className;
 }
 
 this.setClass = function (x,y,cName)
 {
  var GameBar = document.getElementById(this.lableId);
  GameBar.rows[x].cells[y].className=cName;
 }
 
 this.DeleteLine = function()
 {
  for (var i = this.nRows - 1; i >= 0; i--)
  {
   if (this._CanDelete(i))
   {
    this._SetRowValue(i, "SB");
    this._MoveDown(i);
    i++;
    
    this.score++;
    var scoreBar = document.getElementById("scoreBar");
    scoreBar.firstChild.data = "µÃ·Ö£º" + this.score;
    
   }
  }
 }
 
 this.IsLeftEdge = function (x, y)
 {
  if (0 > y)
  {
   return true;
  }
  
  return false;
 }

 this.IsRightEdge = function (x, y)
 {
  if (BOARD_COLS <= y)
  {
   return true;
  }
  
  return false;
 }
 
 this.IsBottomEdge = function (x, y)
 {
  if (BOARD_ROWS <= x)
  {
   return true;
  }

  return false;
 }
 
 this.IsEdge = function (x, y)
 {
  if (this.IsLeftEdge(x, y) || this.IsRightEdge(x, y) || this.IsBottomEdge(x, y))
  {
   return true;
  }
  
  return false;
 }
 
 this.IsBrick = function (x, y)
 {
  if (this.getClass(x, y) == MB_BRICK)
  {
   return true;
  }
  else
  {
   return false;
  }
 }
 
 this._CanDelete = function (row)
 {
  for (var i = 0; i < this.nCols; i++)
  {
   if (this.getClass(row, i) == BK_BRICK)
   {
    return false;
   }
  }
  return true;
 }
 
 this._MoveDown = function (row)
 {
  for (var i = row; i > 0; i--)
  {
   //this._SetRowValue
   for (var j = 0; j < this.nCols; j++)
   {
    //get the upper row value
    var value = this.getClass(i - 1, j);
    this.setClass(i, j, value);
    this.setClass(i - 1, j, BK_BRICK);
   }
  }
 }
 
 this._SetRowValue = function(row, value)
 {
  for (var i = 0; i < this.nCols; i++)
  {
   this.setClass(row, i, value);
  }
 }
 
 //»­Íø¸ñ
 this.DrawGridding = function ()
 {
  document.write("<Tbody id=");
  document.write(this.lableId);
  document.write(">");
  for (var i = 0; i < this.nRows; i++)
  {
   document.write("<tr>");
   
   var j;
   for (j = 0; j < this.nCols; j++)
   {
    document.write("<td class=BK>&#127</td>"); //http://www.webshu.com/tutorial/act/7,id=0205.htm ÍøÒ³°ë½Ç¿Õ¸ñµÄ·¢ÏÖ
   }
   
   document.write("</tr>");
  }
  document.write("</tbody>");
 }
 
 this.InitialzeBoard = function ()
 {
  for (var i = 0; i < this.nRows; i++)
  {
   this._SetRowValue(i, BK_BRICK);
  }
   
 }
}

var mainBoard;
var previewBoard;
var previewBar;
var movingBar;


function Initialize()
{
 mainBoard = new Board("mainBoard", 16, 10);
 previewBoard = new Board("previewBoard", 4, 4);
 
    previewBar = new Bar(previewBoard);
    movingBar  = new Bar(mainBoard);
}

var mTimer = null;

function NextBar()

 if (mainBoard.bRunning == false)
 {
  GameOver();
  return;
 }
 
 movingBar.metrix = previewBar.metrix;  //Copy shape bar from previewBar to movingBar.
 movingBar.CheckCanDraw();   //Check whether this shape can be Draw on the board.
 movingBar.Draw(MB_BRICK);           //Draw it anyway.
 
 //Draw the new shape bar on preview board.
 previewBar.Draw(BK_BRICK);
 previewBar.RandomShapeCreate();  
 previewBar.Draw(MB_BRICK);
 
}

function StartGame()
{
 mainBoard.InitialzeBoard();
 previewBoard.InitialzeBoard();
 movingBar.RandomShapeCreate();
 previewBar.RandomShapeCreate();
 previewBar.Draw(MB_BRICK);
 //movingBar.Draw(MB_BRICK);
 
 mainBoard.bRunning = true; 
 NextBar();
 
 var outTime=1100-1*100; //speed
   
    mTimer=window.setInterval("if (movingBar.MoveDown() == false) { NextBar(); }",outTime);
}

function GameOver()
{
 window.clearInterval(mTimer);
}

//
function KeyboardAction(key)
{
 if (mainBoard.bRunning == false)
 {
  return;
 }
 
 switch(key)
    {
        case KEY_LEFT:
        {  
            movingBar.MoveLeft();
           
            break;
         }
        case KEY_UP:
        {   
            movingBar.Turn();           
            break;
        }
        case KEY_RIGHT:
        {   
         movingBar.MoveRight();
            break;
        }
        case KEY_DOWN:
        {   
            if (movingBar.MoveDown() == false)
            {
             NextBar();
            }
            break;
        }
    }
}

//For firefox browser.
function keyControl(event)
{  
 KeyboardAction(event.which);   
}

//For Microsoft Internet Explorer.
function IEKeyControl()
{
 KeyboardAction(window.event.keyCode);
}

Initialize();
// -->
</script>

</HEAD>

<script language="javascript">
<!--
 if (navigator.appName == "Microsoft Internet Explorer")
 {
  document.write('<BODY bgcolor=Black onkeydown="return IEKeyControl();">');
 }
 else if (navigator.appName == "Netscape")
 {
  document.write('<BODY bgcolor=Black onkeydown="return keyControl(event);">');
 }
 else
 {
  document.write('<BODY>');
 }

// -->
</script>
<center>
<P><b><font color=red>¶íÂÞ˹·½¿é</font></b></P>

<table cellspacing=0 cellpadding=0 class=gt border=1 bordercolor="LightYellow " style="position:absolute;left:220px;top:53px;">


<script language="javascript">
<!--
mainBoard.DrawGridding();
// -->
</script>


</table>

</center>

<table style="position:absolute;top=100px;left:50px">
    <tr>
    <td id=scoreBar style="color:white">µÃ·Ö£º0</td></tr>
    <tr>
    <td id=speedBar style="color:white">ËÙ¶È£º1</td></tr>
</table>

<table cellspacing=0 cellpadding=0 class=gt border=1 bordercolor="LightYellow " style="position:absolute;left:570px;top:53px;">


<script language="javascript">
<!--

previewBoard.DrawGridding();

// -->
</script>


</table>

<table style="position:absolute;left:570px;top:180px;">
    <tr>
    <td><input type=button id="Play" style="width:100px" value="Play" onclick="return StartGame();"></td></tr>
    <tr><td><input type=button id="Quit" style="width:100px" value="Quit" onclick="window.close();"></td></tr>
</table>

</BODY>
</HTML>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值