首页 > 开发 > 综合 > 正文

使用 Jbulider 开发 J2ME 移动游戏程序

2024-07-21 02:39:04
字体:
来源:转载
供稿:网友

  作者使用JBuilder2005,里面自带WTK2.1,自己下载2.2版本,2.2那个默认的模拟器看着实在爽。另外,Jbuilder X下的WTK也是2.0的版本。Jbuilder 9的WTK是1.04。首先开启Jbuilder。建立一个PRoject:
  
 使用 Jbulider 开发 J2ME 移动游戏程序(图一)

  然后填写名字和路径。继续:
  
 使用 Jbulider 开发 J2ME 移动游戏程序(图二)

  然后选择JDK路径,本身已有WTK2.1,你可以选择。但是你也可以自己选择其他的WTK版本:
  
 使用 Jbulider 开发 J2ME 移动游戏程序(图三)

  点击jdk后面的路径按钮,继续:
  
 使用 Jbulider 开发 J2ME 移动游戏程序(图四)

  next.工程建立完毕。接着点击file->New:
  
 使用 Jbulider 开发 J2ME 移动游戏程序(图五)

  然后在对话框左边选择Micro,你就能看到右边出现MIDlet,Displayable。接上面的:如图示
  
 使用 Jbulider 开发 J2ME 移动游戏程序(图六)

  点击MIDlet or Displayable,则让你输入MIDlet的名字.如图:
  
 使用 Jbulider 开发 J2ME 移动游戏程序(图七)

  继续下一步,让你输入一个Displayable的名字,包括类型(form,canvas等),是否自动加载Command等等。
  
 使用 Jbulider 开发 J2ME 移动游戏程序(图八)

  然后Jbuilder自动生成所需代码,以及Jbuilder自加的一些东西。你可以添加或修改之。这里我将本站一篇关于介绍Gamecanvas的文章的代码拿过来,做少量修改,作为本文章的一个演示:
  
  TestMIDlet.java文件:
  
  package temps;
  
  import javax.microedition.midlet.*;
  import javax.microedition.lcdui.*;
  
  /**
  * Title:
  *
  * Description:
  *
  * Copyright: Copyright (c) 2005
  *
  * Company:
  *
  * @author not attributable
  * @version 1.0
  */
  public class TestMIDlet extends MIDlet
  {
  private Display display;
  
  public void startApp()
  {
  try
  {
  display = Display.getDisplay(this);
  Displayable1 gameCanvas = new Displayable1();
  gameCanvas.start();
  display.setCurrent(gameCanvas);
  }
  catch (Exception ex)
  {
  System.out.println(ex);
  }
  }
  
  public Display getDisplay()
  {
  return display;
  }
  
  public void pauseApp()
  {
  }
  
  public void destroyApp(boolean unconditional)
  {
  exit();
  }
  
  public void exit()
  {
  System.gc();
  destroyApp(false);
  notifyDestroyed();
  }
  }
  
  Displayable1.java内容:
  
  package temps;
  
  import javax.microedition.lcdui.*;
  import java.util.Random;
  import javax.microedition.lcdui.game.*;
  /**
  *
Title:
  *
  * Description:
  *
  * Copyright: Copyright (c) 2005
  *
  * Company:
  *
  * @author not attributable
  * @version 1.0
  */
  public class Displayable1 extends GameCanvas implements Runnable
  {
  private boolean isPlay; // Game Loop runs when isPlay is true
  private long delay; // To give thread consistency
  private int currentX, currentY; // To hold current position of the 'X'
  private int width; // To hold screen width
  private int height; // To hold screen height
  private Random random=new Random(System.currentTimeMillis());
  private final int[] sequence={0,1,2,1,0,1,2,1,0,1,2,1,1,1,1,1,1};
  private int counter=0;
  private int enemyX,enemyY;
  private int enemyDirect=0;
  private boolean isdown=false;
  
  private boolean isalive=true;
  
  // Sprites to be used
  private Sprite playerSprite;
  private Sprite backgroundSprite;
  private Sprite enemySprite;
  
  // Layer Manager
  private LayerManager layerManager;
  
  // ConstrUCtor and initialization
  public Displayable1() throws Exception
  {
  super(true);
  width = getWidth();
  height = getHeight();
  
  currentX = width / 2;
  currentY = height / 2;
  enemyX=width/4;
  enemyY=height/4;
  delay = 100;
  
  Image playerImage=null;
  Image backgroundImage=null;
  Image enemy=null;
  // Load Images to Sprites
  try
  {
  playerImage = Image.createImage("/transparent.png");
  backgroundImage = Image.createImage("/background.png");
  enemy=Image.createImage("/frames.png");
  }
  catch(Exception e)
  {
  System.out.println("get Image error!"+e.getMessage());
  }
  
  playerSprite = new Sprite(playerImage, 32, 32);
  System.out.println(playerSprite.getRawFrameCount());
  enemySprite=new Sprite(enemy,50,21);
  backgroundSprite = new Sprite(backgroundImage);
  layerManager = new LayerManager();
  layerManager.append(playerSprite);
  layerManager.append(enemySprite);
  layerManager.append(backgroundSprite);
  
  }
  
  // Automatically start thread for game loop
  public void start()
  {
  isPlay = true;
  Thread t = new Thread(this);
  t.start();
  }
  
  public void stop()
  {
  isPlay = false;
  }
  
  // Main Game Loop
  public void run()
  {
  Graphics g = getGraphics();
  while (isPlay == true)
  {
  input();
  drawScreen(g);
  try
  {
  Thread.sleep(delay);
  }
  catch (InterruptedException ie)
  {
  
  }
  }
  }
  
  // Method to Handle User Inputs
  private void input()
  {
  int keyStates = getKeyStates();
  
  playerSprite.setFrame(0);
  if(isalive)
  {
  if(counter==0)
  {
  enemyDirect=Math.abs(random.nextInt()%4);
  }
  counter++;
  switch(enemyDirect)
  {
  case 0:
  enemyY=Math.max(0,--enemyY);
  break;
  case 1:
  enemyY=Math.min(++enemyY,height-enemySprite.getHeight());
  break;
  case 2:
  enemyX=Math.max(0,--enemyX);
  break;
  case 3:
  enemyX=Math.min(++enemyX,width-enemySprite.getWidth());
  break;
  }
  counter=(counter++)%(sequence.length());
  enemySprite.setFrame(sequence[counter]);
  }
  else
  {
  enemyY+=counter;
  enemyY=Math.min(enemyY,height-enemySprite.getHeight());
  
  if(enemyY  counter++;
  enemySprite.setFrame(3);
  }
  
  // Left
  if ((keyStates & LEFT_PRESSED) !
= 0)
  {
  currentX = Math.max(0, currentX - 1);
  playerSprite.setFrame(1);
  }
  
  // Right
  if ((keyStates & RIGHT_PRESSED) != 0)
  if (currentX + 5 < width)
  {
  currentX = Math.min(width, currentX + 1);
  playerSprite.setFrame(3);
  }
  
  // Up
  if ((keyStates & UP_PRESSED) != 0)
  {
  currentY = Math.max(0, currentY - 1);
  playerSprite.setFrame(2);
  }
  
  // Down
  if ((keyStates & DOWN_PRESSED) != 0)
  {
  isdown=true;
  }
  
  if ((keyStates & DOWN_PRESSED) != 0)
  {
  if(isdown)
  {
  if (currentY + 10 < height)
  {
  currentY = Math.min(height, currentY + 1);
  playerSprite.setFrame(4);
  }
  }
  }
  
  if((keyStates & FIRE_PRESSED)!=0)
  {
  isalive=!isalive;
  counter=0;
  }
  
  if(!(enemyX+enemySprite.getWidth()   enemyY+enemySprite.getHeight()   enemyX>currentX+playerSprite.getWidth()
   enemyY>currentY+playerSprite.getHeight()) && isalive)
  {
  counter=0;
  isalive=false;
  }
  }
  
  // Method to Display Graphics
  private void

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表