基于J2ME WTK的2D手机游戏开发(英文文献翻译) 第10页

基于J2ME WTK的2D手机游戏开发(英文文献翻译) 第10页
GameMIDlet类
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class GameMIDlet extends MIDlet {
 Display display;
 Form form;
 GameMain gamecanvas;
 public GameMIDlet() {
  display=Display.getDisplay(this);
  gamecanvas=GameMain.getGameMain();
 }
 protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
  // TODO Auto-generated method stub
 }
 protected void pauseApp() {
  // TODO Auto-generated method stub
 }
 protected void startApp() throws MIDletStateChangeException {
  display.setCurrent(gamecanvas);
  
 }
}
GameMain类
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.GameCanvas;
public class GameMain extends GameCanvas implements Runnable {
 public static final byte STARE_PLAY = 1;// 游戏变量
 ScreenBase base;
 static int iAction = 0;
 public static byte state =STARE_PLAY;// 状态变量
 static Graphics grap; // 画笔
 boolean ran = true;
 private static final int FRAME_PER_SEC = 80;// 时间常量
 static GameMain gm;// 定义输出变量
 static int widht = 0, hight = 0;

 private GameMain() {
  super(false);
  this.setFullScreenMode(true);// 图像接收
  grap = this.getGraphics();
  hight = this.getHeight();
  widht = this.getWidth(); 
  setNextState(state);// 新的状态改变方法
  new Thread(this).start();// 线程运行
 }
 public static GameMain getGameMain() {
  if (gm == null)
   gm = new GameMain();
  return gm;
 }
 public void run() {
  long time = 0;
  long time_elapsed = 0;
  while (ran) {
   time = System.currentTimeMillis();// 获得系统运行时间
   time_elapsed = System.currentTimeMillis() - time;
   if (time_elapsed < FRAME_PER_SEC) {
    base.input(iAction);
    base.update();
    base.render();
    this.flushGraphics();// 刷新屏幕
    try {
     Thread.sleep(FRAME_PER_SEC - time_elapsed);
    } catch (InterruptedException e) {
     e.printStackTrace();
    }
   }
  }
 }
 public void setNextState(byte new_state) {
  if (base != null) {
   base.free();
   base = null;
  }
  System.gc();// 手动释放
  switch (new_state) {
  case STARE_PLAY:
   if (base == null) {
    base = GamePlay.getInstance();
   }
   break;
  }
  state = new_state;
 }
 public void keyPressed(int keyCode) {// 接收按键方法
  iAction = keyCode;
 }
 public void keyReleased(int keyCode) {// 按键释放
  iAction = 0;
 }
}
GamePlay类
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.LayerManager;
public class GamePlay extends ScreenBase {
 Image img;
 Image img_man;// 主角
 Image img_bing1;
 Image img_hp;
 Image img_ren;
 Image img_yun;
www.751com.cn
 static Role man;
 // Stone stone;
 static LayerManager lay;
 int x = 0, y = 0;
 byte t = 0;// 运行次数
 int time = 0;// 图片张数
 int hight;
 int wight;
 Image img1, img2, img3, img4, img5;
 int man_x = 0;// 人物x坐标
 int man_y = 0;// 人物y坐标
 static final int[] STOP_SEQ = { 0 };
 static final int[] WALK_SEQ = { 0, 1, 2, 3 };
 static final int[] JUMP_SEQ = { 4, 5 };
 static final int[] SQUAT_SEQ = { 6 };
 static final int[] STAGGER_SEQ = { 7, 8, 9, 10 };
 static final int[] BEI_SEQ = { 11 };
 static int manx = 0;
 static Map map_hit;
 static int many = 0;
 public static int level;// 关
 public static int state_t;// 状态
 private static GamePlay gameplay;
 private GamePlay() {
  lay = new LayerManager();
  img_bing1 = Tools.read_image("bing_1");
  img_hp = Tools.read_image("xue");
  img_ren = Tools.read_image("xueren");
  setLevel(Tools.LOGO);
 }
 public static GamePlay getInstance() {
  if (gameplay == null)
   gameplay = new GamePlay();
  return gameplay;
 }
 public void setLevel(int setlevel) {
  for (int i = lay.getSize() - 1; i > -1; i--) {
   lay.remove(lay.getLayerAt(i));
  }
  switch (setlevel) {
  case Tools.LOGO:

上一页  [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]  ... 下一页  >> 

Copyright © 2007-2012 www.chuibin.com 六维论文网 版权所有