CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
山寨机中的战斗机! 程序优化工程师到底对IT界有没有贡献
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  Java >  J2SE / 基础类

求助:谁能告诉我俄罗斯方块的算法和编制思路?谢谢

楼主jupiter_lwm(随风而逝)2002-03-22 09:27:38 在 Java / J2SE / 基础类 提问

如题!!!!! 问题点数:100、回复次数:7Top

1 楼dire_999(秋天的柳树)回复于 2002-03-22 09:48:45 得分 0

可以到网上找个程序看看,应该有很多。能学到不少Top

2 楼xioyoo(逍遥)回复于 2002-03-22 09:58:39 得分 60

import   java.awt.*;  
  import   java.awt.event.*;  
  import   java.applet.*;  
   
  /*  
          TRTRIS   GAME  
          WRITE   BY   DELFAN  
          EMAIL   :   webmaster@delfan.com  
          URL   :   http://www.delfan.com  
   
          经典"俄罗斯方块"游戏  
          作者   :   DELFAN  
          EMail   :   webmaster@delfan.com  
          主页   :   http://www.delfan.com  
   
          版本信息:  
          2002.01.13   基本完成  
   
          请任何时候都保留以上信息.谢谢!  
  */  
   
   
  public   class   tetris   extends   Applet   implements   Runnable  
  {  
          Thread   thread;  
          private   Image   offImg;   //   缓冲图象  
          private   Graphics   offG;   //   缓冲  
   
   
          final   int   BaseX   =   20;  
          final   int   BaseY   =   20;  
          final   int   BlockSize   =   20;     //   每个块的大小  
   
          //  
          //     定义游戏中出现的信息  
          //  
          final   String   INFO_READY   =   "S键   开始游戏";  
          final   String   INFO_PAUSE   =   "P键   继续游戏";  
          final   String   INFO_GAMEOVER   =   "游戏结束   任意键继续";  
   
          byte   SHAPE[][][][]   =   //   [造型索引][旋转索引][4][坐标]  
          {  
                  {     //   造型一  
                      {{   0,   0},{   1,   0},{   0,   1},{   1,   1}},   //   旋转一     [][]....  
                      {{   0,   0},{   1,   0},{   0,   1},{   1,   1}},   //   旋转二     [][]....  
                      {{   0,   0},{   1,   0},{   0,   1},{   1,   1}},   //   旋转三     ........  
                      {{   0,   0},{   1,   0},{   0,   1},{   1,   1}}     //   旋转四     ........  
                  },  
                  {     //   造型二  
                      {{-1,   0},{   0,   0},{   0,   1},{   1,   1}},   //   旋转一                             []  
                      {{   0,-1},{   0,   0},{-1,   0},{-1,   1}},   //   旋转二     []()             []()  
                      {{-1,   0},{   0,   0},{   0,   1},{   1,   1}},   //   旋转三         [][]         []  
                      {{   0,-1},{   0,   0},{-1,   0},{-1,   1}}     //   旋转四  
                  },  
                  {     //   造型三  
                      {{   1,   0},{   0,   0},{   0,   1},{-1,   1}},   //   旋转一                         []  
                      {{   0,-1},{   0,   0},{   1,   0},{   1,   1}},   //   旋转二         ()[]         ()[]  
                      {{   1,   0},{   0,   0},{   0,   1},{-1,   1}},   //   旋转三     [][]                 []  
                      {{   0,-1},{   0,   0},{   1,   0},{   1,   1}}     //   旋转四  
                  },  
                  {     //   造型四  
                      {{   0,   0},{   1,   0},{   2,   0},{   0,   1}},   //   旋转一                                               []         []  
                      {{   0,-2},{   0,-1},{   0,   0},{   1,   0}},   //   旋转二     ()[][]         []()       [][]()         []  
                      {{-2,   0},{-1,   0},{   0,   0},{   0,-1}},   //   旋转三     []                     []                           ()[]  
                      {{-1,   0},{   0,   0},{   0,   1},{   0,   2}}     //   旋转四                             []  
                  },  
                  {     //   造型五  
                      {{-2,   0},{-1,   0},{   0,   0},{   0,   1}},   //   旋转一                         []  
                      {{   0,   0},{   1,   0},{   0,   1},{   0,   2}},   //   旋转二     [][]()         []       []               ()[]  
                      {{   0,-1},{   0,   0},{   1,   0},{   2,   0}},   //   旋转三             []     []()       ()[][]       []  
                      {{   0,-2},{   0,-1},{   0,   0},{-1,   0}}     //   旋转四                                                     []  
                  },  
                  {     //   造型六  
                      {{-1,   0},{   0,   0},{   1,   0},{   0,   1}},   //   旋转一  
                      {{   0,-1},{   0,   0},{   0,   1},{   1,   0}},   //   旋转二         []           []                                 []  
                      {{-1,   0},{   0,   0},{   1,   0},{   0,-1}},   //   旋转三     []()[]       ()[]         []()[]     []()  
                      {{-1,   0},{   0,   0},{   0,-1},{   0,   1}}     //   旋转四                       []                 []             []  
                  },  
                  {     //   造型六  
                      {{   0,-1},{   0,   0},{   0,   1},{   0,   2}},   //   旋转一     []  
                      {{-1,   0},{   0,   0},{   1,   0},{   2,   0}},   //   旋转二     ()       []()[][]  
                      {{   0,-1},{   0,   0},{   0,   1},{   0,   2}},   //   旋转三     []  
                      {{-1,   0},{   0,   0},{   1,   0},{   2,   0}}     //   旋转四     []  
                  }  
          };  
   
          //  
          //     定义游戏中使用的变量  
          //  
          final   int[]   Speeds   =   {60,50,40,30,20,15,10,5,3,1};     //   速度值定义  
   
          byte[][]   Ground   =   new   byte[10][20];     //   在   10   X   20   的场地游戏,预留顶部放置方块的空间一格  
          byte[][]   NextShape   =   new   byte[4][2];   //   存放下块造型  
          int   CurrentX;                                       //   当前X左边  
          int   CurrentY;                                       //   当前Y坐标  
          int   CurrentShapeIndex;                     //   当前造型索引  
          int   CurrentTurnIndex;                       //   当前旋转索引  
          int   CurrentColorIndex;                     //   当前造型色彩索引  
          int   NextShapeIndex;                           //   下块出现的造型的索引  
          int   GameSpeed   =   0;                             //   游戏速度  
          int   SpeedVar   =   0;                               //   延时计数  
          int   FlashSpeed   =   60;                         //   信息提示闪烁速度  
          int   FlashVar   =   0;                               //   闪烁延时计数  
          int   ClearTotalLine   =   0;                   //   消掉的总行数  
   
   
          Color[]   Colors   =   {Color.black,Color.red,Color.green,Color.blue,Color.magenta,Color.yellow,Color.orange,Color.pink};  
   
          //  
          //       定义游戏中出现的状态  
          //  
          final   int   READY   =   0;  
          final   int   GAMING   =   1;  
          final   int   PAUSE   =   2;  
          final   int   GAMEOVER   =   3;  
          private   int   CurrentMode   =   READY   ;       //   游戏状态变量  
   
          String   InfoStr   =   new   String(INFO_READY);  
   
          public   void   init()  
          {  
                  offImg   =   createImage(getSize().width,   getSize().height);   //   创建缓冲图象大小  
                  offG   =   offImg.getGraphics();  
                  setBackground(Color.black);  
          }  
   
          public   void   MakeNextShape()  
          {  
                  NextShapeIndex   =   (int)(Math.random()*70)   /10;  
                  NextShape   =   SHAPE[NextShapeIndex][0];  
          }  
   
          public   void   ClearGround()     //   清除场地  
          {  
                  for(int   i=0;   i<10;   i++)  
                        for(int   j=0;   j<20;   j++)   Ground[i][j]=0;  
                  CurrentMode   =   READY;  
                  ClearTotalLine   =   0;  
          }  
   
          public   void   StartGame()     //   开始游戏  
          {  
                  ClearGround();  
                  CurrentShapeIndex   =   (int)(Math.random()*70)   /10;  
                  CurrentX   =   4;  
                  CurrentY   =   0;  
                  CurrentMode   =   GAMING;  
                  CurrentColorIndex   =   (int)(Math.random()*70)   /10   +1;  
                  MakeNextShape();  
          }  
   
          public   void   drawABlock(Graphics   g,int   x,int   y,int   colorIndex)     //   用指定的颜色画一个方块  
          {  
                  g.setColor(Colors[colorIndex]);  
                  g.fill3DRect(BaseX   +   x   *   BlockSize,   BaseY   +   y   *   BlockSize   ,   BlockSize,   BlockSize,   true);  
                  g.fill3DRect(BaseX   +   x   *   BlockSize   +   1,   BaseY   +   y   *   BlockSize   +1,   BlockSize   -2,   BlockSize-2,   true);  
          }  
   
          public   void   drawShape(Graphics   g,int   x,int   y,   int   shapeindex,int   trunindex)   //   在指定位置画指定的造型  
          {  
                  for(int   i=0;   i<4;   i++)  
                  {  
                          if(y+SHAPE[shapeindex][trunindex][i][1]>=0)       //   不画最顶上不在区域的块  
                                  drawABlock(g,x+SHAPE[shapeindex][trunindex][i][0]  
                                                          ,y+SHAPE[shapeindex][trunindex][i][1],CurrentColorIndex);  
                  }  
          }  
   
          public   void   drawGround(Graphics   g)     //   画整个画面  
          {  
                  //   画提示信息及行数及其他  
                  g.setColor(new   Color(255,255,255));  
                  if(CurrentMode   ==   READY)   g.drawString("   +   /   -   :   调整速度",BaseX,BaseY-3);  
                  if(CurrentMode   ==   GAMING)   g.drawString("Up:翻转   Left/Right:移动   Down:快落   R:重玩",BaseX,BaseY-3);  
                  g.drawRect(BaseX-1,BaseY-1,BlockSize*10,   BlockSize*20);  
                  for(int   i=0;   i<10;   i++)  
                      for(int   j=1;   j<20;   j++)  
                              if(Ground[i][j]!=0)   drawABlock(g,i,j,Ground[i][j]);  
                  //   显示相关信息  
                  g.setColor(Color.lightGray);  
                  g.drawRect(BaseX   +   BlockSize   *   10   ,   BaseY-1,BlockSize   *   2   +   BlockSize   /2   +   BlockSize/2,20);     //   与下一块提示对齐  
                  g.drawString("TETRIS",BaseX   +   BlockSize   *   10+8,BaseY   +   14);   //   画游戏相关信息  
                  g.drawString("已消行数",BaseX   +   BlockSize   *   10+5,BaseY   *   3   );  
                  g.drawString(String.valueOf(ClearTotalLine),BaseX   +   BlockSize   *   10+5,BaseY   *   4);  
                  g.drawString("当前速度",BaseX   +   BlockSize   *   10+5,BaseY   *   6   );  
                  g.drawString(String.valueOf(GameSpeed),BaseX   +   BlockSize   *   10+5,BaseY   *   7);  
                  g.drawString("http://www.delfan.com"   ,BaseX,BaseY   +   BlockSize   *   21-10   );  
          }  
   
          public   void   drawNextBlock(Graphics   g)     //     画下一块  
          {  
                  g.setColor(Color.lightGray);  
                  g.drawString("下一块",BaseX+11*BlockSize   -   BlockSize   /   2,BaseY+17*BlockSize);  
                  g.drawRect(BaseX   +   BlockSize   *   10,   BaseY   +   BlockSize   *18   -   BlockSize   /   2  
                                                                ,BlockSize   *   2   +   BlockSize   /   2,BlockSize   *   2);  
                  g.setColor(Color.blue);  
                  for(int   i=0;   i<4;   i++)  
                          g.fill3DRect(BaseX   +   11   *   BlockSize   +   NextShape[i][0]*BlockSize   /   2  
                                                ,   BaseY   +   18   *   BlockSize   +   NextShape[i][1]*BlockSize   /   2   ,   BlockSize   /   2   ,   BlockSize   /2,   true);  
          }Top

3 楼lianyunzxp(编程浪子)回复于 2002-03-22 09:59:28 得分 0

http://www.delfan.com/java/tetris.html     java   实现的  
  Top

4 楼xioyoo(逍遥)回复于 2002-03-22 09:59:28 得分 0

接上:  
    public   void   drawInfo(Graphics   g)     //   绘制提示的信息  
          {  
                  g.setColor(Color.white);  
                  Font   old   =   g.getFont();  
                  g.setFont(new   Font("宋体",0,24));  
                  g.drawString(InfoStr,BaseX+5*BlockSize   -   InfoStr.length()*   7   ,BaseY+10*BlockSize);  
                  g.setFont(old);  
          }  
   
          public   void   paint(Graphics   g)  
          {  
                  offG.clearRect(0,0,getSize().width,getSize().height);  
                  drawGround(offG);  
                  switch(CurrentMode)  
                  {  
                          case   READY   :  
                          case   PAUSE   :  
                          case   GAMEOVER   :  
                                        drawInfo(offG);  
                                        break;  
                          case   GAMING   :  
                                        drawShape(offG,CurrentX,CurrentY,CurrentShapeIndex,CurrentTurnIndex);  
                                        drawNextBlock(offG);  
                                        break;  
                  }  
                  if(offG!=null)   g.drawImage(offImg,0,0,this);  
          }  
   
          public   void   update(Graphics   g)  
          {  
                  paint(g);  
          }  
   
          public   void   start()  
          {  
                  thread   =   new   Thread(this);  
                  thread.start();  
          }  
   
          public   void   stop()  
          {  
                  thread   =   null;  
          }  
   
          public   void   run()  
          {  
                  Thread   current   =   Thread.currentThread();  
   
                  while(thread   ==   current)  
                  {  
                          try  
                          {  
                                Thread.currentThread().sleep(10);  
                          }   catch   (InterruptedException   e)   {}  
   
                          if(CurrentMode   ==   GAMING)  
                          {  
                                  if(SpeedVar   ++   >   Speeds[GameSpeed])  
                                  {  
                                          SpeedVar   =   0;  
                                          DownIt();  
                                          repaint();  
                                  }  
                          }  
                          else  
                          if(FlashVar   ++   >   FlashSpeed)  
                          {  
                                  FlashVar   =   0;  
                                  switch(CurrentMode)  
                                  {  
                                          case   READY     :  
                                                          if(InfoStr.equals(""))   InfoStr   =   INFO_READY;  
                                                          else   InfoStr   =   "";  
                                                          break;  
                                          case   PAUSE     :  
                                                          if(InfoStr.equals(""))   InfoStr   =   INFO_PAUSE;  
                                                          else   InfoStr   =   "";  
                                                          break;  
                                          case   GAMEOVER   :  
                                                          if(InfoStr.equals(""))   InfoStr   =   INFO_GAMEOVER;  
                                                          else   InfoStr   =   "";  
                                  }  
                                  repaint();  
                          }   //   End   of   if(FlashVar++...  
                  }//   End   of   While  
          }  
   
          public   boolean   CanMoveTo(int   shape,int   turn,int   x,int   y)     //判断是否能移动到指定位置  
          {  
                  boolean   result   =   true;  
   
                  turn   %=4;  
   
                  for(int   i=0;   i<4;   i++)  
                  {  
                          if(   (x   +   SHAPE[shape][turn][i][0]   <   0)   ||   (x   +   SHAPE[shape][turn][i][0]   >   9)   )  
                          {  
                                  result   =   false;  
                                  break;  
                          }  
                          if(   y   +   SHAPE[shape][turn][i][1]   >   19)  
                          {  
                                  result   =   false;  
                                  break;  
                          }  
                          if(Ground[x+SHAPE[shape][turn][i][0]][y   +   SHAPE[shape][turn][i][1]]   !=   0)  
                          {  
                                  result   =   false;  
                                  break;  
                          }  
                  }  
                  return   result;  
          }  
   
          public   synchronized   void   DownIt()     //   处理下落过程  
          {  
                  if(CanMoveTo(CurrentShapeIndex,CurrentTurnIndex,CurrentX,CurrentY+1))  
                  {  
                          CurrentY++;  
                  }  
                  else     //   已经下落到底部了  
                  {  
                          if(CurrentY   ==   0)   CurrentMode   =   GAMEOVER;  
                          else  
                          {  
                                  //   将当前块放到Ground中去  
                                  for(int   i=0;   i<4;   i++)  
                                          Ground[CurrentX   +   SHAPE[CurrentShapeIndex][CurrentTurnIndex][i][0]]  
                                                      [CurrentY   +   SHAPE[CurrentShapeIndex][CurrentTurnIndex][i][1]]   =   (byte)CurrentColorIndex;  
                                  CurrentX=4;  
                                  CurrentY=0;  
                                  CurrentShapeIndex   =   NextShapeIndex;  
                                  CurrentColorIndex   =   (int)(Math.random()*70)   /10   +   1;  
                                  MakeNextShape();  
                                  CurrentTurnIndex   =0;  
                                  //   判断有没有一行的数据  
                                  int   total;  
                                  for(int   y=19;   y>=0;   y--)  
                                  {  
                                          total   =   0;  
                                          for(int   x=0;   x<10;   x++)  
                                                  if(Ground[x][y]   !=   0)   total   ++;  
   
                                          if(total   ==   10)   //   说明行满了  
                                          {  
                                                  //   将此行上面所有数据复制下来  
                                                  for(int   i=0;   i<10;   i++)  
                                                      for(int   j=y;   j>0;   j--)         Ground[i][j]   =   Ground[i][j-1];  
                                                  y++;  
                                                  ClearTotalLine++;  
                                                  if(ClearTotalLine   %   50   ==   0)  
                                                  {  
                                                          GameSpeed   ++;       //   每消50行增加一次速度  
                                                          if(GameSpeed>9)   GameSpeed   =   9;  
                                                  }  
                                          }  
                                  }//end   of   For  
                          }   //   of   else  
                  }   //   of   else  
          }  
   
          public   boolean   keyDown(Event   e,   int   key)   //   处理键盘事件  
          {  
                  switch(CurrentMode)  
                  {  
                          case   READY   :  
                                          if(key   ==   's'   ||   key   =='S')   StartGame();  
                                          if(key   ==   '+')  
                                          {  
                                                    GameSpeed   ++;  
                                                    if(GameSpeed>9)   GameSpeed   =   9;  
                                          }  
                                          if(key   ==   '-')  
                                          {  
                                                    GameSpeed   --;  
                                                    if(GameSpeed<0)   GameSpeed   =   0;  
                                          }  
                                          break;  
                          case   PAUSE   :  
                                          if(key   ==   'p'   ||   key   =='P')   CurrentMode   =   GAMING;  
                                          break;  
                          case   GAMEOVER   :  
                                          CurrentMode   =   READY;       //   按任意键准备游戏  
                                          break;  
                          case   GAMING   :  
                                          if(key   ==   'p'   ||   key   ==   'P')   CurrentMode   =   PAUSE;  
                                          if(key   ==   'r'   ||   key   ==   'R')   CurrentMode   =   READY;  
                                          if(key   ==   Event.LEFT   &&   CanMoveTo(CurrentShapeIndex,CurrentTurnIndex,CurrentX-1,CurrentY))   CurrentX   --;  
                                          if(key   ==   Event.RIGHT   &&   CanMoveTo(CurrentShapeIndex,CurrentTurnIndex,CurrentX+1,CurrentY))   CurrentX   ++;  
                                          if(key   ==   Event.UP   &&   CurrentY>1   &&   CanMoveTo(CurrentShapeIndex,CurrentTurnIndex+1,CurrentX,CurrentY))  
                                          {  
                                                    CurrentTurnIndex   ++;  
                                                    CurrentTurnIndex   %=   4;  
                                          }  
                                          if(key   ==   Event.DOWN)   DownIt();  
                                          repaint();  
                                          break;  
                  }  
                  return   true;  
          }  
  }  
  Top

