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

📄 mangamecanvas.java

📁 该文件主要是为了给要快速度的 了解 java 的 0基础的 人 给予 成功的 快感... 也是 我 对 本站的支持
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
  //--- 监听按键 ----------------------------------------------------------------
  public void commandAction(Command command, Displayable displayable) {
    KeyState = getKeyStates();
    // 退出操作
    if (command.getCommandType() == Command.EXIT) {
      if (running) { // 结束线程
        running = false;
      }
      manMIDlet.quitApp();
    }
    // 开始操作
    else if (command == Cmdstart) {
      if (!running) {
        running = true;
        initrecord(gamelevel);
        GameInit(); // 游戏初始化
        ThRunGame = new Thread(this); // 初始化线程
        ThRunGame.start();
      }
      removeCommand(Cmdstart);
      addCommand(Cmdrestart = new Command("重新开始", Command.ITEM, 1));
    }

    // 重新开始操作
    else if (command == Cmdrestart) {
      if (gamelevelup) {
        gamelevel++;
        gamelevelup = false;
      }
      setGameLevel(gamelevel);
      GameInit();
    }
  }

  //--- 游戏初始化 --------------------------------------------------------------
  private void GameInit() {
    gameover = false;
    // 初始化飞机图象
    SpPlane.setFrame(0);
    SpPlane.setPosition( (SWidth - SpPlane.getWidth()) / 2,
                        (SHeight - SpPlane.getHeight()) / 2);

    MBbullets.setSpeed(gamelevel); //初始化子弹速度

    GameStartTime = System.currentTimeMillis(); // 重新获得起始时间

    ExplosionFrame = -1; // 初始化飞机爆炸帧数控制累加器

    MBbullets.InitBulletsLocus(); // 初始化子弹起始坐标及速度

    useBomb = false; // 是否使用炸弹

    danger = 0; // 初始化危险度

    MBbullets.setPower(0); // 初始化能量值

    System.out.println("gamelevel=" + gamelevel);
    System.out.println("hightscore1=" + hightscore);
    getrecord(gamelevel); // 初始化数据库
    System.out.println("hightscore2=" + hightscore);
  }

  //--- 游戏操作 ----------------------------------------------------------------
  private void GameKeyInput() {
    SpPlane.setFrame(0);

    KeyState = getKeyStates(); // 获得当前按键状态

    // 根据按键状态,飞机图象做相对移动,并判断是否超出屏幕边界
    if ( (KeyState & UP_PRESSED) != 0) { // 上
      if (SpPlane.getY() - vplane < 0) {
        SpPlane.move(0, 0 - SpPlane.getY());
      }
      else {
        SpPlane.move(0, -vplane);
      }
      SpPlane.setFrame(0);
    }
    if ( (KeyState & DOWN_PRESSED) != 0) { // 下
      if (SpPlane.getY() + vplane + SpPlane.getHeight() > SHeight) {
        SpPlane.move(0, SHeight - SpPlane.getY() - SpPlane.getHeight());
      }
      else {
        SpPlane.move(0, vplane);
      }
      SpPlane.setFrame(0);
    }
    if ( (KeyState & LEFT_PRESSED) != 0) { // 左
      if (SpPlane.getX() - vplane < 0) {
        SpPlane.move(0 - SpPlane.getX(), 0);
      }
      else {
        SpPlane.move( -vplane, 0);
      }
      SpPlane.setFrame(1);
    }
    if ( (KeyState & RIGHT_PRESSED) != 0) { // 右
      if (SpPlane.getX() + vplane + SpPlane.getWidth() > SWidth) {
        SpPlane.move(SWidth - SpPlane.getX() - SpPlane.getWidth(), 0);
      }
      else {
        SpPlane.move(vplane, 0);
      }
      SpPlane.setFrame(2);
    }
  }

  //--- [重载]捕获按键事件,此方法无法捕获多个按键按下 -------------------------------
  protected void keyPressed(int key) {
    System.out.println(key);
    if (key == 20 || key == 53) {
      if (running && !gameover) {
        if (!useBomb && BombCount > 0) {
          BombCount--; //炸弹数减1
          SpBomb.setPosition(SpPlane.getX() - 20, SpPlane.getY() - 20);
          useBomb = true;
          BombFrame = -1; // 初始化炸弹爆炸帧数控制累加器
        }
      }
    }

    // 设置飞机速度提高
    if (key == 35) {
      if (vplane < 6) {
        setVPlane(++vplane);
      }
    }

    // 设置飞机速度降低
    if (key == 42) {
      if (vplane > 2) {
        setVPlane(--vplane);
      }
    }
  }

  //--- 自动爆炸 ----------------------------------------------------------------
  protected void autoBomb() {
    if (running && !gameover) {
      SpBomb.setPosition(SpPlane.getX() - 20, SpPlane.getY() - 20);
      useBomb = true;
      BombFrame = -1; // 初始化炸弹爆炸帧数控制累加器
    }
  }

  //--- 设置游戏难度 ------------------------------------------------------------
  public void setGameLevel(int gamelevel) {
    this.gamelevel = gamelevel;
    if (gamelevel == 1) { // 难度1
      BombCount = 6;
      BombOnOff = true;
    }
    if (gamelevel == 2) { // 难度2
      BombCount = 3;
      BombOnOff = true;
    }
    if (gamelevel == 3) { // 难度3
      BombCount = 0;
      BombOnOff = false;
    }
  }

  //--- 初始化数据库 ------------------------------------------------------------
  public void initrecord(int i) {
    boolean flag = true;
    try {
      rs = RecordStore.openRecordStore("hightscore", false); // 打开数据库
      System.out.println("initrecord=="+"open");
    }
    catch (Exception ex) {
      flag = false;
      ex.printStackTrace();
    }
    if (flag) {
      try {
        byte abyte0[] = new byte[rs.getRecordSize(i)]; // 读取数据
        abyte0 = rs.getRecord(i); // 读取数据
        String s = new String(abyte0);
        System.out.println("s=" + s);

        System.out.println("hightscore111=" + hightscore);
        hightscore = Integer.parseInt(s);
        System.out.println("hightscore222=" + hightscore);
      }
      catch (Exception ex) {
        ex.printStackTrace();
      }
    }
    else {
      try {
        rs = RecordStore.openRecordStore("hightscore", true); // 打开数据库
      }
      catch (Exception ex) {
        System.out.println("initrecord=="+i);
        ex.printStackTrace();
      }
      try {
        byte abyte1[] = null; // 初始化数据
        abyte1 = Integer.toString(hightscore).getBytes();
        System.out.println("abyte1"+abyte1);
        rs.addRecord(abyte1, 0, abyte1.length); // 添加到数据库
      }
      catch (Exception ex) {
        ex.printStackTrace();
      }
    }
    try {
      rs.closeRecordStore(); // 关闭数据库
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  //--- 得到游戏记录 ------------------------------------------------------------
  public void getrecord(int i) {
    try {
      rs = RecordStore.openRecordStore("hightscore", false); // 打开数据库
    }
    catch (Exception ex) {}
    try {
      byte abyte0[] = new byte[rs.getRecordSize(i)]; // 读取数据
      abyte0 = rs.getRecord(i); // 读取数据编号
      String s = new String(abyte0);
      hightscore = Integer.parseInt(s); // 得到数据
    }
    catch (Exception ex) {}
    try {
      rs.closeRecordStore(); // 关闭数据库
    }
    catch (Exception ex) {}
  }

  //--- 设置游戏记录 -------------------------------------------------------------
  public void setrecord(int i) {
    try {
      rs = RecordStore.openRecordStore("hightscore", false); // 打开数据库
    }
    catch (Exception ex) {}
    try {
      byte abyte0[] = null; // 初始化数据
      abyte0 = Integer.toString(hightscore).getBytes(); // 取得数据
      rs.setRecord(i, abyte0, 0, abyte0.length); // 修改数据
    }
    catch (Exception ex) {}
    try {
      rs.closeRecordStore(); // 关闭数据库
    }
    catch (Exception ex) {}
  }

  //--- 重置游戏记录 -------------------------------------------------------------
  public void resetrecord(int i) {
    try {
      rs = RecordStore.openRecordStore("hightscore", false); // 打开数据库
    }
    catch (Exception ex) {}
    try {
      byte abyte0[] = null; // 初始化数据
      abyte0 = Integer.toString(0).getBytes(); // 取得数据
      rs.setRecord(i, abyte0, 0, abyte0.length); //修改数据
    }
    catch (Exception ex) {}
    try {
      rs.closeRecordStore(); // 关闭数据库
    }
    catch (Exception ex) {}
  }

  //--- 检查游戏得分 -------------------------------------------------------------
  public void checkrecord() {
    getrecord(gamelevel); // 取得游戏记录
    if (hightscore < (danger + 1) * gamelevel * GameTime * BombHortation) { // 判断游戏数据和数据库数据大小
      hightscore = (danger + 1) * gamelevel * GameTime * BombHortation;
      setrecord(gamelevel); // 修改游戏记录
    }
  }

  //--- 取得游戏状态 -------------------------------------------------------------
  public boolean getGameInfo() {
    return gameover;
  }

  //--- 设置游戏状态 -------------------------------------------------------------
  public void setGameInfo(boolean gameover) {
    this.gameover = gameover;
  }

  //--- 设置游戏飞机爆炸 ---------------------------------------------------------
  public Sprite getSpExplosion() {
    return SpExplosion;
  }

  //--- 设置飞机速度 ------------------------------------------------------------
  public void setVPlane(int vplane) {
    this.vplane = vplane;
  }
}

⌨️ 快捷键说明

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