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

📄 foxcanvas.java

📁 一款java游戏,可以学习参考,类似"是男人就下一百层".
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
//        }
//        int start = 0, end = 0;
//        switch (levelIndex + 1) {
//          case 1:
//            start = 0;
//            end = 4;
//            break;
//          case 2:
//            start = 5;
//            end = 9;
//            break;
//          case 3:
//            start = 10;
//            end = 14;
//            break;
//        }
//        numberList[end] = role.number;
//        nameList[end] = inputName;
//        for (int i = end; i >= start + 1; i--) {
//          if (i - 1 >= 0) {
//            if (numberList[i] > numberList[i - 1]) {
//              int intTemp = numberList[i - 1];
//              String strTemp = nameList[i - 1];
//              numberList[i - 1] = numberList[i];
//              nameList[i - 1] = nameList[i];
//              numberList[i] = intTemp;
//              nameList[i] = strTemp;
//            }
//          }
//        }
//        writeDB();
//        gameState = ST_MAINMENU;
//        Display.setCurrent(canvas);
//        repaint();
//      }
//      if (c == btCancel) {
//        gameState = ST_MAINMENU;
//        Display.setCurrent(canvas);
//        repaint();
//      }
//    }
//  }

  public void processEvent(int type, int keyCode) {
    if (type == Display.KEY_PRESSED_EVENT) {
      iButton = 0;
      switch (gameState) {
        case ST_MAINMENU:
          switch (keyCode) {
            case KEY_UP:
              mainMenuIndex = (mainMenuIndex + 4) % 5;
              break;
            case KEY_DOWN:
              mainMenuIndex = (mainMenuIndex + 1) % 5;
              break;
            case KEY_LS:
            case KEY_MID:
              iButton = 0;
              switch (mainMenuIndex) {
                case 0: //新游戏
                  mainMenuIndex = 0;
                  levelIndex = 0;
                  gameState = ST_GAMELEVEL;
                  break;
                case 1: //设置效果
                  gameState = ST_SETTING;
                  break;
                case 2: //帮助
                  gameState = ST_HELP;
                  break;
                case 3: //排行榜
                  gameState = ST_SCORELEVEL;
                  break;
                case 4: //退出游戏
                  midlet.terminate();
                  break;
              }
              break;
          }
          break;
        case ST_GAMEMENU:
          switch (keyCode) {
            case KEY_UP:
              gameMenuIndex = (gameMenuIndex + 4) % 5;
              break;
            case KEY_DOWN:
              gameMenuIndex = (gameMenuIndex + 1) % 5;
              break;
            case KEY_LS:
            case KEY_MID:
              iButton = 0;
              switch (gameMenuIndex) {
                case 0: //继续游戏
                  gameState = GAME_PLAY;
                  setSoftLabel(SOFT_KEY_2, "\uFF92\uFF86\uFF6D\uFF70");
                  setSoftLabel(SOFT_KEY_1, "拞抐");
                  break;
                case 1: //设置
                  gameState = ST_SETTING;
                  break;
                case 2: //帮助
                  gameState = ST_HELP;
                  break;
                case 3: //主菜单,一旦返回主菜单就不可能回到游戏菜单继续游戏,所以处处应该将gamastate2设置为0
                  bPlay = false;
                  gameMenuIndex = 0;
                  gameState = ST_MAINMENU;
                  break;
                case 4:
                  midlet.terminate();
                  break;
              }
              break;
          }
          break;
        case ST_SETTING:
          switch (keyCode) {
            case KEY_UP:
            case KEY_DOWN:
              soundIndex = (soundIndex + 1) % 2;
              break;
            case KEY_LS:
            case KEY_MID:
              iButton = 0;
              if (soundIndex == 0) {
                bSound = true;
              }
              if (soundIndex == 1) {
                bSound = false;
              }
              if (!bPlay) {
                gameState = ST_MAINMENU;
              }
              else {
                gameState = ST_GAMEMENU;
              }
              break;
            case KEY_RS:
              if (bSound) {
                soundIndex = 0;
              }
              else {
                soundIndex = 1;
              }
              if (!bPlay) {
                gameState = ST_MAINMENU;
              }
              else {
                gameState = ST_GAMEMENU;
              }
              break;
          }
          break;
        case ST_ABOUT:
          if (keyCode == KEY_RS) {
            gameState = ST_MAINMENU;
          }
          break;
        case ST_HELP:
          switch (keyCode) {
            case KEY_UP:
            case KEY_DOWN:
              helpIndex = (helpIndex + 1) % 2;
              break;
            case KEY_LS:
            case KEY_MID:
              iButton = 0;
              switch (helpIndex) {
                case 0:
                  gameState = ST_GAMEINFO;
                  break;
                case 1:
                  gameState = ST_KEYINFO;
                  break;
              }
              break;
            case KEY_RS:
              if (!bPlay) {
                gameState = ST_MAINMENU;
              }
              else {
                gameState = ST_GAMEMENU;
              }
              helpIndex = 0;
              break;
          }
          break;
        case ST_KEYINFO:
          if (keyCode == KEY_RS) {
            gameState = ST_HELP;
          }
          break;
        case ST_GAMEINFO:
          if (keyCode == KEY_RS) {
            gameState = ST_HELP;
            GameInfoPage = 0;
          }
          else
          if (keyCode == KEY_UP || keyCode == KEY_DOWN) {
            GameInfoPage = (GameInfoPage + 1) % 2;
          }
          break;
        case ST_SCORELEVEL:
          switch (keyCode) {
            case KEY_UP:
              levelListIndex = (levelListIndex + 2) % 3;
              break;
            case KEY_DOWN:
              levelListIndex = (levelListIndex + 1) % 3;
              break;
            case KEY_LS:
            case KEY_MID:
              iButton = 0;
              readDB();
              gameState = ST_SCORE;
              break;
            case KEY_RS:
              levelListIndex = 0;
              gameState = ST_MAINMENU;
              break;
          }
          break;
        case ST_GAMELEVEL:
          switch (keyCode) {
            case KEY_DOWN:
              levelIndex = (levelIndex + 1) % 3;
              break;
            case KEY_UP:
              levelIndex = (levelIndex + 2) % 3;
              break;
            case KEY_LS:
            case KEY_MID:
              switch (levelIndex) {
                case 0: //easy
                  boardSpeed = 3;
                  break;
                case 1: //normal
                  boardSpeed = 4;
                  break;
                case 2: //hard
                  boardSpeed = 5;
                  break;
              } //难度设置后直接开始游戏
              role = new FoxRole(this); //构造狐狸
              initBoards(); //初始化狐狸及木板
              role.x = boards[4][0] + 11;
              role.y = boards[4][1] - 24;
              role.number = 0;
              role.life = 12;
              gameState = ST_RUN;
              setSoftLabel(SOFT_KEY_2, "");
              setSoftLabel(SOFT_KEY_1, "");
              break;
            case KEY_RS:
              gameState = ST_MAINMENU;
              break;
          }
          break;
        case ST_SCORE:
          if (keyCode == KEY_RS) {
            gameState = ST_SCORELEVEL;
          }
          break;
        case GAME_PLAY: //开始游戏
          switch (keyCode) {
            case KEY_LEFT:
            case 4:
              role.keyTurn = true;
              role.direct = 0;
              break;
            case KEY_RIGHT:
            case 6:
              role.keyTurn = true;
              role.direct = 1;
              break;
            case KEY_LS:
              try {
               role.sndPlayers[role.iSoundKind].stop();
             }
             catch (Exception e) {
             }
              gameState = ST_GAMEMENU;
              break;
            case KEY_RS:
              iButton = 0;
              try {
                role.sndPlayers[role.iSoundKind].stop();
              }
              catch (Exception e) {
              }
              setSoftLabel(SOFT_KEY_2, "");
              setSoftLabel(SOFT_KEY_1, "栠傞");
              gameState = GAME_PAUSE;
              break;
          }
          break;
        case GAME_PAUSE: //暂停
          switch (keyCode) {
            case KEY_RS:
              iButton = 0;
              setSoftLabel(SOFT_KEY_2, "\uFF92\uFF86\uFF6D\uFF70");
              setSoftLabel(SOFT_KEY_1, "拞抐");
              gameState = GAME_PLAY;
              break;
          }
          break;
        case GAME_OVER_RESULT:
          switch (keyCode) {
            case KEY_LS:
              if (bSaveScore) {
                int start = 0, end = 0;
                switch (levelIndex + 1) {
                  case 1:
                    start = 0;
                    end = 4;
                    break;
                  case 2:
                    start = 5;
                    end = 9;
                    break;
                  case 3:
                    start = 10;
                    end = 14;
                    break;
                }
                numberList[end] = role.number;
//        nameList[end] = inputName;
                for (int i = end; i >= start + 1; i--) {
                  if (i - 1 >= 0) {
                    if (numberList[i] > numberList[i - 1]) {
                      int intTemp = numberList[i - 1];
//              String strTemp = nameList[i - 1];
                      numberList[i - 1] = numberList[i];
//              nameList[i - 1] = nameList[i];
                      numberList[i] = intTemp;
//              nameList[i] = strTemp;
                    }
                  }
                }
                writeDB();
                gameState = ST_MAINMENU;
                bSaveScore = false;
                repaint();
              }
              break;
            case KEY_RS:
              gameState = ST_MAINMENU;
              bSaveScore = false;
              break;
          }
          break;
      }
    }
    else
    if (type == Display.KEY_RELEASED_EVENT) {
      if (gameState == GAME_PLAY) {
        role.keyTurn = false;
      }
    }
  }

  /*绘制剪切区域的图片
   *img被剪切的图片
   *int x,inty 要显示的x y坐标
   *int clipX,int clipY被剪切的小图片的x y在源图片中的坐标
   *int clipW,int clipH被剪切的小图片的宽和高
   */
  public static void drawClipImage(Graphics g, Image img, int x, int y,
                                   int clipX,
                                   int clipY, int clipW, int clipH) {
    g.setClip(x, y, clipW, clipH);
    g.drawImage(img, x - clipX, y - clipY);
    g.setClip(0, 0, WIDTH, HEIGHT);
  }

