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

📄 mangamecanvas.java

📁 本光盘是《J2ME无线移动游戏开发》一书的配套光盘
💻 JAVA
字号:
package ch08;

import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import javax.microedition.lcdui.game.GameCanvas;

public class ManGameCanvas
    extends GameCanvas
    implements Runnable, CommandListener {

  //--- 定义变量 ----------------------------------------------------------------
  private ManMIDlet manMIDlet;
  private Command Cmdstart, Cmdrestart; // 开始、重新开始按键
  private int SWidth, SHeight; // 屏幕宽度和高度
  private int KeyState; // 当前按键状态
  private Graphics ManGraphics; // 定义屏幕的画布
  private TiledLayer TLBackground; //平铺层对象(背景图像)
  private Sprite SpPlane; // 飞机图象(24×24)
  private ManBullets MBbullets; // 子弹图象
  private NumImgTools TimeFont; // 时间文字
  public Sprite SpExplosion; // 飞机爆炸效果图象(32×32)
  private Sprite SpBomb; //炸弹爆炸效果图片(65×65)
  private Image ImgBombIcon; // 炸弹图标
  private NumImgTools BombFont; // 炸弹个数文字
  private int BombCount; // 炸弹个数
  private boolean useBomb; // 是否使用炸弹
  private Thread ThRunGame; // 线程
  private boolean running; // 当前运行状态
  public boolean gameover; // 程序是否已经结束
  private long GameStartTime; // 游戏开始时间
  private int ExplosionFrame; // 飞机爆炸帧数控制累加器
  private int BombFrame; // 炸弹爆炸帧数控制累加器

  //--- 构造函数 ----------------------------------------------------------------
  protected ManGameCanvas(ManMIDlet manMIDlet) {
    // 调用父类的构造方法,必须这样,否则无法编译
    // 当参数为true则无法重载keyPressed回调
    super(false);
    this.manMIDlet = manMIDlet;

    // 添加按键,并开启监听
    addCommand(new Command("退出", Command.EXIT, 1));
    addCommand(Cmdstart = new Command("开始", Command.OK, 1));
    setCommandListener(this);

    SWidth = getWidth(); // 获得设备屏幕宽度
    SHeight = getHeight(); // 获得设备屏幕高度
    running = false; // 当前运行状态
    ThRunGame = null; // 线程初始化

    ManGraphics = getGraphics(); // 获得当前屏幕的画布

    //--- 初始化背景图象 ---
    Image img = ImageTools.getImage("/pic/back_water.png");
    int backcols = SWidth / img.getWidth() + 1; // 计算横向需要多少图片
    int backrows = SHeight / img.getHeight() + 1; // 计算纵向需要多少图片
    // 初始化平铺层对象,将图象平铺显示
    TLBackground = new TiledLayer(backcols, backrows, img, img.getWidth(),
                                  img.getHeight());
    // 由于背景图是由一个图片构成的,所以每个单元的索引均为1。
    //背景若由若干个图片构成,则这些中第一个图片的索引为1,其它按顺序递增。
    for (int i = 0; i < backcols * backrows; i++) {
      TLBackground.setCell(i % backcols, i / backcols, 1);

      //--- 初始化小飞机图象 ---
    }
    img = ImageTools.getImage("/pic/MyPlaneFrames.png");
    SpPlane = new Sprite(img, 24, 24);

    //--- 初始化记时数字图象 ---
    img = ImageTools.getImage("/pic/b_number.png");
    TimeFont = new NumImgTools(ManGraphics, img, 10, 15);

    //--- 初始化子弹图象 ---
    img = ImageTools.getImage("/pic/bullet.png");
    MBbullets = new ManBullets(this, img, SWidth, SHeight);

    //--- 初始化飞机爆炸效果图片 ---
    img = ImageTools.getImage("/pic/explosion.png");
    SpExplosion = new Sprite(img, 32, 32);

    //--- 初始化炸弹爆炸效果图片 ---
    img = ImageTools.getImage("/pic/bomb.png");
    SpBomb = new Sprite(img, 65, 65);

    //--- 初始化炸弹图标 ---
    ImgBombIcon = ImageTools.getImage("/pic/bomb_icon.png");

    //--- 初始化记时数字图象 ---
    img = ImageTools.getImage("/pic/s_number.png");
    BombFont = new NumImgTools(ManGraphics, img, 5, 7);
  }

  //--- 线程主运行 --------------------------------------------------------------
  public void run() {
    long st = 0, et = 0, diff = 0;
    int GameTime = 0, TempTime = 0;
    int rate = 50; // 16-17 帧/每妙
    while (running) {
      st = System.currentTimeMillis();

      ManGraphics.setColor(0, 0, 0); // 清空屏幕
      ManGraphics.fillRect(0, 0, SWidth, SHeight);

      TLBackground.paint(ManGraphics); // 画出背景图象

      if (!gameover) {
        SpPlane.paint(ManGraphics); // 画出飞机图象
        MBbullets.paint(ManGraphics, SpPlane.getX(), SpPlane.getY()); // 画出子弹
        GameKeyInput(); // 游戏操作
        // 获得游戏时间
        TempTime = (int) (System.currentTimeMillis() - GameStartTime) / 1000;
        if (GameTime != TempTime) {
          GameTime = TempTime;
          // 每隔20秒奖励一个炸弹
          if (GameTime % 20 == 0 && GameTime != 0) {
            ++BombCount;
          }
        }
        if (GameTime == 99) { // 若游戏时间达到99秒,强行退出游戏
          ExplosionFrame = 3;
          gameover = true;
        }
        if (useBomb) { // 若使用炸弹,显示爆炸效果
          if (BombFrame < 6) {
            SpBomb.setFrame(++BombFrame);
            SpBomb.paint(ManGraphics);
            MBbullets.BombBullets(SpPlane.getX(), SpPlane.getY());
          }
          else {
            useBomb = false;
          }
        }
      }
      // 如果游戏已经结束
      else {
        if (ExplosionFrame < 3) { // 显示爆炸效果
          SpExplosion.setFrame(++ExplosionFrame);
          SpExplosion.paint(ManGraphics);
        }
        else { // 显示评语
          String GameOverStr;
          if (GameTime < 10) {
            GameOverStr = "你是在用脚玩吗?";
          }
          else if (GameTime < 16) {
            GameOverStr = "再来一次,你能做到!";
          }
          else if (GameTime < 20) {
            GameOverStr = "同情你,再来一次!";
          }
          else if (GameTime < 25) {
            GameOverStr = "非常好,你是一个真正的男人!";
          }
          else if (GameTime < 30) {
            GameOverStr = "你是玩这个游戏的天才!";
          }
          else if (GameTime < 40) {
            GameOverStr = "真不敢相信,这个游戏难道是你做的?";
          }
          else if (GameTime < 99) {
            GameOverStr = "噢,我的上帝!你是人类吗?";
          }
          else {
            GameOverStr = "我很遗憾,你可以不用再完这个游戏了!";

          }
          ManGraphics.setColor(255, 255, 255);
          ManGraphics.drawString(GameOverStr, 5, 25,
                                 ManGraphics.LEFT | ManGraphics.TOP);
        }
      }
      // 画出记时文字
      TimeFont.drawNums(GameTime, (SWidth - 14 * 2) / 2, 10, 2);
      // 画出炸弹图标
      ManGraphics.drawImage(ImgBombIcon, 0, SHeight - 1,
                            ManGraphics.BOTTOM | ManGraphics.LEFT);
      // 画出炸弹个数文字
      BombFont.drawNum(BombCount, ImgBombIcon.getWidth() + 1, SHeight - 8);

      flushGraphics(); // 刷新图象显示,没这句不显示图片

      et = System.currentTimeMillis();
      // 不满足帧数要求,暂停线程至要求帧数
      diff = et - st;
      if (diff < rate) {
        try {
          Thread.sleep(rate - diff);
        }
        catch (InterruptedException ex) {
          System.out.println(ex);
        }
      }
    }
  }

  //--- 监听按键 ----------------------------------------------------------------
  public void commandAction(Command command, Displayable displayable) {
    // 退出操作
    if (command.getCommandType() == Command.EXIT) {
      if (running) { // 结束线程
        running = false;
      }
      manMIDlet.quitApp();
    }
    // 开始操作
    else if (command == Cmdstart) {
      if (!running) {
        running = true;
        GameInit(); // 游戏初始化
        ThRunGame = new Thread(this); // 初始化线程
        ThRunGame.start();
      }
      removeCommand(Cmdstart);
      addCommand(Cmdrestart = new Command("重新开始", Command.OK, 1));
    }
    // 重新开始操作
    else if (command == Cmdrestart) {
      GameInit();
    }
  }

  //--- 游戏初始化 --------------------------------------------------------------
  private void GameInit() {
    gameover = false;
    // 初始化飞机图象,取第一副图片
    SpPlane.setFrame(0);
    //将飞机置于屏幕中心
    SpPlane.setPosition( (SWidth - SpPlane.getWidth()) / 2,
                        (SHeight - SpPlane.getHeight()) / 2);

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

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

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

    BombCount = 3; // 初始化有3个炸弹

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

  //--- 游戏操作 ----------------------------------------------------------------
  private void GameKeyInput() {
    SpPlane.setFrame(0);
    KeyState = getKeyStates(); // 获得当前按键状态
    // 根据按键状态,飞机图象做相对移动,并判断是否超出屏幕边界
    if ( (KeyState & UP_PRESSED) != 0) { // 上
      if (SpPlane.getY() - 3 < 0) {
        SpPlane.move(0, 0 - SpPlane.getY()); //出边界的处理
      }
      else {
        SpPlane.move(0, -3); //正常处理
      }
      SpPlane.setFrame(0);
    }
    if ( (KeyState & DOWN_PRESSED) != 0) { // 下
      if (SpPlane.getY() + 3 + SpPlane.getHeight() > SHeight) {
        SpPlane.move(0, SHeight - SpPlane.getY() - SpPlane.getHeight());
      }
      else {
        SpPlane.move(0, 3);
      }
      SpPlane.setFrame(0);
    }
    if ( (KeyState & LEFT_PRESSED) != 0) { // 左
      if (SpPlane.getX() - 3 < 0) {
        SpPlane.move(0 - SpPlane.getX(), 0);
      }
      else {
        SpPlane.move( -3, 0);
      }
      SpPlane.setFrame(1);
    }
    if ( (KeyState & RIGHT_PRESSED) != 0) { // 右
      if (SpPlane.getX() + 3 + SpPlane.getWidth() > SWidth) {
        SpPlane.move(SWidth - SpPlane.getX() - SpPlane.getWidth(), 0);
      }
      else {
        SpPlane.move(3, 0);
      }
      SpPlane.setFrame(2);
    }
  }

  //--- [重载]捕获按键事件,此方法无法捕获多个按键按下 -------------------------------
  protected void keyPressed(int key) {
    if (key == 20 || key == 53) {
      if (running && !gameover) {
        if (!useBomb && BombCount > 0) {
          BombCount--;
          //X、Y坐标值均少20,因为炸弹65宽、飞机24宽
          SpBomb.setPosition(SpPlane.getX() - 20, SpPlane.getY() - 20);
          useBomb = true;
          BombFrame = -1; // 初始化炸弹爆炸帧数控制累加器
        }
      }
    }
  }

}

⌨️ 快捷键说明

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