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

📄 gameengine.java

📁 我是一个j2ee,开发人员
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                                 clearAllFullRow();
                       }
              }
     }
    
     /**
      *随机产生方块
      */
     public void gernerateBrick(){
              //把下一个方块的类型赋值给当前方块
              brickType = nextBrickType;
              //生成下一个的类型
              nextBrickType = Math.abs(r.nextInt() % brick.length);
              //初始化数据
              index = 0;
              //位置
              cRow = -3;
              cCol = 3;
     }
    
     /**
      *判断方块是否下落到游戏区域底部
      * @return true代表下落到底部
      */
     private boolean reachBottom(){     
              int tempRow = getBottomNotEmptyRow();
              //是否是最后一行
              if(cRow + tempRow >= MAXROW - 1){
                       return true;
              }else{
                       return false;
              }
     }
    
     /**
      *添加方块到地图数据
      */
     private void addBrickToMap(){
              for(int i = 0;i < 4;i++){
                       for(int j = 0;j < 4;j++){
                                 //判断数据未超出地图边界
                                 if(((cCol+j) >= 0) &&
                                                   ((cCol + j < MAXCOL)) &&
                                                   ((cRow + i < MAXROW))){
                                          //不添加0的数据
                                          if(brick[brickType][index][i][j] == 1){
                                                   map[cRow + i][cCol + j] =
                                                             brick[brickType][index][i][j];
                                          }
                                 }
                       }
              }
     }
    
     /**
      *方块变形
      */
     public void changeShape(){
              //变形到下一种形状
              index++;
              if(index == brick[brickType].length){
                       index = 0;
              }
              //变形以后的位置是否超出边界
              if(testNewPosition()){
                       index--;//退回原来的形状
              }
              if(index < 0){
                       index = brick[brickType].length - 1;
              }
     }
    
     /**
      *方块向左移动
      *原则是:方块左侧的第一行非空列贴近边界
      */
     public void moveToLeft(){
             cCol--;
             //如果新的位置不可用
             if(testNewPosition()){
                         cCol++;
             }
     }
    
     /**
      *获得方块左侧的非空列的序号
      * @return
      */
     private int getLeftNotEmptyLine(){
              for(int col = 0;col < 4;col++){
                       for(int row = 0;row < 4;row++){
                                 if(brick[brickType][index][row][col] == 1){
                                          return col;
                                 }
                       }
              }
              return 0;
     }
    
     /**
      *方块向右移动
      */
     public void moveToRight(){
                cCol++;
                //如果新的位置不可用
                if(testNewPosition()){
                          cCol--;
               }
     }
    
     /**
      *获得方块右侧第一个非空列的序号
      * @return非空列的序号
      */
     private int getRightNotEmptyLine(){
              for(int col = 3;col >= 0;col--){
                       for(int row = 0;row < 4;row++){
                                 if(brick[brickType][index][row][col] == 1){
                                          return col;
                                 }
                       }
              }
              return 3;
     }
    
     /**
      *方块底部的第一个非空行的行号
      * @return行号
      */
     private int getBottomNotEmptyRow(){
              for(int row = 3;row >= 0;row--){
                       for(int col = 0;col < 4;col++){
                                 if(brick[brickType][index][row][col] == 1){
                                          //System.out.println("底部非空行:" + row);
                                          return row;
                                 }
                       }
              }
              return 3;
     }
    
     /**
      *测试新的位置是否可用
      * @return true代表不可用,false代表可用
      */
     private boolean testNewPosition(){
              //左侧
              if((getLeftNotEmptyLine() + cCol) < 0){
                       return true;
              }
              //右侧
              if(cCol + getRightNotEmptyLine() > MAXCOL - 1){
                       return true;
              }
              //下边界
              if(getBottomNotEmptyRow() + cRow >= MAXROW - 1){
                       System.out.println(222);
                       return true;
              }
              //是否和地图重合
              if(collisWithMap()){
                       return true;
              }
              return false;
     }
    
     /**
      *是否和已有的方块叠加
      * @return true代表叠加,false代表未叠加
      */
     private boolean collisWithMap(){
              for(int col = 0;col < 4;col++){
                       for(int row = 3;row >= 0;row--){
                                 //有格子
                                 if(brick[brickType][index][row][col] == 1){
                                          //下标未越界
                                          if((cRow +row >= 0) && (cRow + row <= MAXROW - 1) &&
                                                             (cCol + col >= 0) && (cCol + col <= MAXCOL - 1)){
                                                   //判别地图数据
                                                   if(map[cRow + row][cCol + col] == 1){ //重叠
                                                             return true;
                                                   }else{
                                                             break;
                                                   }
                                          }
                                 }
                       }
              }
              return false;
     }
    
     /**
      *清除满行
      */
     private void clearAllFullRow(){
              for(int row = MAXROW - 1;row >= 0;row--){
                       //如果是满行
                       if(isFullRow(row)){
                                 //增加积分
                                 score += 10;
                                 //消当前行
                                 clearOneFullRow(row);
                                 row++; //继续处理当前行
                       }
              }
     }
    
     /**
      *判断是否是满行
      * @param row行号
      * @return true代表是满行,false代表不是满行
      */
     private boolean isFullRow(int row){
              int count = 0;
              for(int col = 0;col < MAXCOL;col++){
                       if(map[row][col] == 1){
                                 count++;
                       }
              }
              if(count == MAXCOL){
                       return true;
              }else{
                       return false;
              }
     }
    
     /**
      *消掉一个满行
      * @param row需要消的行号
      */
     private void clearOneFullRow(int row){
              //上面的行全部下落
              for(int i = row - 1;i >= 0;i--){ //循环上面所有的行
                       for(int col = 0;col < MAXCOL;col++){
                                 //下落
                                 map[i + 1][col] = map[i][col];
                       }
              }
              //把第一行数据全部初始化为空
              for(int col = 0;col < MAXCOL;col++){
                       map[0][col] = 0;
              }
     }
    
     /**
      *游戏是否结束
      * @return true代表结束,false代表未结束
      */
     public boolean isGameOver(){
              //下一个位置是否和地图数据重合
              cRow++;
              if(collisWithMap()){
                       cRow--; //还原位置
                       //判断屏幕第一行以上是否有数据
                       if(cRow < 0){
                                 for(int row = 0;row < 4;row++){
                                          for(int col = 0;col < 4;col++){
                                                   if(row + cRow >= 0){
                                                             break;
                                                   }else{ //屏幕上面的行
                                                             if(brick[brickType][index][row][col] == 1){
                                                                      return true;
                                                             }
                                                   }
                                          }
                                 }
                       }
              }else{
                       cRow--;
              }
              return false;
     }
    
     /**
      *高速下落,速度是正常速度的2倍
      */
     public void highSpeed(){
              times = 5;
     }
    
     /**
      *正常速度
      */
     public void normalSpeed(){
              times = 1;
     }
}

⌨️ 快捷键说明

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