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

📄 gamescreen.java

📁 J2ME Game Programming的隋书源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
               {
                  synchronized (this)
                  {
                     wait(MS_PER_FRAME - timeSinceStart);
                  }
               }
               else
                  Thread.yield();

            }
            else
            {
               // just hang around sleeping if we are paused
               try
               {
                  Thread.sleep(100);
               }
               catch (java.lang.InterruptedException e)
               {
               }
            }
         }

         // fall back to menu
         theMidlet.activateMenu();
      }
      catch (Exception e)
      {
         System.out.println("App exception: " + e);
         e.printStackTrace();
      }
   }

   public void loadGame()
   {
      world.loadLevel();
      setState(STARTING_UP);
   }

   public void startNewGame()
   {
      setLives(0);
      score = 0;
      world.setLevelNum(1);
      world.generateLevel();
      setState(STARTING_UP);
      resetPanView();
   }

   public void setLives(int lives)
   {
      this.lives = lives;
   }

   public int getLives()
   {
      return lives;
   }

   public void restart()
   {
      world.restart();
      setState(STARTING_UP);
      resetPanView();
   }

   public void notifyLevelOver()
   {
      setState(STARTING_UP);
      world.setLevelNum(world.getLevelNum() + 1);
      world.clear();
      world.generateLevel();
   }

   public void notifyShipDied(Ship ship)
   {
      if (ship == playerShip)
      {
         setLives(lives - 1);
         if (lives < 1)
         {
            setState(GAME_OVER);
            world.removeSavedGame();
         }
         else
         {
            world.saveLevel(); // resave to store lives count
            world.restart();
            setState(DYING);
         }
      }
   }

   private final void updateEnergyBar(int barFill, boolean showRed)
   {
      Graphics g = energyBarImage.getGraphics();
      g.setColor(0);
      g.fillRect(0, 0, energyBarImage.getWidth(), energyBarImage.getHeight());

      g.setColor(0xaaaaaa);
      g.drawRect(0, 0, barWidth, 6);

      // show red bar if under half
      if (showRed)
         g.setColor(0xcc2222);
      else
         g.setColor(0x22cc22);

      g.fillRect(1, 1, barFill, 5);
   }

   public void resetPanView()
   {
      currentViewPosX = playerShip.getX() - 50;
      currentViewPosY = playerShip.getY() - 50;
   }

   private final void renderWorld(Graphics graphics)
   {
      // clear the background
      graphics.setColor(0);
      graphics.fillRect(0, 0, screenWidth, screenHeight);

      world.setView(currentViewPosX-halfScreenWidth, currentViewPosY-halfScreenHeight);
      //world.setView(playerShip.getCenterX() - halfScreenWidth, playerShip.getCenterY() - halfScreenHeight);
      world.render(graphics);

      // draw the playerShip energy bar
      int p = playerShip.getEnergyMax() / barWidth;
      int barFill = playerShip.getEnergy() / p;
      if (lastDrawnBarValue != barFill)
      {
         updateEnergyBar(barFill, playerShip.getEnergy() > 0 &&
                                  playerShip.getEnergyMax() / playerShip.getEnergy() > 1);
         lastDrawnBarValue = barFill;
      }
      graphics.drawImage(energyBarImage, 6, screenHeight - 12, Tools.GRAPHICS_TOP_LEFT);

      // Draw the score.
      graphics.setColor(0x00ffffff);
      graphics.setFont(defaultFont);
      graphics.drawString("" + score, getWidth()-2, 2, Graphics.TOP|Graphics.RIGHT);

      //#ifdef debug
      // draw the CPS
      graphics.setColor(0x00ffcc66);
      graphics.setFont(defaultFont);
      graphics.drawString("" + cps, getWidth() - 30, getHeight() - defaultFontHeight, Tools.GRAPHICS_TOP_LEFT);
      //#endif

      //graphics.setFont(defaultFont);
      //graphics.setColor(0x00ffcc66);
      //graphics.drawString("x=" + playerShip.getX() + " y=" + playerShip.getY() + " tx=" +
      //						  world.getTileAtX(playerShip.getX()) + " ty=" + world.getTileAtY(playerShip.getY()),
      //						  0, 0, Tools.GRAPHICS_TOP_LEFT);

      if (state == STARTING_UP)
      {
         drawGroovyText(graphics, "LEVEL " + world.getLevelNum(), halfScreenWidth, halfScreenHeight + defaultFontHeight);
         drawGroovyText(graphics, "GET READY...", halfScreenWidth, halfScreenHeight + (defaultFontHeight * 2) + 3);

         // draw a ship for every life they have left
         for (int i = 0; i < lives - 1; i++)
         {
            Ship.getYellowShipImageSet().draw(graphics, 12, 0,
                                              ((screenWidth - ((lives - 1) * 18)) / 2) + (i * 18),
                                              halfScreenHeight + (defaultFontHeight * 3) + 4);
         }
      }

      if (state == GAME_OVER)
         drawGroovyText(graphics, "GAME OVER", getWidth() / 2, getHeight() / 2 + defaultFontHeight);
   }

   private void drawGroovyText(Graphics graphics, String text, int x, int y)
   {
      graphics.setFont(defaultFont);
      graphics.setColor(0xffcc66);
      graphics.drawString(text, x + 1, y, Graphics.HCENTER | Graphics.TOP);
      graphics.drawString(text, x, y + 1, Graphics.HCENTER | Graphics.TOP);
      graphics.drawString(text, x - 1, y, Graphics.HCENTER | Graphics.TOP);
      graphics.drawString(text, x, y - 1, Graphics.HCENTER | Graphics.TOP);
      graphics.setColor(0x000000);
      graphics.drawString(text, x, y, Graphics.HCENTER | Graphics.TOP);
   }

   protected void paint(Graphics graphics)
   {
      if (!isDoubleBuffered())
      {
         renderWorld(osg);
         graphics.drawImage(osb, 0, 0, Tools.GRAPHICS_TOP_LEFT);
      }
      else
      {
         renderWorld(graphics);
      }
   }

   protected void keyPressed(int keyCode)
   {
      if (state != PLAYING) return;

      int action = getGameAction(keyCode);
      if (action == RIGHT || keyCode == rightKeyCode)
         playerShip.setSpin(-23);
      if (action == LEFT || keyCode == leftKeyCode)
         playerShip.setSpin(23);

      if (!StarAssault.getApp().isOptionAutoFire())
      {
         if (action == GAME_A || keyCode == fireKeyCode || action == UP)
            playerShip.setFiring(true);
      }

      //#ifdef nokia
      //# if (keyCode == FullCanvas.KEY_SOFTKEY1 || keyCode == FullCanvas.KEY_SOFTKEY2)
      //# {
         //# pause();
         //# theMidlet.activateMenu();
      //# }
      //#endif
   }

   protected void keyReleased(int keyCode)
   {
      if (state != PLAYING) return;

      int action = getGameAction(keyCode);
      if (action == RIGHT || keyCode == rightKeyCode)
         playerShip.setSpin(0);
      if (action == LEFT || keyCode == leftKeyCode)
         playerShip.setSpin(0);
      if (!StarAssault.getApp().isOptionAutoFire())
      {
         if (action == GAME_A || keyCode == fireKeyCode || action == UP)
            playerShip.setFiring(false);
      }
   }

   public void pause()
   {
      statePriorToPause = state;
      setState(PAUSED);
   }

   public void resume()
   {
      if (state != PAUSED) return;

      // special case: stop firing if they've turned off autofire
      // we also turn off things they may have left on when they paused
      if (!StarAssault.getApp().isOptionAutoFire())
      {
         playerShip.setFiring(false);
         playerShip.setSpin(0);
      }

      setState(statePriorToPause);
   }

   //#ifndef nokia
   public void commandAction(Command c, Displayable d)
   {
      if (c == menu)
      {
         pause();
         theMidlet.activateMenu();
      }
   }
   //#endif
}

⌨️ 快捷键说明

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