熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Oracle >> 正文

使用 Jbulider 開發 J2ME 移動游戲程序

2022-06-13   來源: Oracle 

  作者使用JBuilder裡面自帶WTK自己下載版本那個默認的模擬器看著實在爽另外Jbuilder X下的WTK也是的版本Jbuilder 的WTK是首先開啟Jbuilder建立一個Project
  
 

  然後填寫名字和路徑繼續
  
 

  然後選擇JDK路徑本身已有WTK你可以選擇但是你也可以自己選擇其他的WTK版本:
  
 

  點擊jdk後面的路徑按鈕繼續:
  
 

  next工程建立完畢接著點擊file>New:
  
 //jpg>

  然後在對話框左邊選擇Micro你就能看到右邊出現MIDletDisplayable接上面的:如圖示
  
 //jpg>

  點擊MIDlet or Displayable則讓你輸入MIDlet的名字如圖:
  
 //jpg>

  繼續下一步讓你輸入一個Displayable的名字包括類型(formcanvas等)是否自動加載Command等等
  
 //jpg>

  然後Jbuilder自動生成所需代碼以及Jbuilder自加的一些東西你可以添加或修改之這裡我將本站一篇關於介紹Gamecanvas的文章的代碼拿過來做少量修改作為本文章的一個演示
  
  TestMIDletjava文件:
  
  package temps;
  
  import javaxmicroeditionmidlet*;
  import javaxmicroeditionlcdui*;
  
  /**
  *

  Title:


  *
  *

  Description:


  *
  *

  Copyright: Copyright (c)


  *
  *

  Company:


  *
  * @author not attributable
  * @version
  */
  public class TestMIDlet extends MIDlet
  {
  private Display display;
  
  public void startApp()
  {
  try
  {
  display = DisplaygetDisplay(this);
  Displayable gameCanvas = new Displayable();
  gameCanvasstart();
  displaysetCurrent(gameCanvas);
  }
  catch (Exception ex)
  {
  Systemoutprintln(ex);
  }
  }
  
  public Display getDisplay()
  {
  return display;
  }
  
  public void pauseApp()
  {
  }
  
  public void destroyApp(boolean unconditional)
  {
  exit();
  }
  
  public void exit()
  {
  Systemgc();
  destroyApp(false);
  notifyDestroyed();
  }
  }
  
  Displayablejava內容:
  
  package temps;
  
  import javaxmicroeditionlcdui*;
  import javautilRandom;
  import javaxmicroeditionlcduigame*;
  /**
  *

  Title:


  *
  *

  Description:


  *
  *

  Copyright: Copyright (c)


  *
  *

  Company:


  *
  * @author not attributable
  * @version
  */
  public class Displayable 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(SystemcurrentTimeMillis());
  private final int[] sequence={};
  private int counter=;
  private int enemyXenemyY;
  private int enemyDirect=;
  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 Displayable() throws Exception
  {
  super(true);
  width = getWidth();
  height = getHeight();
  
  currentX = width / ;
  currentY = height / ;
  enemyX=width/;
  enemyY=height/;
  delay = ;
  
  Image playerImage=null;
  Image backgroundImage=null;
  Image enemy=null;
  // Load Images to Sprites
  try
  {
  playerImage = ImagecreateImage(/transparentpng);
  backgroundImage = ImagecreateImage(/backgroundpng);
  enemy=ImagecreateImage(/framespng);
  }
  catch(Exception e)
  {
  Systemoutprintln(get Image error!+egetMessage());
  }
  
  playerSprite = new Sprite(playerImage );
  Systemoutprintln(playerSpritegetRawFrameCount());
  enemySprite=new Sprite(enemy);
  backgroundSprite = new Sprite(backgroundImage);
  layerManager = new LayerManager();
  layerManagerappend(playerSprite);
  layerManagerappend(enemySprite);
  layerManagerappend(backgroundSprite);
  
  }
  
  // Automatically start thread for game loop
  public void start()
  {
  isPlay = true;
  Thread t = new Thread(this);
  tstart();
  }
  
  public void stop()
  {
  isPlay = false;
  }
  
  // Main Game Loop
  public void run()
  {
  Graphics g = getGraphics();
  while (isPlay == true)
  {
  input();
  drawScreen(g);
  try
  {
  Threadsleep(delay);
  }
  catch (InterruptedException ie)
  {
  
  }
  }
  }
  
  // Method to Handle User Inputs
  private void input()
  {
  int keyStates = getKeyStates();
  
  playerSpritesetFrame();
  if(isalive)
  {
  if(counter==)
  {
  enemyDirect=Mathabs(randomnextInt()%);
  }
  counter++;
  switch(enemyDirect)
  {
  case :
  enemyY=Mathmax(enemyY);
  break;
  case :
  enemyY=Mathmin(++enemyYheightenemySpritegetHeight());
  break;
  case :
  enemyX=Mathmax(enemyX);
  break;
  case :
  enemyX=Mathmin(++enemyXwidthenemySpritegetWidth());
  break;
  }
  counter=(counter++)%(sequencelength());
  enemySpritesetFrame(sequence[counter]);
  }
  else
  {
  enemyY+=counter;
  enemyY=Mathmin(enemyYheightenemySpritegetHeight());
  
  if(enemyY  counter++;
  enemySpritesetFrame();
  }
  
  // Left
  if ((keyStates & LEFT_PRESSED) != )
  {
  currentX = Mathmax( currentX );
  playerSpritesetFrame();
  }
  
  // Right
  if ((keyStates & RIGHT_PRESSED) != )
  if (currentX + < width)
  {
  currentX = Mathmin(width currentX + );
  playerSpritesetFrame();
  }
  
  // Up
  if ((keyStates & UP_PRESSED) != )
  {
  currentY = Mathmax( currentY );
  playerSpritesetFrame();
  }
  
  // Down
  if ((keyStates & DOWN_PRESSED) != )
  {
  isdown=true;
  }
  
  if ((keyStates & DOWN_PRESSED) != )
  {
  if(isdown)
  {
  if (currentY + < 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
From:http://tw.wingwit.com/Article/program/Oracle/201311/17168.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.