//**************************************************************************开始写存取数据并读写
//   static String nameList[] = new String[15];
   static int numberList[] = new int[15];

//初始化数据库
  private void InitDB() {
    try {
      InputStream is = Connector.openInputStream("scratchpad:///0");
      DataInputStream dis = new DataInputStream(is);
      int iSuccess = dis.readInt();
      dis.close();
      is.close();
      dis = null;
      is = null;
      if (iSuccess != 1) {
        OutputStream os = Connector.openOutputStream("scratchpad:///0");
        DataOutputStream dos = new DataOutputStream(os);
        dos.writeInt(1);
        for (int i = 0; i < 15; i++) {
//          dos.writeUTF("Bbmf");
          dos.writeInt(0);
        }
        dos.flush();
        dos.close();
        os.close();
        dos = null;
        os = null;
      }
    }
    catch (IOException e) {}
  }

  /**
   * writeDB 写数据库
   */
  private void writeDB() {
    try {
      OutputStream os = Connector.openOutputStream("scratchpad:///0;pos=4");
      DataOutputStream dos = new DataOutputStream(os);
      for (int i = 0; i < 15; i++) {
//        dos.writeUTF(nameList[i]);
        dos.writeInt(numberList[i]);
      }
      dos.close();
      os.close();
      dos = null;
      os = null;
    }
    catch (IOException e) {
      e.printStackTrace();
    }
  }

  /**
   * readDB 读数据库
   */
  private void readDB() {
    try {
      InputStream is = Connector.openInputStream("scratchpad:///0;pos=4");
      DataInputStream dis = new DataInputStream(is);
      for (int i = 0; i < 15; i++) {
//        nameList[i] = dis.readUTF();
        numberList[i] = dis.readInt();
      }
      dis.close();
      is.close();
      dis = null;
      is = null;
    }
    catch (IOException e) {
      e.printStackTrace();
    }
  }
}

⌨️ 快捷键说明

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