⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tetrics.java~26~

📁 简单的俄罗斯方块游戏 源码 可 延伸成 网络版
💻 JAVA~26~
字号:
package tetrics;import java.math.*;import java.awt.*;import java.awt.event.*;/** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2003</p> * <p>Company: </p> * @author not attributable * @version 1.0 */public class Tetrics implements Runnable{  int m_nSqLength;//方块长  final int XOFFSET=200;  final int RIVAL_XOFFSET=500;  static int m_nCols;//游戏区域列  static int m_nRows;//游戏区域行  public static int WIDTH=800;  public static int HEIGHT=450;  int m_nField[][];  int m_nOldField[][];  Square m_curPiece[]=new Square[4];  Square m_prePiece[]=new Square[4];  boolean m_bGameInPlay;  boolean m_bNeedNewPiece;  boolean m_bPaused=false;  int m_nRivalField[][];  Thread m_theThread=null;  Color m_colors[];  int m_nPieceValue,m_nTheScore=0;  int m_nPlayLevel;  int m_nTotalPieces;  boolean m_bJustupdating=false;  int m_nDelayMap[]={600,600,600,600,500,400,300,250,200,150,100};  final Color BAKCOLOR=new Color(80,123,166);  final Color FORCOLOR=Color.BLACK;  public static int NOSQUARE=0;  Graphics m_gOffGraph;  TFrame m_tFrame;  public Tetrics() {    super();    m_tFrame.setBackground(BAKCOLOR);    initParam();  }  public Tetrics(TFrame tFrame) {    super();    m_tFrame=tFrame; m_tFrame.setBackground(BAKCOLOR);    initParam();  }  public void initParam(){     m_nSqLength=20;     m_nCols=10;     m_nRows=20;     m_nField=new int [m_nCols][m_nRows+4];     m_nOldField=new int [m_nCols][m_nRows+4];     m_nRivalField=new int [m_nCols][m_nRows+4];     m_nPlayLevel=5;     m_colors=new Color[8];     m_colors[0]=new Color(40,40,40);     m_colors[1]=new Color(255,0,0);     m_colors[2]=new Color(0,200,0);     m_colors[3]=new Color(0,200,255);     m_colors[4]=new Color(255,255,0);     m_colors[5]=new Color(255,150,0);     m_colors[6]=new Color(210,0,240);     m_colors[7]=new Color(40,0,240);  }  public void sendStr(String str)     {     }  public synchronized void start(){    if(m_theThread!=null){      m_bPaused=false;      m_theThread.resume();      return;    }   m_tFrame.repaint();   for(int i=0;i<m_nCols;i++){      for(int j=0;j<m_nRows+4;j++){        m_nField[i][j]=0;        m_nOldField[i][j]=-1;        m_nRivalField[i][j]=0;      }    }    m_nTheScore=0;    m_nTotalPieces=0;    m_bNeedNewPiece=true;    m_bGameInPlay=true;    m_theThread=new Thread(this);    newPrePiece();    m_theThread.start();    m_tFrame.requestFocus(); }  public synchronized void paint(Graphics g){   g.setFont(new Font("宋体", 0, 18));   int gx = m_nSqLength;   int gy = m_nSqLength * m_nRows / 4;   g.clearRect(gx, gy - 25, XOFFSET - gx, 25);   g.drawString("Score:" + m_nTheScore, gx, gy);   gy += 30;   g.clearRect(gx, gy - 25, XOFFSET - gx, 25);   g.drawString("Level:" + m_nPlayLevel, gx, gy);   int middle = m_nCols / 2;   int top = m_nRows;   gy += 30;   g.setColor(Color.black);   //画预览区   g.fillRect(gx, gy, m_nSqLength * 4, m_nSqLength * 4);   if (m_bGameInPlay) {     for(int i=0;i<4;i++){       g.setColor(m_colors[m_prePiece[i].m_nColor]);       g.fill3DRect((m_prePiece[i].m_nColumn-middle+2)*m_nSqLength+gx,          gy-(m_prePiece[i].m_nRow-top)*m_nSqLength,m_nSqLength,m_nSqLength,true);           }   }   Image img1 = m_tFrame.createImage(m_nSqLength * 10, m_nSqLength * 20);   Image img2 = m_tFrame.createImage(m_nSqLength * 10, m_nSqLength * 20);   Graphics g1 = img1.getGraphics();   Graphics g2 = img2.getGraphics();//画 游戏区   for (int i = 0; i < m_nCols; i++) {     for (int j = 0; j < m_nRows; j++) {       g1.setColor(m_colors[m_nField[i][m_nRows - 1 - j]]);       g1.fill3DRect(m_nSqLength * i, m_nSqLength * j, m_nSqLength,                     m_nSqLength, true);       g2.setColor(m_colors[m_nRivalField[i][m_nRows-1-j]]);       g2.fill3DRect(m_nSqLength*i,m_nSqLength*j,m_nSqLength,m_nSqLength,true);     }  //   g.drawImage(img1, XOFFSET, m_nSqLength, this);   //  g.drawImage(img2, RIVAL_XOFFSET, m_nSqLength, this);     m_bJustupdating = false;   } }  public void run() {    while(m_bGameInPlay){           try           {                  int t;                  if(m_nPlayLevel>10) t=75;                  else t=m_nDelayMap[m_nPlayLevel];                  Thread.sleep(t);           }catch(InterruptedException e){e.printStackTrace();}           if(m_bNeedNewPiece)           {                   if(m_nPieceValue>0)               {                   m_nTheScore+=m_nPieceValue;                   m_nTotalPieces+=1;                   if(m_nTotalPieces%30==0)m_nPlayLevel++;               }               removelines();                   transferPreToCur();               newPrePiece();                   m_bNeedNewPiece=false;           }           else           {                   m_bNeedNewPiece=!moveCurPiece(0,-1,false);                   if(!m_bNeedNewPiece) m_nPieceValue-=5;           }          m_tFrame.repaint();          sendStatus();       }       m_theThread=null;  }  private void sendStatus() {        //联机信息发送     }  private synchronized boolean moveCurPiece(int nDx,int nDy,boolean bRotate){      Square newpos[]=new Square[4];      for(int i=0;i<4;i++){        if(bRotate){          int dx=m_curPiece[i].m_nColumn-m_curPiece[0].m_nColumn;          int dy=m_curPiece[i].m_nRow-m_curPiece[0].m_nRow;            newpos[i]=new Square(m_curPiece[0].m_nColumn-dy,                               m_curPiece[0].m_nRow+dx,m_curPiece[i].m_nColor);        }        else {            newpos[i]=new Square(m_curPiece[i].m_nColumn+nDx,                                 m_curPiece[i].m_nRow+nDy,m_curPiece[i].m_nColor);        }      }      if(moveSquares(m_curPiece,newpos)==false)        return false;      m_curPiece=newpos;      return true;     }  boolean moveSquares(Square from[],Square to[]){       outerlable:       for(int i=0;i<to.length;i++){          if(to[i].InBounds()==false)            return false;          if(m_nField[to[i].m_nColumn][to[i].m_nRow]!=0){            for(int j=0;j<from.length;j++)              if(to[i].IsEqual(from[j]))                continue outerlable;            return false;          }         }      for(int i=0;i<from.length;i++)           if(from[i].InBounds())             m_nField[from[i].m_nColumn][from[i].m_nRow]=0;      for(int i=0;i<to.length;i++)        m_nField[to[i].m_nColumn][to[i].m_nRow]=to[i].m_nColor;        return true;     }  private void newPrePiece(){       int middle=m_nCols/2;       int top=m_nRows;       switch((int)(Math.random()*7)){         case 0://棍           m_nPieceValue=100;           m_prePiece[0]=new Square(middle-1,top-1,1);           m_prePiece[1]=new Square(middle-2,top-1,1);           m_prePiece[2]=new Square(middle,top-1,1);           m_prePiece[3]=new Square(middle+1,top-1,1);           break;         case 1://z形           m_nPieceValue=100;           m_prePiece[0]=new Square(middle,top-1,1);           m_prePiece[1]=new Square(middle-1,top-1,1);           m_prePiece[2]=new Square(middle-1,top,1);           m_prePiece[3]=new Square(middle+1,top,1);          break;         case 2://反z           m_nPieceValue=100;           m_prePiece[0]=new Square(middle-1,top,1);           m_prePiece[1]=new Square(middle,top,1);           m_prePiece[2]=new Square(middle,top-1,1);           m_prePiece[3]=new Square(middle+1,top-1,1);          break;          case 3://反L           m_nPieceValue=100;           m_prePiece[0]=new Square(middle-1,top-1,1);           m_prePiece[1]=new Square(middle-1,top,1);           m_prePiece[2]=new Square(middle,top,1);           m_prePiece[3]=new Square(middle+1,top,1);          break;          case 4://L形           m_nPieceValue=100;           m_prePiece[0]=new Square(middle-1,top,1);           m_prePiece[1]=new Square(middle,top,1);           m_prePiece[2]=new Square(middle+1,top,1);           m_prePiece[3]=new Square(middle+1,top-1,1);          break;          case 5://正方形           m_nPieceValue=100;           m_prePiece[0]=new Square(middle-1,top-1,1);           m_prePiece[1]=new Square(middle-1,top,1);           m_prePiece[2]=new Square(middle,top,1);           m_prePiece[3]=new Square(middle,top-1,1);          break;         case 6://|-形           m_nPieceValue=100;           m_prePiece[0]=new Square(middle-1,top,1);           m_prePiece[1]=new Square(middle,top,1);           m_prePiece[2]=new Square(middle,top-1,1);           m_prePiece[3]=new Square(middle+1,top,1);          break;       }     }     /**    * 将预览的方块转变成正在动的方块    */   private void transferPreToCur()   {        Square old[]=new Square[4];        old[0]=old[1]=old[2]=old[3]=new Square(-1,-1,0);        for(int i=0;i<4;i++)        {            m_curPiece[i]=m_prePiece[i];        }        m_bGameInPlay=moveSquares(old,m_curPiece);        //if(!m_bGameInPlay )m_tFrame.sendStr("GameOver:"+m_nTheScore);        //else if(!m_bGameInPlay && m_tFrame!=null)m_tFrame.insertScoreReport(m_nTheScore);   }   //去掉可以消去的行   private void removelines()   {        outerlabel:        for(int j=0;j<m_nRows;j++)        {                for(int i=0;i<m_nCols;i++)                  if(m_nField[i][j]==0)                      continue outerlabel;                for(int k=j;k<m_nRows-1;k++)                    for(int i=0;i<m_nCols;i++)                        m_nField[i][k]=m_nField[i][k+1];            j-=1;         //   m_tFrame.sendStr("RemoveLine");      }   }   public boolean keyDown(Event evt,int nKey)   {   if(!m_bGameInPlay)return true;   if(m_bPaused)return true;   switch(nKey){        case 'a':        case Event.LEFT	:            moveCurPiece(-1,0,false);            m_bNeedNewPiece=false;          m_tFrame.repaint();            break;        case 'd':        case Event.RIGHT:            moveCurPiece(1,0,false);            m_bNeedNewPiece=false;         m_tFrame.repaint();            break;        case 'w':        case Event.UP:            moveCurPiece(0,0,true);          m_tFrame.repaint();            break;        case 's':        case Event.DOWN:            while(moveCurPiece(0,-1,false));         m_tFrame.repaint();            break;       }       return true;   }   public synchronized void stop()   {        if(m_theThread!=null)                m_theThread.stop();        m_theThread=null;   }   public synchronized void pause()   {        if(m_theThread!=null)        {            try            {                m_theThread.suspend();                m_bPaused=true;            }            catch(Exception e){e.printStackTrace();}        }   }   public void setPlayLevel(int nLevel)   {        m_nPlayLevel=nLevel;//        System.out.println("m_nPlayLevel="+m_nPlayLevel);       /* Graphics g=getGraphics();               g.setFont(new Font("宋体",0,18));        int gx=m_nSqLength;            int gy=m_nSqLength*m_nRows/4+30;        g.clearRect(gx,gy-25,XOFFSET-gx,25);            g.drawString("Level:"+m_nPlayLevel,gx,gy);*///        System.out.println("paint m_nPlayLevel="+m_nPlayLevel);   }   public int getPlayLevel()   {        return m_nPlayLevel;   }   public synchronized void addRandomLine()   {        int nRandom[]=new int[m_nCols];        boolean bAllZero=true;        boolean bNoZero=true;        for(int i=0;i<m_nCols;i++)        {            nRandom[i]=(int)(7*Math.random());            if(nRandom[i]!=0)bAllZero=false;            else bNoZero=false;        }        if(bAllZero)        {            nRandom[(int)(m_nCols*Math.random())]=(int)(Math.random()*6+1);        }        else if(bNoZero)        {            nRandom[(int)(m_nCols*Math.random())]=0;        }        for(int nCol=0;nCol<m_nCols;nCol++)            for(int nRow=m_nRows+3;nRow>0;nRow--)            {                m_nField[nCol][nRow]=m_nField[nCol][nRow-1];            }        for(int nCol=0;nCol<m_nCols;nCol++)m_nField[nCol][0]=nRandom[nCol];        for(int i=0;i<4;i++)        {            m_curPiece[i].m_nRow++;        }   }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -