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

📄 ptetrics.java~146~

📁 JBuilder实现的具有单机
💻 JAVA~146~
📖 第 1 页 / 共 5 页
字号:
package mymatrix;
import java.awt.*;
import dialog.GameOverD;

public class PTetrics extends Panel implements Runnable
{  //Graphics cg;
    //每个小方块的大小:宽,高
   int m_nSqLength;
   //信息区大小
   final int XOFFSET=100;
   final int RIVAL_XOFFSET=500;
   final int MID_XOFFSET=400;

   static int m_nCols;   //the number of cols of the game
   static int m_nRows;


   int m_nField[][];   //descripe the status of the game now

   Square m_curPiece[]=new Square[4];  //the group of square that are playing now
   Square m_prePiece[]=new Square[4];
   boolean m_bGameInPlay;    //whether the game is playing now

   boolean m_bNeedNewPiece;  //whether need a new piece now


   boolean m_bPaused=false;

   boolean m_drawback=true;


   boolean c_bGameInPlay;
   boolean c_bNeedNewPiece;
   Square c_prePiece[]=new Square[4];
   Square c_curPiece[]=new Square[4];
   int m_nRivalField[][];

   Thread m_theThread=null;
   Color m_colors[];
   int m_nPieceValue,c_nPieceValue,m_nTheScore=0,c_nTheScore=0;//the score of the piece and the score of
                                   //the game
   int m_nPlayLevel;               //the level of the game

   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;

   int c_type;

   Graphics m_gOffGraph;

   TFrame m_tFrame;

   public char key[]={'a','d','w','s'};



    public PTetrics()
    {
        super();
        setBackground(BAKCOLOR);
        initParam();
        //drawbackgroud();
    }

    public PTetrics(TFrame tFrame)
    {
        super();
        m_tFrame=tFrame;
        setBackground(BAKCOLOR);
        initParam();
       // drawbackgroud();
    }
   public void initParam()
   {
   //设置初始参数
        m_nSqLength=20;
        m_nCols=10;
        m_nRows=20;
        m_nField=new int [m_nCols][m_nRows+4];
      //w  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(60,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);
    //to initialize the m_prePiece;


   }

   public synchronized void start()
   {


     if(m_theThread!=null)
        //游戏是被暂停,而不是重新开始
        {
            m_bPaused=false;
            m_theThread.resume();
            return;
        }
        repaint();
        //重新开始赋上游戏的状态
        for(int i=0;i<m_nCols;i++)
        {
          for(int j=0;j<m_nRows+4;j++)
          {
            m_nField[i][j]=0;
            m_nRivalField[i][j]=0;
          }
        }

        m_bNeedNewPiece=true;
        c_bNeedNewPiece=true;
        m_bGameInPlay=true;
        c_bGameInPlay=true;

        m_theThread=new Thread(this);
        newPrePiece();
        cnewPrePiece();
        m_theThread.start();
        requestFocus();
   }

   public void update(Graphics g)
   {
            m_bJustupdating=true;
            paint(g);
   }


   public synchronized void paint(Graphics g)
   {

            g.setFont(new Font("宋体",0,18));

            int gx=MID_XOFFSET-3*m_nSqLength;
            int gy=50;
            g.clearRect(gx,gy-25,150,25);
            g.drawString("Your Score:"+m_nTheScore,gx,gy);

            gy+=30;
            g.clearRect(gx,gy-25,150,25);
            g.drawString("Rival Score:"+c_nTheScore,gx,gy);

            gy+=30;
            g.clearRect(gx,gy-25,100,25);
            g.drawString("Level:"+m_nPlayLevel,gx,gy);


        //画预览的方块
        int middle=m_nCols/2;
        int top=m_nRows;
        gx=160;
        gy=10;
        g.setColor(FORCOLOR);
        g.fill3DRect(gx,gy,m_nSqLength*4,m_nSqLength*4,true);

        if(m_bGameInPlay&&c_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);
            }
        }

        gx=560;
        g.setColor(FORCOLOR);
        g.fill3DRect(gx,gy,m_nSqLength*4,m_nSqLength*4,true);
        if(m_bGameInPlay&&c_bGameInPlay)
        {
            for(int i=0;i<4;i++)
            {
                g.setColor(m_colors[c_prePiece[i].m_nColor]);
                g.fill3DRect((c_prePiece[i].m_nColumn-middle+2)*m_nSqLength+gx,
                    gy-(c_prePiece[i].m_nRow-top)*m_nSqLength,m_nSqLength,m_nSqLength,true);
            }
        }


       Image img1=createImage(m_nSqLength*10,m_nSqLength*20);
       //用来画自己的游戏区域
       Image img2=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++)
            {
                //-1代表没有刚刚开始

               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,115,this);
           g.drawImage(img2,RIVAL_XOFFSET,115,this);
           m_bJustupdating=false;
   }

   public void run()
   {
      while(m_bGameInPlay&&c_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;

             }
             removelines();
                 transferPreToCur();
             newPrePiece();
                 m_bNeedNewPiece=false;
         }
         else
         {
                 m_bNeedNewPiece=!moveCurPiece(0,-1,false);
                 if(!m_bNeedNewPiece) m_nPieceValue-=5;
         }


         if(c_bNeedNewPiece)
         {
                 if(c_nPieceValue>0)
             {
                 c_nTheScore+=c_nPieceValue;
             }
             cremovelines();
             ctransferPreToCur();

             adjust(c_type);
             cnewPrePiece();

                 c_bNeedNewPiece=false;
         }
         else
         {
                 c_bNeedNewPiece=!cmoveCurPiece(0,-1,false);
                 if(!c_bNeedNewPiece) c_nPieceValue-=5;


          }

        repaint();

     }
     new GameOverD(m_nTheScore, c_nTheScore);
     m_tFrame.stop();
     m_theThread=null;
   }

   private synchronized boolean moveCurPiece(int nDx,int nDy,boolean bRotate)
   {//nDx:move left(-1) ior right; nDy:move up or down(-1)
        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;
   }
   /**
    * 移动方块,如果不能移动,则返回假
    */

   private synchronized boolean cmoveCurPiece(int nDx,int nDy,boolean bRotate)
   {//nDx:move left(-1) ior right; nDy:move up or down(-1)

        Square newpos[]=new Square[4];
        for(int i=0;i<4;i++){
          if(bRotate)
          {
            int dx=c_curPiece[i].m_nColumn-c_curPiece[0].m_nColumn;
            int dy=c_curPiece[i].m_nRow-c_curPiece[0].m_nRow;
            newpos[i]=new Square(c_curPiece[0].m_nColumn-dy,c_curPiece[0].m_nRow+dx,c_curPiece[i].m_nColor);
          }
          else
          {
            newpos[i]=new Square(c_curPiece[i].m_nColumn+nDx,c_curPiece[i].m_nRow+nDy,c_curPiece[i].m_nColor);
          }
        }
        if(cmoveSquares(c_curPiece,newpos)==false) return false;
        c_curPiece=newpos;
        return true;
   }

   boolean cmoveSquares(Square from[],Square to[])
   {
         //判断是否能移动
    outerlable:
            for (int i=0;i<to.length;i++)
        {
              if(to[i].InBounds()==false) return false;
          //如果不在可玩区域,则返回假
              if(m_nRivalField[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_nRivalField[from[i].m_nColumn][from[i].m_nRow]=0;

            for(int i=0;i<to.length;i++)
              m_nRivalField[to[i].m_nColumn][to[i].m_nRow]=to[i].m_nColor;
                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;

⌨️ 快捷键说明

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