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

📄 battlecanvas.java

📁 这是我在原来单位的时候写的一个坦克大战的J2ME游戏的代码,可以给其他作J2ME游戏的人作个参考
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
      }
    
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  private void createMenuButton() {
    try {
      m_menuButtonImg=null;
      m_menuButtonImg = Image.createImage("/tank/map_res/menu_button.png");
      m_menuWordImg=null;
      m_menuWordImg = Image.createImage("/tank/map_res/menu_word.png");
      //MENU BUTTON
      m_menuButtonImgSprite = new Sprite[MENU_BUTTON_COUNTS];
      for (int i = 0; i < MENU_BUTTON_COUNTS; i++) {
        m_menuButtonImgSprite[i] = new Sprite(m_menuButtonImg,
                                              DrawTools.MENU_BUTTON_WIDTH,
                                              DrawTools.MENU_BUTTON_HEIGHT);
        m_layerManager.insert(m_menuButtonImgSprite[i], 0);
      }

      m_menuWordImgSprite=new Sprite[MENU_BUTTON_COUNTS];
      for (int i = 0; i < MENU_BUTTON_COUNTS; i++) {
        m_menuWordImgSprite[i]=new Sprite(m_menuWordImg,DrawTools.MENU_WORD_WIDTH,
                                          DrawTools.MENU_WORD_HEIGHT);
        m_layerManager.insert(m_menuWordImgSprite[i], 0);
      }
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }


  private void displayTopAndBottomPanels(Graphics g) {
    try {
      //draw top panel
      int topPanelX, topPanelY = 0;
      int bottomPanelX, bottomPanelY = 0;

      topPanelX = topPanelY = 0;
      DrawTools.XDrawImage(g, m_topPanel, topPanelX, topPanelY,
                           0, 0, GameLogic.MAP_WIDTH, GameLogic.MAP_CELL_HEIGHT);

      bottomPanelX = 0;
      bottomPanelY = GameLogic.MAP_HEIGHT + GameLogic.MAP_CELL_HEIGHT;
      DrawTools.XDrawImage(g, m_topPanel, bottomPanelX, bottomPanelY,
                           0, GameLogic.MAP_CELL_HEIGHT, GameLogic.MAP_WIDTH,
                           GameLogic.MAP_CELL_HEIGHT);
      //显示阴影1
      int shadow1X, shadow1Y = 0;
      shadow1X = 16;
      shadow1Y = 2;
      DrawTools.XDrawImage(g, m_shadowImage, shadow1X, shadow1Y,
                           1, 1, 62, 14);
      //显示金币
      DrawTools.XDrawImage(g, m_panelIcon, shadow1X, shadow1Y,
                           0, 0, 18, 14);
      //显示敌方坦克剩余个数
      DrawTools.XDrawImage(g, m_panelIcon, 182, 2,
                           18, 0, 18, 14);
      //显示X
      DrawTools.XDrawImage(g, m_panelIcon, 198, 2,
                           126, 0, 18, 14);

      //显示阴影2
      int shadow2X, shadow2Y = 0;
      shadow2X = 16;
      shadow2Y = GameLogic.MAP_HEIGHT + GameLogic.MAP_CELL_HEIGHT + 3;
      DrawTools.XDrawImage(g, m_shadowImage, shadow2X, shadow2Y,
                           63, 1, 103, 17);

      //显示阴影3
      int shadow3X, shadow3Y = 0;
      shadow3X = 123;
      shadow3Y = GameLogic.MAP_HEIGHT + GameLogic.MAP_CELL_HEIGHT + 3;
      DrawTools.XDrawImage(g, m_shadowImage, shadow3X, shadow3Y,
                           165, 1, 56, 17);

    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  public static void pause(){
    m_threadAlive=true;
    m_currentState=GameLogic.GAME_STATE_LOOP;
    //跳出POP UP MENU
    setPopupMenu(true);
    GameLogic.pause();
  }


 public static void reset(){
   m_currentMenuButtonIndex=0;
   GameLogic.reset();
 }

  public static void  stop() {
    m_threadAlive=false;
    releaseResource();
	System.gc();
    //System.out.println(Runtime.getRuntime().freeMemory());
  }

  public int getHeight(){
  	return 320;
  }

  public void render(Graphics g) {
    try {
      int w = getWidth();
      int h = getHeight();
      //创建一个BACKGROUND
      g.setColor(0xffffff);
      g.fillRect(0, 0, w, h);

      int x = 0;
      int y = 0;

      //游戏界面的大小的计算(矩形)。
      x = 0;
      y = GameLogic.MAP_CELL_HEIGHT;
      //地图在中间显示
      g.setColor(0x000000);
      if (null != m_layerManager) {
        m_layerManager.paint(g, 0, 0);
      }

      displayBombIcons();
      displayUserTankIcons();
      displayPoints();
      displayEnemyTanks();
      displayStage();

      g.setClip(0, 0, getWidth(), getHeight());


      if (true == m_popupGameMenu) {
        DrawTools.displayMenuButton(
            m_menuButtonImgSprite,
            m_menuWordImgSprite,
            m_switchImgSprite,
            MENU_BUTTON_X,
            MENU_BUTTON_Y,
            m_switchState,
            chosenWordIndex,
            unchosenWordIndex,
            m_currentMenuButtonIndex,
            MENU_BUTTON_COUNTS);
          m_menuBackgroundSprite.setVisible(true);
      }
      else {
        hideMenuButton();
        //don't display plane bomb in pause state
        if (true == GameLogic.m_planeStarted) {
          GameLogic.displayPlane(g);
        }
      }

      //display tank blood bar
      if(null!=m_gameLogic){
        int enemy_num_per = GameLogic.game_level[GameLogic.m_gameLevel][
        GameLogic.GAME_LEVEL_PROPERTY_ENEMY_NUM_PER];
        m_gameLogic.displayTankBloodBar(GameLogic.m_tank,0);
        for(int i=0;i<enemy_num_per;i++){
          m_gameLogic.displayTankBloodBar(GameLogic.m_enemySprite[i],0);
        }
      }

      flushGraphics();

    }
    catch (Exception ex) {
      ex.printStackTrace();
    }

  }

  private void hideMenuButton(){
    if(null ==m_menuButtonImgSprite||null==m_switchImgSprite){
      return;
    }

    try {
      for(int i=0;i<MENU_BUTTON_COUNTS;i++){
        m_menuButtonImgSprite[i].setVisible(false);
        m_menuWordImgSprite[i].setVisible(false);
      }

      for(int i=0;i<DrawTools.SWITCH_WORD_COUNTS;i++){
        m_switchImgSprite[i].setVisible(false);
      }
      m_menuBackgroundSprite.setVisible(false);
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  public void logic() {
    try {
      if(null==m_gameLogic){
        return;
      }
      m_gameLogic.run();
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  public static void setPopupMenu(boolean popup){
    try {
      m_popupGameMenu=popup;
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }


  private String getMenuCmdString(int index){
    String cmd=null;
    switch (index) {
      case BACKWAR_INDEX_SELECTED:
        cmd="backwar";
        break;
      case LOADPROGRESS_INDEX_SELECTED:
        cmd="loadprogress";
        break;
      case TRAINING_INDEX_SELECTED:
        cmd="training";
        break;
      case MUSIC_INDEX_SELECTED:
        cmd="music";
        break;
      case BACKMENU_INDEX_SELECTED:
        cmd="backmenu";
        break;
      default:
        break;
    }

    return cmd;
  }

  protected void showNotify(){
    try {
      if(m_threadAlive==false){
        m_threadAlive=true;
      }
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  //from show to pause
  protected void hideNotify(){
    try {
      m_threadAlive=false;

      GameLogic.pause();
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }


   protected void keyPressed(int keyCode) {
    try {
      //key # pressed
      if (keyCode == KEY_POUND) {
        m_gameLogic.m_tank.useBomb();
      }

      String cmd = null;
      if (DrawTools.KEY_CODE_LSK == keyCode ||
          DrawTools.KEY_CODE_SELECT == keyCode) {
        if (true == m_popupGameMenu) {
          m_popupGameMenu = false;
          cmd = getMenuCmdString(m_currentMenuButtonIndex);
          if(cmd==null){
            return;
          }
          if (cmd.equalsIgnoreCase("backwar")) {
            pause();
          }else if(cmd.equalsIgnoreCase("music")){
            pause();
            //change music state
            changeMusicState(m_switchState);
          }
          else {
            stop(); //release all the resources
          }
          Navigator.flow(cmd);
        }
      }
      else if (DrawTools.KEY_CODE_RSK == keyCode) {
        //pop up game menu
        if (false == m_popupGameMenu) {
          m_popupGameMenu = true; //pop up game menu
          pause();
        }
      }
      else if (DrawTools.KEY_CODE_UP == keyCode) {
        if (true == m_popupGameMenu) {
          m_currentMenuButtonIndex--;
          if (m_currentMenuButtonIndex <= 0) {
            m_currentMenuButtonIndex = 0;
          }
          if (m_currentMenuButtonIndex >= MENU_BUTTON_COUNTS) {
            m_currentMenuButtonIndex = MENU_BUTTON_COUNTS - 1;
          }
        }
      }
      else if (DrawTools.KEY_CODE_DOWN == keyCode) {
        if (true == m_popupGameMenu) {
          m_currentMenuButtonIndex++;
          if (m_currentMenuButtonIndex >= MENU_BUTTON_COUNTS) {
            m_currentMenuButtonIndex = MENU_BUTTON_COUNTS - 1;
          }
          if (m_currentMenuButtonIndex <= 0) {
            m_currentMenuButtonIndex = 0;
          }

        }
      }

    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }

  private void changeMusicState(int state) {
    switch (state) {
      case DrawTools.SWITCH_OPEN:
        m_switchState=DrawTools.SWITCH_CLOSE;
        GameLogic.m_soundOn=false;
        break;
      case DrawTools.SWITCH_CLOSE:
        m_switchState=DrawTools.SWITCH_OPEN;
        GameLogic.m_soundOn=true;
        break;
      default:
        break;
    }

  }

  //for thread implement,the main game loop
  public void run() {
    if (!m_threadAlive) {
      return;
    }

    Graphics g = m_graphics;

    switch (m_currentState) {
      case GameLogic.GAME_STATE_READY:

        break;
      case GameLogic.GAME_STATE_LOOP:
        input();
        logic();
        render(g);
        break;
    }
  }

  public void start() {
    m_popupGameMenu = false; //don't display popup menu
    m_threadAlive=true;
    m_currentState=GameLogic.GAME_STATE_LOOP;
    GameLogic.start();
  }

  private void input() {
    int keyStates = getKeyStates();

    if ( (keyStates & LEFT_PRESSED) != 0) {
      if (true == m_popupGameMenu) {

      }
      else {
        m_gameLogic.m_tank.move(Sprite.
                                TRANS_ROT270, 3);
      }
    }
    else if ( (keyStates & RIGHT_PRESSED) != 0) {
      if (true == m_popupGameMenu) {

      }
      else {
        m_gameLogic.m_tank.move(Sprite.
                                TRANS_ROT90, 3);
      }
    }
    else if ( (keyStates & UP_PRESSED) != 0) {
      if (true == m_popupGameMenu) {
        /*
        m_currentMenuButtonIndex--;
        if (m_currentMenuButtonIndex <= 0) {
          m_currentMenuButtonIndex = 0;
        }
        if (m_currentMenuButtonIndex >= MENU_BUTTON_COUNTS) {
          m_currentMenuButtonIndex = MENU_BUTTON_COUNTS - 1;
        }*/

      }
      else {

⌨️ 快捷键说明

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