#眉標=Java #副標=如何簡化遊戲的設計 #大標=Game API進階(中) #引言= ======================== [程式1] MyGameWithSpriteRotateCanvas.java import javax.microedition.lcdui.* ; import javax.microedition.lcdui.game.* ; public class MyGameWithSpriteRotateCanvas extends GameCanvas implements Runnable { private LayerManager lm; private Sprite c1 ; public MyGameWithSpriteRotateCanvas() { super(true) ; lm = new LayerManager(); c1 = createTank("/pic/tank.png") ; lm.append(c1); } private Sprite createTank(String pic) { Image img = null ; try { img = Image.createImage(pic); }catch(Exception exp) { System.out.println(exp); } return new Sprite(img,32,32) ; } boolean conti = true ; int rate = 100 ; public void run() { long st = 0 ; long et = 0 ; Graphics g = getGraphics() ; while(conti) { st = System.currentTimeMillis() ; input() ; render(g) ; et = System.currentTimeMillis() ; if((et-st)= 96 ) y = 90 ; }if((keystate & LEFT_PRESSED)!=0) { x = x - 2 ; //判斷左邊界 if( x <= 0 ) x = 0 ; }if((keystate & RIGHT_PRESSED)!=0) { x = x + 2 ; //判斷右邊界 if( x >= 96 ) x = 90 ; } } public void render(Graphics g) { g.setColor(255,255,255); g.fillRect(0,0,getWidth(),getHeight()); c1.setPosition(x,y); lm.paint(g,10,10); g.setColor(0,0,0); g.drawRect(10,10,128,128); flushGraphics() ; } public void start() { Thread t = new Thread(this) ; t.start(); } public void exit() { conti = false ; } } ================================ =========================== [程式2] TankSprite.java import javax.microedition.lcdui.* ; import javax.microedition.lcdui.game.* ; public class TankSprite extends Sprite { int bx = 0 ; int by = 0 ; public TankSprite(Image img,int w,int h,int bx,int by) { super(img,w,h); this.bx = bx ; this.by = by ; } public void moveUp() { move(0,-2) ; //判斷上邊界 if( getY() <= 0 ) { setPosition(getX(),0) ; } } public void moveDown() { move(0,2) ; //判斷下邊界 if( getY() > (by - getHeight()) ) { setPosition(getX(),(by - getHeight())) ; } } public void moveLeft() { move(-2,0) ; //判斷左邊界 if( getX() <= 0 ) { setPosition(0,getY()) ; } } public void moveRight() { move(2,0) ; //判斷右邊界 if( getX() > (bx - getWidth()) ) { setPosition((bx - getWidth()),getY()) ; } } } =====================================   ======================   [程式3] MyGameWithSpriteRotateCanvas2.java import javax.microedition.lcdui.* ; import javax.microedition.lcdui.game.* ; public class MyGameWithSpriteRotateCanvas2 extends GameCanvas implements Runnable { private LayerManager lm; private TankSprite c1 ; public MyGameWithSpriteRotateCanvas2() { super(true) ; lm = new LayerManager(); c1 = createTank("/pic/tank.png") ; c1.setPosition(10,10); lm.append(c1); } private TankSprite createTank(String pic) { Image img = null ; try { img = Image.createImage(pic); }catch(Exception exp) { System.out.println(exp); } return new TankSprite(img,32,32,128,128) ; } boolean conti = true ; int rate = 100 ; public void run() { long st = 0 ; long et = 0 ; Graphics g = getGraphics() ; while(conti) { st = System.currentTimeMillis() ; input() ; render(g) ; et = System.currentTimeMillis() ; if((et-st)============================   如此一來,整個系統看起來有調理多了。 ======================== [程式4] c1 = createTank("/pic/tank.png") ; c1.setPosition(20,20); System.out.println("Pixel X:"+ c1.getX()); System.out.println("Pixel Y:"+ c1.getY()); System.out.println("Pixel X ref :"+ c1.getRefPixelX()); System.out.println("Pixel Y ref :"+ c1.getRefPixelY()); ================================== ==================== ?程式5? c1.defineReferencePixel(5,5); c1.setPosition(20,20); System.out.println("Pixel X:"+ c1.getX()); System.out.println("Pixel Y:"+ c1.getY()); System.out.println("Pixel X ref :"+ c1.getRefPixelX()); System.out.println("Pixel Y ref :"+ c1.getRefPixelY()); ====================== ================== [程式6] c1.defineReferencePixel(5,5); c1.setRefPixelPosition(40,40); System.out.println("Pixel X:"+ c1.getX()); System.out.println("Pixel Y:"+ c1.getY()); System.out.println("Pixel X ref :"+ c1.getRefPixelX()); System.out.println("Pixel Y ref :"+ c1.getRefPixelY()); ============================