📄 bullets.java
字号:
/*
* Bullets.java
*
* Created on 2006年11月23日, 下午6:20
* 子弹操作类 -- 负责所有子弹的状态和与飞机的碰撞检测
*/
import javax.microedition.lcdui.game.*;
import javax.microedition.lcdui.*;
import java.util.Random;
/**
*
* @author TOM
*/
public class Bullets {
private ManCanvas manCanvas; //游戏画面
private int[][] Locus; //存储子弹数组
private Sprite Bullets; // 子弹图象
private Random rnd; // 随机种子
private int SWidth, SHeight; // 设备屏幕的宽度与高度
public Bullets(ManCanvas manCanvas, Image img, int SWidth, int SHeight) {
this.manCanvas = manCanvas;
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;
}
// 初始化运行轨迹
public void InitBulletsLocus() {
for (int i = 0; i < Locus.length; i++) {
InitBulletLocus(i);
}
}
// 初始化运行轨迹随机数组
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; //子弹初始X坐标值
Locus[i][2] = (rnd.nextInt() & 0x7fffffff) % SHeight; //子弹初始的Y坐标值
Locus[i][3] = (rnd.nextInt() & 0x7fffffff) % 3 + 1; //vx子弹X坐标增量
Locus[i][4] = (rnd.nextInt()) % 3; //vy子弹Y坐标增量
break;
case 1:
Locus[i][1] = SWidth + 5;
Locus[i][2] = (rnd.nextInt() & 0x7fffffff) % SHeight;
Locus[i][3] = ( (rnd.nextInt() & 0x7fffffff) % 3 + 1) * -1;
Locus[i][4] = (rnd.nextInt()) % 3;
break;
case 2:
Locus[i][1] = (rnd.nextInt() & 0x7fffffff) % SWidth;
Locus[i][2] = -5;
Locus[i][3] = (rnd.nextInt()) % 3;
Locus[i][4] = (rnd.nextInt() & 0x7fffffff) % 3 + 1;
break;
case 3:
Locus[i][1] = (rnd.nextInt() & 0x7fffffff) % SWidth;
Locus[i][2] = SHeight + 5;
Locus[i][3] = (rnd.nextInt()) % 3;
Locus[i][4] = ( (rnd.nextInt() & 0x7fffffff) % 3 + 1) * -1;
break;
}
}
// 显示子弹
public void paint(Graphics g, int planeX, int planeY) {
for (int i = 0; i < Locus.length; i++) {
if (isCollision(planeX, planeY, i, 10)) { // 碰撞判断
if (!manCanvas.isGameOver) {
manCanvas.isGameOver = true;
}
continue;
}
Bullets.setPosition(Locus[i][1], Locus[i][2]);
Bullets.paint(g);
UpdataBulletLocus(i); // 更新运行轨迹数组
}
}
// 更新运行轨迹数组
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, int range) {
boolean result = false;
int planeXCenter = planeX + 12; // 将飞机近似的看成圆,取得其圆心坐标
int planeYCenter = planeY + 12;
int bulletXCenter = Locus[i][1] + 3; // 同理,取得子弹的圆心坐标
int bulletYCenter = Locus[i][2] + 3;
if (Math.abs(planeXCenter - bulletXCenter) < range) { // 理由半径检测,小于10即已碰撞
if (Math.abs(planeYCenter - bulletYCenter) < range) {
result = true;
}
}
return result;
}
// 炸弹爆炸操作
public void BombBullets(int planeX, int planeY) {
for (int i = 0; i < Locus.length; i++) {
if (isCollision(planeX, planeY, i, 32)) {
InitBulletLocus(i);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -