`
ic081ic
  • 浏览: 71496 次
最近访客 更多访客>>
社区版块
存档分类
最新评论

基于xhtml+Javascript的俄罗斯方块游戏源程序

 
阅读更多

基于xhtml+Javascript的俄罗斯方块游戏源程序
2011年01月17日
  
  [b]http://www.w3.org/1999/xhtml
">
  
  俄罗斯方块
  
  
  
  BODY {
  BACKGROUND: black; MARGIN: 0px
  }
  TD {
  COLOR: #ffffff; FONT-FAMILY: 宋体; FONT-SIZE: 9pt; HEIGHT: 20px; WIDTH: 20px
  }
  #block_div {
  POSITION: absolute; WIDTH: 80px; Z-INDEX: 1
  }
  #table_div {
  POSITION: absolute; WIDTH: 320px
  }
  #nblock_div {
  COLOR: red; FONT: 48px system; POSITION: absolute; Z-INDEX: 2
  }
  #title_div {
  COLOR: white; FONT-SIZE: 14px; LEFT: 326px; POSITION: absolute; TOP: 7px
  }
  #infobar_div {
  LEFT: 5px; POSITION: absolute; TOP: 460px
  }
  #infobar2_div {
  COLOR: white; FONT-SIZE: 14px; LEFT: 396px; POSITION: absolute; TOP: 480px
  }
  #infobar2_div A {
  COLOR: #99ccff; TEXT-DECORATION: none
  }
  
  
  [b]http://www.dynamicdrive.com

  */
  if (document.all){
  var gameInterval="";
  var gamestart=0;
  var n_width = 800;
  var n_height =640;
  var n_left = Math.round( screen.width/2 ) - n_width/2;
  var n_top = Math.round( screen.height/2 ) - n_height/2;
  var n_IncStep = 20;
  var curBlcok,nextBlock;
  var arr_curBlock = new Array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
  var curX,curY;
  var speed=1;
  var maxspeed=9;
  var clr_per_line=18;
  var pause=0;
  var gameover=0;
  var colors = new Array("#999999","#0000FF","#80FFFF","#80FF80","#FFFF00","#FF8000","#FF00FF","#FF0000"
  );
  var cid;
  var ncid;
  var blocks = new Array("tt_O1","tt_T2","tt_Z1","tt_S1","tt_L1","tt_J1","tt_I2");
  var bid;
  var killedlines=0;
  var tt_O1 = new Array(0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0);
  var tt_O2 = new Array(0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0);
  var tt_O3 = new Array(0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0);
  var tt_O4 = new Array(0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0);
  var tt_T1 = new Array(0,0,0,0,0,1,0,0,1,1,0,0,0,1,0,0);
  var tt_T2 = new Array(0,0,0,0,0,0,0,0,1,1,1,0,0,1,0,0);
  var tt_T3 = new Array(0,0,0,0,0,1,0,0,0,1,1,0,0,1,0,0);
  var tt_T4 = new Array(0,0,0,0,0,1,0,0,1,1,1,0,0,0,0,0);
  var tt_Z1 = new Array(0,0,0,0,1,1,0,0,0,1,1,0,0,0,0,0);
  var tt_Z2 = new Array(0,0,0,0,0,0,1,0,0,1,1,0,0,1,0,0);
  var tt_Z3 = new Array(0,0,0,0,0,1,1,0,0,0,1,1,0,0,0,0);
  var tt_Z4 = new Array(0,0,1,0,0,1,1,0,0,1,0,0,0,0,0,0);
  var tt_S1 = new Array(0,0,0,0,0,1,1,0,1,1,0,0,0,0,0,0);
  var tt_S2 = new Array(0,0,0,0,0,1,0,0,0,1,1,0,0,0,1,0);
  var tt_S3 = new Array(0,0,0,0,0,0,1,1,0,1,1,0,0,0,0,0);
  var tt_S4 = new Array(0,1,0,0,0,1,1,0,0,0,1,0,0,0,0,0);
  var tt_L1 = new Array(0,1,0,0,0,1,0,0,0,1,1,0,0,0,0,0);
  var tt_L2 = new Array(0,0,0,0,0,0,1,0,1,1,1,0,0,0,0,0);
  var tt_L3 = new Array(0,0,0,0,0,1,1,0,0,0,1,0,0,0,1,0);
  var tt_L4 = new Array(0,0,0,0,0,1,1,1,0,1,0,0,0,0,0,0);
  var tt_J1 = new Array(0,0,1,0,0,0,1,0,0,1,1,0,0,0,0,0);
  var tt_J2 = new Array(0,0,0,0,1,1,1,0,0,0,1,0,0,0,0,0);
  var tt_J3 = new Array(0,0,0,0,0,1,1,0,0,1,0,0,0,1,0,0);
  var tt_J4 = new Array(0,0,0,0,0,1,0,0,0,1,1,1,0,0,0,0);
  var tt_I1 = new Array(0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0);
  var tt_I2 = new Array(0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1);
  var tt_I3 = new Array(0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0);
  var tt_I4 = new Array(0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1);
  var table = new Array(
  1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
  1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
  1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
  1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
  1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
  1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
  1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
  1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
  1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
  1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
  1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
  1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
  1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
  1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
  1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
  1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
  1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
  1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
  1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
  1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
  1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)
  }
  else
  alert("You need IE4+ to play Tetris!")
  function dimension2(row,col,num){
  var i = row * num + col;
  return (this);
  }
  Array.prototype.getd = dimension2;
  function ShowBlock(x,y,block_type,color){
  for (var i=0;ifor (var j=0;jfor (var i=0;ifor (var j=0;j4||numfor (i=0;ifor (j=0;jfor (i=0;ifor (j=0;j1)&&(curX/n_IncStep+j14))
  table_tbl.rows(curY/n_IncStep+i).cells(curX/n_IncStep+j).style.background = colors[cid];
  if (table[(curY/n_IncStep+i)*16+curX/n_IncStep+j]!=1)
  table_tbl.rows(curY/n_IncStep+i).cells(curX/n_IncStep+j).style.background = "black";
  }
  }
  }
  function DelLine(line){
  for(i=line;i>0;i--){
  for(j=2;j14;j++){
  table[i*16+j]=table[(i-1)*16+j];
  }
  }
  table_tbl.deleteRow(line);
  table_tbl.insertRow(0);
  for (i=0;i13) table_tbl.rows(0).cells(i).style.background="navy";
  }
  killedlines++;
  cll.innerText=parseInt(cll.innerText)+1;
  }
  function DelLines(){
  var c,d,i,j;
  d=0;
  curY=block_div.style.pixelTop;
  for(i=(curY/20+3);i>curY/20-1;i--){
  c=0;
  for(j=2;j14;j++){
  if (isNaN(table[i*16+j])||i==21) break;
  c+=table[i*16+j];
  }
  if(c==12){
  DelLine(i);
  i++;
  d++;
  }
  }
  if (d>0)
  sco.innerText=parseInt(sco.innerText)+d*d*36;
  }
  function Lucifer(){
  for(var i=2;i14;i++){
  if (table[16+i]==1) return true;
  }
  return false;
  }
  function GameOver(){
  gameover=1;
  clearInterval(gameInterval);
  block_div.innerHTML="";
  for (i=0;ifor (j=2;j14;j++){
  setTimeout("table_tbl.rows(" + i + ").cells(" + j + ").style.background = colors[Math.round(Math.random()*7)];",16*i*j);
  }
  }
  nblock_div.innerHTML = "游戏结束
";
  }

  function document_onkeydown() {
  if (gameover==1) return;
  with (block_div.style){
  curX = pixelLeft;
  curY = pixelTop;
  switch (event.keyCode){
  case 37:
  if(pause==1||gamestart==0){break;}
  if (CanMove(curX-n_IncStep,curY,arr_curBlock))
  pixelLeft-=n_IncStep;
  break;
  case 38:
  if(pause==1||gamestart==0){break;}
  Change(1);
  break;
  case 39:
  if(pause==1||gamestart==0){break;}
  if (CanMove(curX+n_IncStep,curY,arr_curBlock))
  pixelLeft+=n_IncStep;
  break;
  case 40:
  if(pause==1||gamestart==0){break;}
  if (CanMove(curX,curY+n_IncStep,arr_curBlock)){
  pixelTop+=n_IncStep;
  }
  else{
  SaveBlock();
  DelLines();
  if (Lucifer()){
  GameOver();
  return;
  }
  sco.innerText=parseInt(sco.innerText)+2;
  NewBlock();
  }
  break;
  case 32:
  if (pause==0){
  if (gameInterval==""){break}
  clearInterval(gameInterval);
  pause=1;
  }
  else{
  gameInterval=window.setInterval("Handle_Interval()",(maxspeed-speed+1)*60);
  pause=0;
  }
  break;
  case 90:
  if(pause==1||gamestart==0){break;}
  Change(1);
  break;
  case 88:
  if(pause==1||gamestart==0){break;}
  Change(-1);
  break;
  default:
  }
  }
  }
  function Handle_Interval(){
  curX = block_div.style.pixelLeft;
  curY = block_div.style.pixelTop;
  if (CanMove(curX,curY+n_IncStep,arr_curBlock)){
  block_div.style.pixelTop+=n_IncStep;
  }
  else{
  SaveBlock();
  DelLines();
  if (Lucifer()){
  GameOver();
  return;
  }
  sco.innerText=parseInt(sco.innerText)+2
  NewBlock();
  }
  if (killedlines>=clr_per_line){
  killedlines-=clr_per_line;
  if (speedspeed=maxspeed;
  spd.innerText=speed;
  clearInterval(gameInterval);
  gameInterval=window.setInterval("Handle_Interval()",(maxspeed-speed+1)*60);
  }
  }
  //-->
  
  for=document language=javascript>
  
  
  
  
  
  if (document.all){
  for (var i=0;i");
  for (var j=0;j");
  }
  document.write("");
  }
  }
  
  
  
  
  
  
  
  if (document.all){
  for (var i=0;i");
  for (var j=0;j");
  }
  document.write("");
  }
  }
  
  
  
  
  
  
  
  if (document.all){
  for (var i=0;i");
  for (var j=0;j");
  else
  document.write("");
  }
  document.write("");
  }
  }
  
  
  
  
  

  请输入数字(1-9)

  

  起始速度:
  
  开始

  

  

  下一块:

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  说明:

  

   ↑ :改变形状 ← :向左移动

  → :向右移动 ↓ :向下移动

  

  空格键:暂停/继续

  

  ======= 
  
  
  
  
  速度:
  1
  总分:
  0
  成绩:
  0
  
  
  
  
  
  
  speed=parseInt(document.all.speedin.value);
  if (isNaN(speed)||speed==null||speed>maxspeed||speedspeed=1;
  spd.innerText=speed;
  gameInterval=window.setInterval("Handle_Interval()",(maxspeed-speed+1)*60);
  }
  //-->
  
  
  
  
  
  
;input type=button value='-' name='goppp' onClick='Javascript:window.close();' accesskey='q'>
  
  
  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics