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

📄 tetrics.java

📁 俄罗斯方块游戏,大学时写的,支持单机及点对点网络对战!同时我还在里面加入了使用技能的功能!不过有一个缺点,呵呵好象代码写的不够严谨,所以现在性能很低,我一直也没有优化它!
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    */
   boolean moveSquares(Square from[],Square to[])
   {
         //判断是否能移动
    outerlable://定义标识变量
            for (int i=0;i<to.length;i++)
        {
              if(to[i].InBounds()==false) return false;
          //如果不在可玩区域,则返回假,不能移动
              if(oGameArea.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())//方块在游戏区
                 oGameArea.m_nField[from[i].m_nColumn][from[i].m_nRow]=0;//将原位置方块擦掉

            for(int i=0;i<to.length;i++)//在下一位置绘制方块
              oGameArea.m_nField[to[i].m_nColumn][to[i].m_nRow]=to[i].m_nColor;
                return true;//可以移动
   }

   /**
    * 将预览的方块转变成正在动的方块
    */
   private void transferPreToCur()
   {
        Square old[]=new Square[4];//声名新方块实例
        for(int i=0;i<4;i++)
        old[i]=new Square(-1,-1,0,0);//定位新生成的方块

        for(int i=0;i<4;i++)
        {
            m_curPiece[i]=nextPiece.m_prePiece[i];//将预览区方块移到游戏区
            m_nTotalPieces++;//下落的方块+1
            mvalue+=nextPiece.m_prePiece[i].m_nPieceValue;
        }

        m_bGameInPlay=moveSquares(old,m_curPiece);//若方块可移动则游戏可进行
        if(!m_bGameInPlay && m_tFrame.m_nNetStatus!=Frame1.NOCONNECT){
          m_tFrame.sendStr("GameOver:" + sScore.m_nTheScore);
         // m_tFrame.ScoreReport(sScore.m_nTheScore,m_tFrame.m_oScore);
        }
        else if(!m_bGameInPlay && m_tFrame!=null)m_tFrame.ScoreReport(m_tFrame.jLabel8.getText(),sScore.m_nTheScore);
   }
   //去掉可以消去的行
   private void removelines()
   {
        outerlabel://标识
        for(int j=0;j<m_nRows;j++)//判断游戏区内方块状态
        {
                for(int i=0;i<m_nCols;i++)//判断列
                  if(oGameArea.m_nField[i][j]==0)
                      continue outerlabel;//返回标识并继续
                  //消行
                for(int k=j;k<m_nRows-1;k++)
                    for(int i=0;i<m_nCols;i++)
                      oGameArea.m_nField[i][k] = oGameArea.m_nField[i][k + 1]; //将游戏第k+1行状态赋给第k行

                  sScore.m_nTheScore +=mvalue; //分数增加
                  m_nOldScore += mvalue; //累加分数增加

            j-=1;//下移一行
            m_sound.remov_voice();
            nextPiece.lines++;
            System.out.println(nextPiece.lines);
      }
        if(nextPiece.lines>=3)canuseskill=true;
   }

   //键盘响应
   public boolean keyDown(Event evt,int nKey)
   {//判断游戏是否在游戏区及游戏是否暂停
   if(!m_bGameInPlay)return true;
   if(m_bPaused)return true;
   //按键响应
   switch(nKey){
        case 'p'://暂停键
        case KeyEvent.VK_P:
          this.pause();
          m_tFrame.sendStr("PauseGame");
          m_repaint();
          break;
        case 'q'://游戏结束
      case KeyEvent.VK_Q:
        this.stop();
        m_repaint();
        break;


        case 'z'://技能
        case KeyEvent.VK_Z:
          if(canuseskill){
            m_tFrame.sendStr("addSpeed");
            canuseskill=false;
          }
         else
           JOptionPane.showConfirmDialog(this,"条件不足,不能使用次技能!!!","提示",
                                                   JOptionPane.DEFAULT_OPTION,JOptionPane.WARNING_MESSAGE);//提示不能使用技能
         m_repaint();
         break;

         case 'x'://技能
         case KeyEvent.VK_X:
           if(canuseskill){
             m_tFrame.sendStr("addRandomBox");
             canuseskill=false;
           }
           else
       JOptionPane.showConfirmDialog(this,"条件不足,不能使用次技能!!!","提示",
                                               JOptionPane.DEFAULT_OPTION,JOptionPane.WARNING_MESSAGE);//提示不能使用技能

             m_repaint();
             break;

         case 'c'://技能
         case KeyEvent.VK_C:
           if(canuseskill){
             m_tFrame.sendStr("addRandomLine");
             canuseskill=false;
           }
           else
         JOptionPane.showConfirmDialog(this,"条件不足,不能使用次技能!!!","提示",
                                                 JOptionPane.DEFAULT_OPTION,JOptionPane.WARNING_MESSAGE);//提示不能使用技能
           m_repaint();
           break;

        case 'a':
        case Event.LEFT	://‘a ’键或左键
            moveCurPiece(-1,0,false);//方块左移1单位
            nextPiece.m_bNeedNewPiece=false;//不需生成新方块
          m_repaint();

            break;//跳出本次操作

        case 'd':
        case Event.RIGHT://‘d’键或右键
            moveCurPiece(1,0,false);//方块右移1单位
            nextPiece.m_bNeedNewPiece=false;
          m_repaint();
            break;//跳出本次操作

        case 'w':
        case Event.UP://‘w’键或上键
            moveCurPiece(0,0,true);//方块不动
           m_repaint();
           m_sound.remot_voice();
            break;//跳出本次操作

        case 's':
        case Event.DOWN://‘s’键或下键
           addspeed();
            break;//跳出本次操作
       }
       return true;
   }

  //结束游戏
   public synchronized void stop()
   {     b=-1;//游戏结束状态
        if(m_theThread!=null)
           m_theThread.stop();//线程停止
           m_theThread=null;//清空线程
   }

//暂停游戏
   public synchronized void pause()
   {     b=1;//游戏暂停状态
        if(m_theThread!=null)
        {
            try
            {
                m_theThread.suspend();//暂停线程
                m_bPaused=true;//m_bPaused赋真值
            }
            catch(Exception e){e.printStackTrace();}
        }
   }

//设置游戏难度
   public void setPlayLevel(int nLevel)
   {
        sScore.m_nPlayLevel=nLevel;//将传来的参数设为难度
        System.out.println("m_nPlayLevel="+sScore.m_nPlayLevel);//在cmd窗口中输出"m_nPlayLevel=难度"
       if(m_tFrame.m_nNetStatus!=Frame1.NOCONNECT)
       m_tFrame.sendStr("Level:"+nLevel);//将难度设置传递给对方
   }

//得到游戏难度
   public int getPlayLevel()
   {
        return sScore.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;//若本行内有无快则bAllZero为假
            else bNoZero=false;//若本行内都为方块则bNoZero为假
        }
        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--)//
            {
                oGameArea.m_nField[nCol][nRow]=oGameArea.m_nField[nCol][nRow-1];//将原游戏区底0-22行游戏画面赋给新游戏画面的第1-23行
            }
        for(int nCol=0;nCol<m_nCols;nCol++)oGameArea. m_nField[nCol][0]=nRandom[nCol];//将随机产生的方块加入游戏区第0行
        for(int i=0;i<4;i++)
        {
            m_curPiece[i].m_nRow++;//游戏区内正在下落方块上移1行
        }
   }

   //添加随机方块
  public synchronized void addRandomBox(){
    //随机产生1到5个方块
       int r = (int) (5 * Math.random()+1);
       int nRandom[]= new int[r]; //定义整形数组,存放随机生成的方块
       //随机选择一组行和列
       int m_cols[]=new int[r];
       int m_rows[]=new int[r];
       for(int i=0;i<r;i++){
        nRandom[i]=(int) (6 * Math.random()+1);
        m_cols[i]=(int) (10 * Math.random());
        m_rows[i]=(int) (14 * Math.random());
       }
       //将随机产生的方块赋给游戏区域
              for(int k=0;k<r;k++){
            oGameArea.m_nField[m_cols[k]][m_rows[k]]=nRandom[k];
            }
   }
   public synchronized void addspeed(){
     //方块快速下落
     while(moveCurPiece(0,-1,false));//方块持续下移
           m_repaint();
           m_sound.down_voice();

   }
}

⌨️ 快捷键说明

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