5 楼tbhgshp(田)回复于 2002-03-22 09:59:41 得分 0

去找源代码看,网上有很多。Top

6 楼lianyunzxp(编程浪子)回复于 2002-03-22 10:01:51 得分 20

http://helj2000.3322.net/application/game1.HTML   也是Java实现Top

7 楼xioyoo(逍遥)回复于 2002-03-22 10:03:24 得分 20

http://oop.51.net  
  也可以找到一个APPLICATIONTop

相关问题

  • 求俄罗斯方块游戏算法
  • 请给一个俄罗斯方块的算法
  • 哪里有俄罗斯方块的算法和例子程序?
  • 百分求教,俄罗斯方块算法。
  • 俄罗斯方块如何实现联网?求点思路啊
  • 求助:哪里有俄罗斯方块的算法和例子程序?
  • 哪位大侠指点俄罗斯方块游戏的算法?高分相送。
  • 俄罗斯方块
  • 要在WINDOWS环境中做一个俄罗斯方块的游戏,该怎么作呀,给个思路。多谢。
  • 俄罗斯方块问题

关键词

  • .net
  • 游戏
  • 信息
  • delfan
  • basey
  • basex
  • 旋转
  • shape
  • final
  • import java

得分解答快速导航

  • 帖主:jupiter_lwm
  • xioyoo
  • lianyunzxp
  • xioyoo

相关链接

  • CSDN Java频道
  • Java类图书
  • Java类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo