josn1984

常用链接

统计

最新评论

此J2ME程序已经完善,贴出来大家看看(可惜我没能及时做注释,还请大家见谅)

这个是主文件(后面的是执行的操作文件):
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class LostInForest extends MIDlet implements CommandListener{
 
 private Display display;
 private Canvas canvas;
 private Command exitCmd;
 MenuScreen menuScreen = new MenuScreen();
 MyGameCanvas gameCanvas = new MyGameCanvas();
 
 private Command exitGame;
 Alert aAlert = new Alert("Game Method","Login the game,point up,down,left,right contron",null,AlertType.INFO);
 Alert bAlert = new Alert("Maker","our",null,AlertType.INFO);
 
 public LostInForest(){
  exitCmd = new Command("exit",Command.EXIT,2);
  exitGame = new Command("Exit Game",Command.EXIT,1);
  aAlert.setTimeout(10000);
  bAlert.setTimeout(10000);
  aAlert.addCommand(exitCmd);
  bAlert.addCommand(exitCmd);


  gameCanvas.addCommand(exitGame);
  gameCanvas.setCommandListener(this);
  aAlert.setCommandListener(this);
  bAlert.setCommandListener(this);
 }
 public void startApp() throws MIDletStateChangeException{
  canvas = new MenuScreen();
  display = Display.getDisplay(this);
  display.setCurrent(canvas);
 }
 public void pauseApp(){}
 public void destroyApp(boolean b){}
 public void commandAction(Command cmd,Displayable dis){
  if(cmd == exitCmd){
   display.setCurrent(menuScreen);
  }
  else if(cmd == exitGame){
   System.gc();
   destroyApp(false);
   notifyDestroyed();
  }
 }
 class MenuScreen extends Canvas implements Runnable{
  final Font lowFont = Font.getFont(Font.FACE_MONOSPACE,Font.STYLE_PLAIN,Font.SIZE_MEDIUM);
  final Font highFont = Font.getFont(Font.FACE_MONOSPACE,Font.STYLE_BOLD,Font.SIZE_LARGE);
  final int lowColor = 0x000000AA;
  final int highColor = 0x00FF0000;
  final int highBGColor = 0x00AAAAAA;
  int width;
  int height;
  int startHeight;
  int menuHeight;
  final int spacing = highFont.getHeight()/2;
  final String[] mainMenu = {"Start Game","Game Method","Worker","Exit"};
  int menuIdx;
  Thread menuThread;
  
  public MenuScreen(){
   width = getWidth();
   height = getHeight();
   menuHeight = (highFont.getHeight() * mainMenu.length)+((mainMenu.length-1)*spacing);
   startHeight = (height - menuHeight)/2+4;
   menuIdx = 0;
   menuThread = new Thread(this);
   menuThread.start();
  }
  public void run(){
   while(true){
    repaint();
   }
  }
  public void paint(Graphics g){
   g.setColor(0x00FFFF00);
   g.fillRect(0,0,width,height);
   
   g.setColor(00,00,00);
   g.setFont(Font.getFont(Font.FACE_MONOSPACE,Font.STYLE_BOLD,Font.SIZE_LARGE));
   Font label1 = Font.getFont(Font.FACE_MONOSPACE,Font.STYLE_BOLD,Font.SIZE_LARGE);
   g.drawString("LostInForest",62,label1.getHeight()/2,Graphics.TOP|Graphics.LEFT);
   g.setColor(00,00,00);
   g.setFont(Font.getFont(Font.FACE_MONOSPACE,Font.STYLE_BOLD,Font.SIZE_LARGE));
   g.drawString("LostInForest",0,24,Graphics.TOP|Graphics.LEFT);
   g.setColor(00,00,00);
   g.setFont(Font.getFont(Font.FACE_MONOSPACE,Font.STYLE_BOLD,Font.SIZE_LARGE));
   g.drawString("Copyright@2007 SZPT",25,145,Graphics.LEFT|Graphics.TOP);
   g.setColor(00,00,00);
   g.setFont(Font.getFont(Font.FACE_MONOSPACE,Font.STYLE_BOLD,Font.SIZE_LARGE));
   g.drawString("All Rights Reserved.",30,160,Graphics.LEFT|Graphics.TOP);
   
   for(int i = 0;i<mainMenu.length;i++){
    if(i == menuIdx){
     g.setColor(highBGColor);
     g.fillRect(0,startHeight + (i*highFont.getHeight())+spacing,width,highFont.getHeight());
     g.setFont(highFont);
     g.setColor(highColor);
     g.drawString(mainMenu[i],(width - highFont.stringWidth(mainMenu[i]))/2,startHeight+(i*highFont.getHeight())+spacing,20); 
    }
    else{
   g.setFont(lowFont);
   g.setColor(lowColor);
   g.drawString(mainMenu[i],(width - highFont.stringWidth(mainMenu[i]))/2,startHeight+(i*highFont.getHeight())+spacing,20);
          }
   }
  }
  protected void keyPressed(int code){
   if(getGameAction(code) == Canvas.UP&&menuIdx - 1 >= 0){
    menuIdx--;
   }
   else if(getGameAction(code) == Canvas.DOWN&&menuIdx + 1 < mainMenu.length){
    menuIdx ++;
   }
   if(getGameAction(code) == Canvas.FIRE){
    if(mainMenu[menuIdx] == "Start Game"){
     gameCanvas.start();
     display.setCurrent(gameCanvas);
     }
    else if(mainMenu[menuIdx] == "Game Method"){
     display.setCurrent(aAlert);
    }
    else if(mainMenu[menuIdx] == "Maker"){
     display.setCurrent(bAlert);
    }
    else if(mainMenu[menuIdx] == "Exit"){
     notifyDestroyed();
    }
   }
  } 
 }
}
这个是主操作文件:
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import javax.microedition.rms.*;

public class MyGameCanvas extends GameCanvas implements Runnable{
 private boolean isPlay;
 private long delay;
 private int currentX,currentY;
 private int width;
 private int height;
 Graphics g;
 public Display display;
 Image image = null;
 Image tileImages = null;
 private Sprite spriteRole;
 private static int INDEX_OF_UP = 0;
 private static int INDEX_OF_DOWN = 1;
 private static int INDEX_OF_LEFT = 3;
 private static int INDEX_OF_RIGHT = 2;
 private TiledLayer tiledBackground;
 private TiledLayer tiledLayer = null;
 private LayerManager layerManager;
 private RecordStore rs = null;
 private int gameLifeValueInt = 3;
 private String gameLifeValueString = null;
 private int firstGate = 0;
 private int[] map = {
 };
 
 public MyGameCanvas(){
  super(true);
  width = getWidth() - 20;
  height = getHeight() - 17;
  currentX = 160;
  currentY = 160;
  delay = 20;
  g = getGraphics();
  try{
   image = Image.createImage("/player.png");
   spriteRole = new Sprite(image,16,16);
   tileImages = Image.createImage("/tiles.png");
   tiledLayer = new TiledLayer(12,12,tileImages,16,16);
   tiledBackground = initBackground();
   layerManager = new LayerManager();
   layerManager.append(tiledBackground);
   layerManager.append(spriteRole);
   IntToString();
   createNewDB();
   setRecordToDB(1,gameLifeValueString,0,1);
  }
  catch(Exception e){
   e.printStackTrace();
  }
 }
 public void start(){
  isPlay = true;
  Thread t = new Thread(this);
  t.start();
 }
 public void stop(){
  isPlay = false;
 }
 public void run(){
  while(isPlay){
   input();
   drawScreen(g);
   try{
    Thread.sleep(delay);
   }
   catch(InterruptedException ie){}
  }
 }
 private void IntToString(){
  gameLifeValueString = gameLifeValueInt + "";
 }
 private void createNewDB(){
  try{
   rs = RecordStore.openRecordStore("GameDB",true);
  }
  catch(RecordStoreNotFoundException rsnfe){}
  catch(RecordStoreFullException rsfe){}
  catch(RecordStoreException rse){}
 }
 private void addRecordToDB(String str){
  byte rec[] = str.getBytes();
  try{
   rs.addRecord(rec,0,rec.length);
  }
  catch(RecordStoreFullException rse){}
  catch(RecordStoreException rse){}
 }
 private String getRecordFromDB(){
  byte data[] = new byte[5];
  int dataLength = 0;
  try{
   dataLength = rs.getRecord(1,data,0);
  }
  catch(RecordStoreException rse){}
  return new String(data,0,dataLength);
 }
 private void setRecordToDB(int recordID,String str,int offset,int len){
  try{
   byte data[] = str.getBytes();
   rs.setRecord(recordID,data,offset,len);
  }
  catch(RecordStoreException rse){}
 }
 private void input(){
  int keyStates = getKeyStates();
  try{
   if((keyStates&LEFT_PRESSED)!=0&&gameLifeValueInt > 0){
    int tempX = Math.max(0,currentX - 16);
    int tempPoint = tempX/16+currentY*12/16;
    if(map[tempPoint]==1){
     currentX = tempX;
    }
    else if(map[tempPoint]==5){
     currentX = tempX;
     map[tempPoint] = 1;
     try{
      String temp = getRecordFromDB();
      gameLifeValueInt = Integer.parseInt(temp);
      gameLifeValueInt--;
      IntToString();
      setRecordToDB(1,gameLifeValueString,0,1);
      tiledBackground = initBackground();
     }
     catch(Exception e){}
    }
    else if(map[tempPoint]==6){
     currentX  = tempX;
     map[tempPoint] = 1;
     try{
      String temp = getRecordFromDB();
      gameLifeValueInt = Integer.parseInt(temp);
      gameLifeValueInt++;
      IntToString();
      setRecordToDB(1,gameLifeValueString,0,1);
      tiledBackground = initBackground();
     }
     catch(Exception e){}
    }
    spriteRole.setFrame(INDEX_OF_LEFT);
   }
  else if((keyStates&RIGHT_PRESSED)!=0&&gameLifeValueInt>0){
   int tempX = Math.min(width,currentX + 16);
   int tempPoint = tempX/16+currentY*12/16;
    if(map[tempPoint]==1){
     currentX = tempX;
    }
    else if(map[tempPoint]==5){
     currentX = tempX;
     map[tempPoint] = 1;
     try{
      String temp = getRecordFromDB();
      gameLifeValueInt = Integer.parseInt(temp);
      gameLifeValueInt--;
      IntToString();
      setRecordToDB(1,gameLifeValueString,0,1);
      tiledBackground = initBackground();
     }
     catch(Exception e){}
    }
    else if(map[tempPoint]==6){
     currentX  = tempX;
     map[tempPoint] = 1;
     try{
      String temp = getRecordFromDB();
      gameLifeValueInt = Integer.parseInt(temp);
      gameLifeValueInt++;
      IntToString();
      setRecordToDB(1,gameLifeValueString,0,1);
      tiledBackground = initBackground();
     }
     catch(Exception e){}
    }
    spriteRole.setFrame(INDEX_OF_RIGHT);
  }
  else if((keyStates&UP_PRESSED)!=0&&gameLifeValueInt > 0){
   int tempY = Math.max(0,currentY - 16);
   int tempPoint = currentX/16+tempY*12/16;
    if(map[tempPoint]==1){
     currentY = tempY;
    }
    else if(map[tempPoint]==5){
     currentY = tempY;
     map[tempPoint] = 1;
     try{
      String temp = getRecordFromDB();
      gameLifeValueInt = Integer.parseInt(temp);
      gameLifeValueInt--;
      IntToString();
      setRecordToDB(1,gameLifeValueString,0,1);
      tiledBackground = initBackground();
     }
     catch(Exception e){}
    }
    else if(map[tempPoint]==6){
     currentY = tempY;
     map[tempPoint] = 1;
     try{
      String temp = getRecordFromDB();
      gameLifeValueInt = Integer.parseInt(temp);
      gameLifeValueInt++;
      IntToString();
      setRecordToDB(1,gameLifeValueString,0,1);
      tiledBackground = initBackground();
     }
     catch(Exception e){}
    }   
    else if(map[tempPoint]==2){
     currentY  = tempY;
     try{
      firstGate = 100;
      tiledBackground = initBackground();
     }
     catch(Exception e){}
    }
    spriteRole.setFrame(INDEX_OF_UP);
  }
  else if((keyStates&DOWN_PRESSED)!=0&&gameLifeValueInt>0){
   if(currentY + 10<height){
   int tempY = Math.min(height,currentY + 16);
   int tempPoint = currentX/16+tempY*12/16;
    if(map[tempPoint]==1){
     currentY = tempY;
    }
    else if(map[tempPoint]==5){
     currentY = tempY;
     map[tempPoint] = 1;
     try{
      String temp = getRecordFromDB();
      gameLifeValueInt = Integer.parseInt(temp);
      gameLifeValueInt--;
      IntToString();
      setRecordToDB(1,gameLifeValueString,0,1);
      tiledBackground = initBackground();
     }
     catch(Exception e){}
    }
    else if(map[tempPoint]==6){
     currentY  = tempY;
     map[tempPoint] = 1;
     try{
      String temp = getRecordFromDB();
      gameLifeValueInt = Integer.parseInt(temp);
      gameLifeValueInt++;
      IntToString();
      setRecordToDB(1,gameLifeValueString,0,1);
      tiledBackground = initBackground();
     }
     catch(Exception e){}
    }
    spriteRole.setFrame(INDEX_OF_DOWN);
   }
  }
   }
 catch(ArrayIndexOutOfBoundsException e){}
 }
 
 public void drawScreen(Graphics g){
 g.setColor(0x00FFFFFF);
 g.fillRect(22,22,getWidth(),getHeight());
 layerManager.append(tiledBackground);
 layerManager.paint(g,0,0);
 spriteRole.setPosition(currentX,currentY);
 spriteRole.paint(g);
 g.setColor(255,255,255);
 g.setFont(Font.getFont(Font.FACE_SYSTEM,Font.STYLE_BOLD,Font.SIZE_MEDIUM));
 g.drawString("Life:"+gameLifeValueInt,110,0,Graphics.RIGHT|Graphics.TOP);
 if(gameLifeValueInt<=0){
  g.setFont(Font.getFont(Font.FACE_SYSTEM,Font.STYLE_BOLD,Font.SIZE_LARGE));
  g.drawString("In forest you ...",150,80,Graphics.RIGHT|Graphics.TOP);
  g.setFont(Font.getFont(Font.FACE_SYSTEM,Font.STYLE_BOLD,Font.SIZE_SMALL));
  g.drawString("PLEASE RESTART",170,100,Graphics.RIGHT|Graphics.TOP);
 }
 else if(gameLifeValueInt>=1&&firstGate == 100){
  g.setFont(Font.getFont(Font.FACE_SYSTEM,Font.STYLE_BOLD,Font.SIZE_LARGE));
  g.drawString("you do it",160,80,Graphics.RIGHT|Graphics.TOP);
  g.setFont(Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN,Font.SIZE_SMALL));
  g.drawString("MISSION COMPLETE",170,100,Graphics.RIGHT|Graphics.TOP);
 }
 flushGraphics();
  }
  private TiledLayer initBackground() throws Exception{
   for(int i = 0; i < map.length; i ++){
    int column = i % 12;
    int row = (i - column)/12;
    tiledLayer.setCell(column,row,map[i]);
   }
   return tiledLayer;
  }
}
至于地图,请大家自己看着程序自己随意构思一个好了。我没有在这里写入地图,呵呵!程序已经编译通过!

posted on 2007-04-25 06:32 香蕈阁 阅读(505) 评论(0)  编辑  收藏 所属分类: java程序代码


只有注册用户登录后才能发表评论。


网站导航: