gamescreen.java~7~

来自「基于J2ME的手机游戏软件。可以控制游戏人物在地图上上下左右行走;可以在地图上放」· JAVA~7~ 代码 · 共 147 行

JAVA~7~
147
字号
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Image;

public class GameScreen
    extends Canvas
    implements Runnable {
  public static final int STATE_INIT = 1;
  public static final int STATE_PLAYING = 2;
  public static final int STATE_END = 3;

  private BombMan thisMidlet;
  private boolean run;
  private int width;
  private int height;
  private int screenWidth;
  private int screenHeight;
  private int originX;
  private int originY;
  private long perCycleTime;
  private int fps;
  private World world;
  private Image offScreen;
  private boolean isPause;
  private int state;

//	private Thread thread;

  public GameScreen(BombMan midlet) {
    this.setFullScreenMode(true);
    thisMidlet = midlet;
    run = false;
    width = getWidth();
    height = getHeight();
    screenWidth = width;
    screenHeight = height;
//    originX = (width - screenWidth) / 2;
//    originY = (height - screenHeight) / 2;
    perCycleTime = 50L;
    world = new World(this);
    offScreen = Image.createImage(screenWidth, screenHeight);
    isPause = false;
    state = STATE_INIT;
    //	thread = new Thread(this);
  }

  public void start() {
    run = true;
    state = STATE_PLAYING;
    Thread thread = new Thread(this);
    thread.start();
  }

  public void close() {
    run = false;
    state = STATE_END;
    //	thisMidlet.close();
    thisMidlet.activateGameMenu();
  }

  protected void keyPressed(int keyCode) {
    world.keyPressed(getGameAction(keyCode));
  }

  protected void keyReleased(int keyCode) {
    world.keyReleased(getGameAction(keyCode));
  }

  private void cycle() {
    world.cycle();
  }

  public void paint(Graphics gra) {
//    gra.setColor(0x000000);
//    gra.fillRect(0, 0, width, height);
    //////////
//    gra.translate(originX, originY);
    Graphics igra = offScreen.getGraphics();
    world.render(igra);
    gra.drawImage(offScreen, 0, 0, Graphics.TOP | Graphics.LEFT);
    //////////
//    gra.translate( -gra.getTranslateX(), -gra.getTranslateY());
    //	gra.setColor(0x000000);
    //	gra.fillRect(originX + screenWidth,0,width - originX - screenWidth,height);
    //	gra.fillRect(0,originY + screenHeight,width,height - originY - screenHeight);
    //	gra.fillRect(0,0,width,originY);
    //	gra.fillRect(0,0,originX,height);
//    gra.setColor(0xffffff);
//    gra.drawString("FPS:" + fps, 10, 10, Graphics.TOP | Graphics.LEFT);
  }

  public void run() {
    long lastCycleTime = 0L;
    long bufTime = 0L;
    int bufFps = 0;
    while (run) {
      if (!isPause) {
        cycle();
        repaint();
        if (System.currentTimeMillis() - lastCycleTime < perCycleTime) {
          try {
            Thread.sleep(perCycleTime
                         - (System.currentTimeMillis() - lastCycleTime));
          }
          catch (InterruptedException ie) {
            System.out.println("interrupted exception");
          }
        }
        lastCycleTime = System.currentTimeMillis();
        bufFps++;
        if (System.currentTimeMillis() - bufTime > 1000) {
          fps = bufFps;
          bufFps = 0;
          bufTime = System.currentTimeMillis();
        }
      }
      if (isPause) {
        try {
          Thread.sleep(1000);
        }
        catch (InterruptedException ie) {
          ie.printStackTrace();
        }
      }
    }
  }

  public void pause() {
    isPause = true;
    world.pause();
  }

  public void resume() {
    isPause = false;
    world.resume();
  }

  public void startNewGame() {
    world.startNewGame();
    resume();
  }

  public int getState() {
    return state;
  }
}

⌨️ 快捷键说明

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