?? manbullets.java~32~
字號:
package man;import javax.microedition.lcdui.game.*;import javax.microedition.lcdui.*;import java.util.Random;/** * 子彈操作類 */public class ManBullets { private ManGameCanvas manGC; // 子彈運(yùn)動的坐標(biāo)軌跡,Locus[i][j],j為0-5,表示: // 子彈類型,位置x,y,速度vx,vy private int[][] Locus; private Sprite Bullets; // 子彈圖象 private Random rnd; // 隨機(jī)種子 private int SWidth, SHeight; // 設(shè)備屏幕的寬度與高度 //--- 構(gòu)造函數(shù) ---------------------------------------------------------------- public ManBullets(ManGameCanvas manGC, Image img, int SWidth, int SHeight) { this.manGC = manGC; Bullets = new Sprite(img, img.getWidth(), img.getHeight()); Bullets.setFrame(0); Locus = new int[20][5]; rnd = new Random(); this.SWidth = SWidth; this.SHeight = SHeight; } //--- 初始化運(yùn)行軌跡 ----------------------------------------------------------- public void InitBulletsLocus() { for (int i = 0; i < Locus.length; i++) { InitBulletLocus(i); } } //--- 初始化運(yùn)行軌跡隨機(jī)數(shù)組 ---------------------------------------------------- private void InitBulletLocus(int i) { // 子彈的類型,0:上 1:下 2:左 3:右 Locus[i][0] = (rnd.nextInt() & 0x7fffffff) % 4; switch (Locus[i][0]) { case 0: Locus[i][1] = -5; Locus[i][2] = (rnd.nextInt() & 0x7fffffff) % SHeight; Locus[i][3] = (rnd.nextInt() & 0x7fffffff) % 3 + 1; //vx Locus[i][4] = (rnd.nextInt()) % 3; //vy break; case 1: Locus[i][1] = SWidth + 5; Locus[i][2] = (rnd.nextInt() & 0x7fffffff) % SHeight; Locus[i][3] = ((rnd.nextInt() & 0x7fffffff) % 3 + 1) * -1; //vx Locus[i][4] = (rnd.nextInt()) % 3; //vy break; case 2: Locus[i][1] = (rnd.nextInt() & 0x7fffffff) % SWidth; Locus[i][2] = -5; Locus[i][3] = (rnd.nextInt()) % 3; //vx Locus[i][4] = (rnd.nextInt() & 0x7fffffff) % 3 + 1; //vy break; case 3: Locus[i][1] = (rnd.nextInt() & 0x7fffffff) % SWidth; Locus[i][2] = SHeight + 5; Locus[i][3] = (rnd.nextInt()) % 3; //vx Locus[i][4] = ((rnd.nextInt() & 0x7fffffff) % 3 + 1) * -1; //vy break; } } //--- 顯示子彈 ---------------------------------------------------------------- public void paint(Graphics g, int planeX, int planeY) { for (int i = 0; i < Locus.length; i++) { if (isCollision(planeX, planeY, i)) { // 碰撞判斷 if (!manGC.gameover) manGC.gameover = true; manGC.SpExplosion.setPosition(Locus[i][1] - 16, Locus[i][2] - 16); continue; } Bullets.setPosition(Locus[i][1], Locus[i][2]); Bullets.paint(g); UpdataBulletLocus(i); // 更新運(yùn)行軌跡數(shù)組 } } //--- 更新運(yùn)行軌跡數(shù)組 --------------------------------------------------------- private void UpdataBulletLocus(int i) { Locus[i][1] += Locus[i][3]; Locus[i][2] += Locus[i][4]; if (Locus[i][1] < -5 || Locus[i][1] > SWidth + 5) { Locus[i][3] *= -1; } if (Locus[i][2] < -5 || Locus[i][2] > SHeight + 5) { Locus[i][4] *= -1; } } //--- 碰撞判斷 ---------------------------------------------------------------- private boolean isCollision(int planeX, int planeY, int i) { boolean result = false; // 將飛機(jī)近似的看成圓,取得其圓心坐標(biāo) int planeXCenter = planeX + 12; int planeYCenter = planeY + 12; // 同理,取得子彈的圓心坐標(biāo) int bulletXCenter = Locus[i][1] + 3; int bulletYCenter = Locus[i][2] + 3; // 理由半徑檢測,小于10即已碰撞 if (Math.abs(planeXCenter - bulletXCenter) < 10) { if (Math.abs(planeYCenter - bulletYCenter) < 10) { result = true; } } return result; }}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -