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

📄 mancanvas.java

📁 是男人就撑20秒游戏j2me版
💻 JAVA
字号:
/*
 * GameScreen.java
 *
 * Created on 2006年11月23日, 下午6:24
 *
 * 游戏主类 -- 负责接收用户输入、逻辑处理、绘制屏幕、游戏结束的退出
 */

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

public class ManCanvas extends GameCanvas implements Runnable{
    private ManMidlet midlet;           //主类
    private int SWidth;                 //屏幕尺寸
    private int SHeight;
    private Graphics g;
    private volatile Thread animationThread = null; //游戏运行动画线程
    private long startTime;             //游戏开始时间
    private long gameDuration = 0;      //游戏运行时间
    private long start;                 //用于刷屏临时时间纪录
    private int score;                  //纪录分数
    private static final int MILLIS_PER_TICK = 50;//每次循环预计耗费的时间 16-17 帧/秒
    private int keyState;               //纪录键盘状态
    private TiledLayer background;      //平铺层对象(背景图像)
    private Sprite SpPlane;                  //飞机图象(24×24)
    private NumImgTools TimeFont;       // 时间文字
    private Image ImgBombIcon;               // 炸弹图标
    private NumImgTools BombFont;            // 炸弹个数文字
    private int BombCount;                   // 炸弹个数
    private boolean useBomb;                // 是否使用炸弹
    private Sprite SpBomb;                   //炸弹爆炸效果图片(65×65)
    private int BombFrame;                   // 炸弹爆炸帧数控制累加器
    private Bullets bullets;                 // 子弹图象
    public boolean isGameOver;              //游戏结束标志
    int GameTime,TempTime;                   //用于奖励炸弹个数计时

  protected ManCanvas(ManMidlet midlet) {
    super(false);                       // 当参数为true则无法重载keyPressed回调
    this.midlet = midlet;
    SWidth = getWidth();
    SHeight = getHeight();
    load();
  }

  /** 装载游戏所需要的资源 */
  private void load(){
      Image img = ImageTools.createImage("/background.png"); //初始化背景图象
      int backcols = SWidth / img.getWidth() + 1;
      int backrows = SHeight / img.getHeight() + 1;
      background = new TiledLayer(backcols, backrows, img, img.getWidth(), img.getHeight());
      for (int i = 0; i < backcols * backrows; i++) {
          background.setCell(i % backcols, i / backcols, 1);
      }

      img = ImageTools.createImage("/plane.png");           //初始化飞机图象
      SpPlane = new Sprite(img, 24, 24);
      img = ImageTools.createImage("/b_number.png");        //初始化计时数字图象
      TimeFont = new NumImgTools(getGraphics(), img, 10, 15);
      ImgBombIcon = ImageTools.createImage("/bomb_icon.png");//初始化炸弹图标
      img = ImageTools.createImage("/s_number.png");         //初始化炸弹计数图象
      BombFont = new NumImgTools(getGraphics(), img, 5, 7);
      img = ImageTools.createImage("/bomb.png");             //初始化炸弹爆炸效果图片
      SpBomb = new Sprite(img, 65, 65);
      img = ImageTools.createImage("/bullet.png");          //初始化子弹图象
      bullets = new Bullets(this, img, SWidth, SHeight);
  }

  /**  初始化游戏数据 */
  public void init(){
      GameTime = 0;                                             //奖励炸弹个数计时清零
      TempTime = 0;
      gameDuration = 0;                                         //清空游戏持续时间
      isGameOver = false;                                       //游戏结束标志位
      SpPlane.setFrame(0);                                     //设置飞机正常形状
      SpPlane.setPosition( (SWidth - SpPlane.getWidth()) / 2, //将飞机置于屏幕中心
                        (SHeight - SpPlane.getHeight()) / 2);
      bullets.InitBulletsLocus();                             //初始起始坐标及速度
      BombCount = 1;                                            // 初始化炸弹个数
      useBomb = false;                                         // 是否使用炸弹
      startTime = System.currentTimeMillis();                 // 获得起始时间
  }

  /**  启动游戏 */
  public synchronized void start(){
      animationThread = new Thread(this);                     //创建游戏主线程
      animationThread.start();                                 //启动游戏
      startTime = System.currentTimeMillis()-gameDuration;    //纪录开始时间
  }

  /**  暂停游戏*/
  public synchronized void stop(){
      gameDuration = System.currentTimeMillis()-startTime;      //纪录游戏持续时间
      animationThread = null;
  }

  /**  游戏运行中.....  */
  public void run(){
      Thread currentThread = Thread.currentThread();
      g = getGraphics();
      try{
          while(currentThread == animationThread){
              start = System.currentTimeMillis();
              input();
              tick();
              render();
              long timeTaken = System.currentTimeMillis()-start;
              if(timeTaken < MILLIS_PER_TICK){
                  synchronized(this){
                      wait(MILLIS_PER_TICK-timeTaken);
                  }
              }else{
                  currentThread.yield();
              }
          }
      }catch(InterruptedException e){}
  }

  /**  获取用户输入  */
  private void input(){
      keyState = getKeyStates();
  }

  /**  游戏逻辑运算 */
  private void tick(){
      if(isGameOver){                               //游戏结束
          gameDuration = System.currentTimeMillis()-startTime;
          midlet.GameCanvasGameOver((int)gameDuration, (int)gameDuration/1000 + BombCount);
      }
      SpPlane.setFrame(0);                         //默认的飞机形状
      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);
      }
      if((keyState & FIRE_PRESSED) != 0){           // 使用炮弹
          if (!useBomb && BombCount > 0) {
              BombCount--;
              SpBomb.setPosition(SpPlane.getX() - 20, SpPlane.getY() - 20);
              useBomb = true;
              BombFrame = -1;                       // 初始化炸弹爆炸帧数控制累加器
          }
      }
      // 奖励炮弹算法
      TempTime = (int) (System.currentTimeMillis() - startTime) / 1000;
      if (GameTime != TempTime) {
          GameTime = TempTime;
          if (GameTime % 10 == 0 && GameTime != 0){ // 每10秒奖励一个炸弹
              ++BombCount;
          }
      }
  }

  /**  显示游戏画布内容  */
  public void render(){
      g.setColor(0, 0, 0);                              // 清空屏幕
      g.fillRect(0, 0, SWidth, SHeight);
      background.paint(g);                              // 画出背景图象
      SpPlane.paint(g);                                 // 画出飞机图象
      bullets.paint(g, SpPlane.getX(), SpPlane.getY()); // 画出子弹
      TimeFont.drawNums((int)(System.currentTimeMillis()-startTime)/1000,(SWidth - 14 * 2) / 2, 10, 2);             // 画出记时文字
      g.drawImage(ImgBombIcon, 0, SHeight - 1,g.BOTTOM | g.LEFT);                       // 画出炸弹图标
      BombFont.drawNum(BombCount,ImgBombIcon.getWidth() + 1, SHeight - 8);// 画出炸弹个数文字
      if (useBomb) {                                    // 若使用炸弹,显示爆炸效果
          if (BombFrame < 6) {
              SpBomb.setFrame(++BombFrame);
              SpBomb.paint(g);
              bullets.BombBullets(SpPlane.getX(), SpPlane.getY());  //消除被炸掉的子弹
          }else {
              useBomb = false;  //爆炸播放完毕
          }
      }
      flushGraphics();
  }

  /** 输入非游戏按键-游戏暂停  */
  protected void keyPressed(int keyCode) {
  	/*if(keyCode < 0){
        stop();
        midlet.GameCanvasMenu();
    }*/
  }
}

⌨️ 快捷键说明

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