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

📄 maincanvas.java

📁 Java游戏山洞历险源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
          map[i+1][j] = map[i][j];          map[i][j] = -1;          count --;          break;        }      }    }    if(count == 2){ //2个释放方块均已下落      isDowning = false;//可以继续释放新的方块      //判断新的下落方块是否引起爆炸      if(isArranging == false && bombCount > 0){//没有进行方块整理,并且地图中有炸弹        for(int i=hFlagChooser+1;i< 7;i++){ //行          for(int j = 0; j<6; j++){  //列            if(map[i][j] % 3 > 0){ //在整个地图寻找炸弹              if(will_bomb(i,j) == true) {  //是否能爆炸                //要爆炸的方块不可能导致颜色增加,去掉这种颜色                for(int k =0; k<2; k++){                  if(Color[k]>=0 && Color[k] == map[i][j]/3){                    Color[k] = -1;                    break;                  }                }                colorTemp[scoreCount] = map[i][j] /3;                do_bomb(i,j,map[i][j]/3);                isBombed = true;                scoreCount ++;                count++;              }            }          }        }      }      //检验是否增加新的颜色      if(count > 0){        for(int j =0; j<2; j++){          if(Color[j] != -1){            int i;            for(i =0; i<popoEx.flagThrow; i++){              if(Color[j]*3 == popoEx.color[i])                break;            }            if(i>=popoEx.flagThrow){//增加新的颜色              popoEx.color[popoEx.flagThrow] = Color[j]*3;              for(i=0; i<4; i++){                if(colorInit[i] == Color[j]*3){                  color[i] = true;                  break;                }              }              if(popoEx.flagThrow<4)                popoEx.flagThrow++;              indexThrow = 0;              Color[j] = -1;              //set颜色数组              if(level==3 && popoEx.flagThrow<4)                popoEx.setThrower(popoEx.flagThrow);              else if(popoEx.flagThrow<3)                popoEx.setThrower(popoEx.flagThrow);              //trace              System.out.println("indexThrow:  "+indexThrow);///////////////              System.out.println("popoEx.flagThrow:  "+popoEx.flagThrow);///////////              System.out.println("popoEx.color:  "+popoEx.color[0]+" "+popoEx.color[1]+" "+popoEx.color[2]+" "+popoEx.color[3]);///////////              System.out.println("color:  "+color[0]+" "+color[1]+" "+color[2]+" "+color[3]);///////////            }          }        }      }      if(count == 2){ //下落方块没有引起爆炸        //计算地图最高高度        getMaxHeight();        //发射行移动        if( Math.abs(hFlagMap - hFlagChooser) != 2 ){          chooser_change();        }        //是否game over        if(hFlagMap == 1){          if(is_over())            do_over();        }        //判断是否增加颜色      }    }  }   //计算地图最高高度  public void getMaxHeight(){    int i =hFlagChooser + 1, j = 0;    while(map[i][j] == -1){      j++;      if(j==6){        j=0;        i++;        if(i==7)          break;      }    }    hFlagMap = i;  }  //判断游戏是否失败  public boolean is_over(){    int count = 0;    for(int i=0; i<5; i++)    {      if(map[1][i]==-1 &&  map[1][i+1] == -1)          count++;    }    if(count == 0)      return true;    return false;  }  //game over  public void do_over(){    isLost = true;    thread = null;    aTimer.cancel();    repaint();  }  //过关  public void do_clear(){    isClear = true;    thread = null;    aTimer.cancel();    level ++;//进入下一关    if(level > 3)      level = 1;    repaint();  }  //爆炸后重新排列地图  public void do_arrange(){    int count = 0;    for(int j = 0; j < 6; j++){  //列      for(int i = hFlagChooser+1; i<6; i++){ //行        if(map[i][j] >= 0 && map[i+1][j] == -1){          for(int k = i; k>=hFlagChooser+1; k--){           map[k+1][j] = map[k][j];           map[k][j] = -1;          }          count ++;          break;        }      }    }    if(count == 0){//arrange结束      //检查重新排列之后是否又有方块爆炸      if(bombCount > 0){//还有炸弹        for(int i=hFlagChooser+1;i< 7;i++){ //行 再优化          for(int j = 0; j< 6; j++){  //列            if(map[i][j] >= 0){              if(map[i][j] % 3 > 0){ //在整个地图寻找炸弹              if(will_bomb(i,j) == true) {  //是否能爆炸                //记录颜色                for(int k =0; k<2; k++){                  if(Color[k]>=0 && Color[k] == map[i][j]/3)                    Color[k] = -1;                }                colorTemp[scoreCount] = map[i][j] /3;                do_bomb(i,j,map[i][j]/3);                isBombed = true;//爆炸标志                scoreCount++;                count++;              }             }            }          }        }      }      if(count == 0){//没有新的爆炸        //加分        tempScore -= 20*scoreCount;//炸弹本身不计数        score +=tempScore * scoreCount;        System.out.println("tempScore: "+tempScore);////////////        System.out.println("scoreCount:  "+scoreCount);////////////        tempScore = 0;        repaint(25,frameHeight-25,55,25);//更新分数        //查找地图中的颜色,是否需要消去某些颜色        for(int i =0; i< scoreCount ; i++){          int j =0,k =0, Color = colorTemp[i];          for(j = hFlagMap; j<7; j++){            for(k = 0; k<6; k++){              if(map[j][k]>=0 && map[j][k]/3 ==Color)                break;            }            if(k<6)  break;          }          if(j>=7){//地图中不存在这种颜色,需要消去            count++;            popoEx.flagThrow --;            for(k = 0;k<4; k++){              if(Color*3 == colorInit[k]){                color[k] = false;                break;              }            }          }        }        if(count>0){ //有需要消去的颜色          int j =0;          for(int i =0; i< popoEx.flagThrow; i++){            while(j<4){              if(color[j] == true){                popoEx.color[i] = colorInit[j];                j++;                break;              }              j++;            }          }          //重设数组          if(level == 3 ){            if(popoEx.flagThrow<4)              popoEx.setThrower(popoEx.flagThrow);            }else if(popoEx.flagThrow<3)              popoEx.setThrower(popoEx.flagThrow);          indexThrow = 0;          //trace          System.out.println("indexThrow:  "+indexThrow);///////////////          System.out.println("popoEx.flagThrow:  "+popoEx.flagThrow);///////////          System.out.println("popoEx.color:  "+popoEx.color[0]+" "+popoEx.color[1]+" "+popoEx.color[2]+" "+popoEx.color[3]);///////////          System.out.println("color:  "+color[0]+" "+color[1]+" "+color[2]+" "+color[3]);///////////        }        //更新参数        scoreCount = 0;        isArranging = false;        getMaxHeight();        if( Math.abs(hFlagMap - hFlagChooser) != 2 ) chooser_change();//发射行移动        if(hFlagMap == 7) //到底          do_clear();//游戏过关        else //是否game over          if(hFlagMap == 1){            if(is_over()) do_over();          }      }    }  }  public void next(){    if(isDowning == true) do_down();//发射的方块正在下落    if(isArranging == true) do_arrange();//正在进行爆炸后的地图重排  }   public void run(){     Thread _thread = Thread.currentThread();     long scheduled = System.currentTimeMillis();     while (thread == _thread) {       synchronized (this) {         try {           this.wait(200);           next();           //重绘地图区,time and score不重绘           repaint(chooserX,chooserY,recWidth*7,recHeight*8);           serviceRepaints();         } catch (InterruptedException e) {         }       }     }   }    protected void paint(Graphics g) {      if (images != null) {        g.drawImage((Image)images.elementAt(18),                             1, 0, Graphics.LEFT|Graphics.TOP);        //画所有方块         //爆炸效果         if(isBombed == true){//有爆炸,需要绘制bomb数组           countFlash ++;           for(int i=0; i < 7; i++){            for(int j=0; j < 6; j++){              if(map[i][j] < 0){ //可能有爆炸的方块                if(bomb[i][j] > 0){//查询bomb数组                  g.drawImage((Image)images.elementAt(bomb[i][j]),                             recX+j*recWidth, recY+i*recHeight, Graphics.LEFT|Graphics.TOP);                  //闪烁效果                  if(countFlash == 2){                    bomb[i][j] = -1;                  }                }              }else {//此位置没有爆炸的方块                g.drawImage((Image)images.elementAt(map[i][j]),                             recX+j*recWidth, recY+i*recHeight, Graphics.LEFT|Graphics.TOP);                if(map[i][j] % 3 == 1)                  map[i][j] ++;                else if(map[i][j] % 3 == 2)                  map[i][j] --;              }            }           }           if(countFlash == 2){             isArranging = true;             isBombed = false;             countFlash = 0;           }         }else{//没有爆炸,绘制地图即可           for(int i=0; i < 7; i++){            for(int j=0; j < 6; j++){              if(map[i][j] >= 0){                g.drawImage((Image)images.elementAt(map[i][j]),                             recX+j*recWidth, recY+i*recHeight, Graphics.LEFT|Graphics.TOP);                if(map[i][j] % 3 == 1)                  map[i][j] ++;                else if(map[i][j] % 3 == 2)                  map[i][j] --;              }            }          }         }        //画选择框         g.drawImage((Image)images.elementAt(indexChooser),                             chooserX+wFlagChooser*recWidth, chooserY+hFlagChooser*recHeight, Graphics.LEFT|Graphics.TOP);         if(isLost == true){            g.drawImage((Image)images.elementAt(indexLost),                        frameWidth/2-74, frameHeight/2-12 ,Graphics.LEFT|Graphics.TOP);         }else if(isClear == true) {           g.drawImage((Image)images.elementAt(indexClear),                        frameWidth/2-51, frameHeight/2-12 ,Graphics.LEFT|Graphics.TOP);         }         //set font for score and time         g.setFont(font);         g.setColor(0xff0000);         //pause         if(isPause == true)           g.drawString("PAUSE !",frameWidth/2-25,frameHeight/2-25,Graphics.LEFT|Graphics.TOP);         //score         g.drawString(Integer.toString(score),25,frameHeight-25,Graphics.LEFT|Graphics.TOP);         //time         g.drawString(Integer.toString(time/600),frameWidth-65,frameHeight-25,Graphics.LEFT|Graphics.TOP);         g.drawString(Integer.toString(time/60%10),frameWidth-55,frameHeight-25,Graphics.LEFT|Graphics.TOP);         g.drawString(" : ",frameWidth-50,frameHeight-25,Graphics.LEFT|Graphics.TOP);         g.drawString(Integer.toString(time%60/10),frameWidth-40,frameHeight-25,Graphics.LEFT|Graphics.TOP);         g.drawString(Integer.toString(time%60%10),frameWidth-30,frameHeight-25,Graphics.LEFT|Graphics.TOP);      }    }    class GameTimerTask extends TimerTask{       public void run(){         long now = System.currentTimeMillis();         time ++;         repaint(frameWidth-55,frameHeight-25,55,25);       }    }}

⌨️ 快捷键说明

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