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

📄 mgame.java

📁 java2应用编程150例
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
              }
              catch(IOException ioexception) {}
              catch(NullPointerException npe){}
              //////////////////////////////////////////////
          }
      }

      public void activate(Display disp)
      {
          display = disp;
          display.setCurrent(this); // 设置显示目标
          setCommandListener(this); // 监视菜单选择
      }

      // 是否全部完成判断
      private boolean Judge()
      {
          if (leftbomb == 0)
          {
              int i,j;
              for(i=0;i<Height; i++)
              {
                  for(j=0; j<Width; j++)
                  {
                      if (grid[i][j] >= 10 && grid[i][j] < 20) return false;
                  }
              }
              return true;
          }
          else
              return false;
      }

      // 开始新游戏
      private void newGame()
      {
          gameover = false;
          selx = 0;
          sely = 0;
          leftbomb = MINECOUNT;

          int i,j,x,y;
          for(i=0;i<Height; i++)
          {
              for(j=0; j<Width; j++)
                  grid[i][j] = 10;
          }

          for(i=0; i<MINECOUNT; i++)
          {
              while(true)
              {
                  x = Math.abs(rand.nextInt()) % Width;
                  y = Math.abs(rand.nextInt()) % Height;
                  if (grid[y][x] != 19)
                  {
                      grid[y][x] = 19;
                      break;
                  }
              }
          }

          for(i=0;i<Height; i++)
          {
              for(j=0; j<Width; j++)
              {

                  if (grid[i][j] == 19) continue;
                  int k, l;
                  for (k=-1; k<2; k++)
                  {
                      if (i+k<0) continue;
                      if (i+k>=Height) continue;
                      for (l=-1; l<2; l++)
                      {
                          if (l+j<0) continue;
                          if (l+j>=Width) continue;
                          if (k==0 && l==0) continue;

                          if (grid[i+k][j+l] == 19) grid[i][j]++;
                      }
                  }
              }
          }

          ExScreenImg.clear((byte)0);
          for (i=0; i<=Width; i++)
          {
              Exg.drawLine(i*GRIDSIZE, 0, i*GRIDSIZE, Height*GRIDSIZE);
          }
          for (i=0; i<=Height; i++)
          {
              Exg.drawLine(0, i*GRIDSIZE, Width*GRIDSIZE, i*GRIDSIZE);
          }

          for(i=0;i<Height; i++)
          {
              for(j=0; j<Width; j++)
              {
                  Exg.drawImage(HideImg, j*GRIDSIZE+1, i*GRIDSIZE+1, 20);
              }
          }
          Exg.drawImage(fHideImg, selx*GRIDSIZE+1, sely*GRIDSIZE+1, 20);
          Exg.drawString("剩", 101, 0, Graphics.RIGHT | Graphics.TOP);
          Exg.drawString("余", 101, 13, Graphics.RIGHT | Graphics.TOP);
          Exg.drawString(""+leftbomb, 101, 26, Graphics.RIGHT | Graphics.TOP);

          SoundPlay(2);
      }

      // 画一个格子
      // focus标示着个格子是否为焦点,如果为true,则要画反色图形
      private int DrawBlock(int x, int y, boolean focus)
      {
          int retval = 0;
          if (grid[y][x] == 0)
          {
              if (!focus) Exg.setColor(0xffffff);
              Exg.fillRect(x*GRIDSIZE+1, y*GRIDSIZE+1, GRIDSIZE-1, GRIDSIZE-1);
              if (!focus) Exg.setColor(0);
          }
          else if (grid[y][x]>0 && grid[y][x]<9)
          {
              if (focus)
                  Exg.drawImage(fNumImg[grid[y][x]-1], x*GRIDSIZE+1, y*GRIDSIZE+1, 20);
              else
                  Exg.drawImage(NumImg[grid[y][x]-1], x*GRIDSIZE+1, y*GRIDSIZE+1, 20);
          }
          else if (grid[y][x] == 9)
          {
              int i, j;
              for(i=0;i<Height; i++)
              {
                  for(j=0; j<Width; j++)
                  {
                      if (grid[i][j] == 19 || grid[i][j] == 29)
                          Exg.drawImage(MineImg, j*GRIDSIZE+1, i*GRIDSIZE+1, 20);
                  }
              }
              if (focus)
                  Exg.drawImage(fMineImg, x*GRIDSIZE+1, y*GRIDSIZE+1, 20);

              retval = 1;
          }
          else if (grid[y][x]>=10 && grid[y][x]<20)
          {
              if (focus)
                  Exg.drawImage(fHideImg, x*GRIDSIZE+1, y*GRIDSIZE+1, 20);
              else
                  Exg.drawImage(HideImg, x*GRIDSIZE+1, y*GRIDSIZE+1, 20);
          }
          else
          {
              if (focus)
                  Exg.drawImage(fFlagImg, x*GRIDSIZE+1, y*GRIDSIZE+1, 20);
              else
                  Exg.drawImage(FlagImg, x*GRIDSIZE+1, y*GRIDSIZE+1, 20);
          }

          return retval;    // 返回值:1-画的是地雷;0-不是
      }

      private void Expand(int x, int y)
      {
          int i, j;
          for (i=-1; i<2; i++)
          {
              if (y+i<0) continue;
              if (y+i>=Height) continue;
              for (j=-1; j<2; j++)
              {
                  if (x+j<0) continue;
                  if (x+j>=Width) continue;
                  if (i==0 && j==0) continue;

                  if (grid[y+i][x+j] >= 10 && grid[y+i][x+j] < 20)
                  {
                      grid[y+i][x+j] -= 10;
                      DrawBlock(x+j, y+i, false);
                      if (grid[y+i][x+j] == 0) Expand(x+j, y+i);
                  }
              }
          }
      }

      private boolean SafeExp(int x, int y)
      {
          int i, j, flag = 0;
          for (i=-1; i<2; i++)
          {
              if (y+i<0) continue;
              if (y+i>=Height) continue;
              for (j=-1; j<2; j++)
              {
                  if (x+j<0) continue;
                  if (x+j>=Width) continue;
                  if (i==0 && j==0) continue;
                  if (grid[y+i][x+j] > 20) flag ++;
              }
          }
          if (flag != grid[y][x]) return true;


          boolean retval = true;
          for (i=-1; i<2; i++)
          {
              if (y+i<0) continue;
              if (y+i>=Height) continue;
              for (j=-1; j<2; j++)
              {
                  if (x+j<0) continue;
                  if (x+j>=Width) continue;
                  if (i==0 && j==0) continue;
                  if (grid[y+i][x+j] == 19) // 翻到地雷
                  {
                      grid[y+i][x+j] = 9;
                      DrawBlock(x+j, y+i, true);
                      retval = false;
                  }
                  else if (grid[y+i][x+j] > 20 && grid[y+i][x+j] != 29) // 在标记错误的地方画叉
                  {
                      Exg.drawLine((x+j)*GRIDSIZE+1, (y+i)*GRIDSIZE+1, (x+j+1)*GRIDSIZE-1, (y+i+1)*GRIDSIZE-1);
                      Exg.drawLine((x+j)*GRIDSIZE+1, (y+i+1)*GRIDSIZE-1, (x+j+1)*GRIDSIZE-1, (y+i)*GRIDSIZE+1);
                  }
              }
          }

          if (retval)
          {
              for (i=-1; i<2; i++)
              {
                  if (y+i<0) continue;
                  if (y+i>=Height) continue;
                  for (j=-1; j<2; j++)
                  {
                      if (x+j<0) continue;
                      if (x+j>=Width) continue;
                      if (i==0 && j==0) continue;

                      if (grid[y+i][x+j] >= 10 && grid[y+i][x+j] < 20)
                      {
                          grid[y+i][x+j] -= 10;
                          DrawBlock(x+j, y+i, false);
                          if (grid[y+i][x+j] == 0) Expand(x+j, y+i);
                      }
                  }
              }
          }
          return retval;
      }

      // 游戏音效
      private void SoundPlay(int n)
      {
          if (!Snd) return;
          melody.resetMelody();
          try
          {
              if (n == 0)       //通关
              {
                  melody.setBPM(110);
                  melody.appendNote(MelodyComposer.TONE_C2, MelodyComposer.TONELENGTH_1_16);
                  melody.appendNote(MelodyComposer.TONE_E2, MelodyComposer.TONELENGTH_1_16);
                  melody.appendNote(MelodyComposer.TONE_G2, MelodyComposer.TONELENGTH_1_16);
                  melody.appendNote(MelodyComposer.TONE_E2, MelodyComposer.TONELENGTH_1_16);
                  melody.appendNote(MelodyComposer.TONE_G2, MelodyComposer.TONELENGTH_1_16);
                  melody.appendNote(MelodyComposer.TONE_C3, MelodyComposer.TONELENGTH_1_8);
              }
              else if(n == 1)   // 爆炸
              {
                  melody.setBPM(132);
                  melody.appendNote(MelodyComposer.TONE_D1, MelodyComposer.TONELENGTH_1_64);
                  melody.appendNote(MelodyComposer.TONE_E1, MelodyComposer.TONELENGTH_1_32);
                  melody.appendNote(MelodyComposer.TONE_D1, MelodyComposer.TONELENGTH_1_32);
                  melody.appendNote(MelodyComposer.TONE_C1, MelodyComposer.TONELENGTH_1_64);
                  melody.appendNote(MelodyComposer.TONE_A1, MelodyComposer.TONELENGTH_1_64);
              }
              else              //新游戏
              {
                  melody.setBPM(100);
                  melody.appendNote(MelodyComposer.TONE_G3, MelodyComposer.TONELENGTH_1_16);
                  melody.appendNote(MelodyComposer.TONE_G3, MelodyComposer.TONELENGTH_1_16);
                  melody.appendNote(MelodyComposer.TONE_H3, MelodyComposer.TONELENGTH_1_16);
                  melody.appendNote(MelodyComposer.TONE_G3, MelodyComposer.TONELENGTH_1_8);
              }

              Melody music = melody.getMelody();
              music.play();
              music = null;
          }
          catch (Exception exception){}
      }

  }

⌨️ 快捷键说明

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