📄 bullets.java
字号:
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import java.util.*;
public class Bullets extends GameObject {
private int bullets[][];//子弹状态数组
private int bulletstotal;//数组的length
private Random rnd;//随机数
public static final int BULLET_TYPE_LEFT=0;//子弹初始化的位置类型:分为上下左右四种
public static final int BULLET_TYPE_RIGHT=1;//
public static final int BULLET_TYPE_TOP=2;
public static final int BULLET_TYPE_BOTTOM=3;
private int width,height;//屏幕的高和宽,用于随机子弹位置
private int speed;
//private int bombadded[];
public Bullets(Image img,int picwidth,int picheight,int width,int height) {
super(img,picwidth,picheight);
rnd=new Random();
this.width=width;
this.height=height;
}
public void initBullets(int bulletspeed,int bulletstotal){//初始化子弹状态数组
bullets=new int[bulletstotal][6];
speed=bulletspeed;
for (int i = 0; i < bullets.length; i++) {
initBullet(i,bulletspeed);
}
}
private void initBullet(int i,int m) {//初始化index号子弹
bullets[i][0] = (rnd.nextInt() & 0x7fffffff) % 4; //子弹的初始方位
bullets[i][5] = 1; //alive 1表示存活, 0表示死去
switch (bullets[i][0]) {
case BULLET_TYPE_LEFT:
bullets[i][1] = 0; //横坐标
bullets[i][2] = (rnd.nextInt() & 0x7fffffff) % height; //纵坐标
bullets[i][3] = (rnd.nextInt() & 0x7fffffff) % m + 1; //横向速度
bullets[i][4] = (rnd.nextInt() & 0x7fffffff) % m + 1; //纵向速度
break;
case BULLET_TYPE_RIGHT:
bullets[i][1] = width-6;
bullets[i][2] = (rnd.nextInt() & 0x7fffffff) % height;
bullets[i][3] = ( (rnd.nextInt() & 0x7fffffff) % m + 1) * -1;
bullets[i][4] = (rnd.nextInt() & 0x7fffffff) % m + 1;
break;
case BULLET_TYPE_TOP:
bullets[i][1] = (rnd.nextInt() & 0x7fffffff) % width;
bullets[i][2] = 0;
bullets[i][3] = (rnd.nextInt() & 0x7fffffff) % m + 1;
bullets[i][4] = (rnd.nextInt() & 0x7fffffff) % m + 1;
break;
case BULLET_TYPE_BOTTOM:
bullets[i][1] = (rnd.nextInt() & 0x7fffffff) % width;
bullets[i][2] = height-6;
bullets[i][3] = (rnd.nextInt() & 0x7fffffff) % m + 1;
bullets[i][4] = ( (rnd.nextInt() & 0x7fffffff) % m + 1) * -1;
break;
}
}
public void updata(int i){//根据速度更新i子弹下一桢的位置,碰壁反弹
bullets[i][1]+=bullets[i][3];
bullets[i][2]+=bullets[i][4];
if(bullets[i][1]<0 || bullets[i][1]>width-6){
bullets[i][3]*=-1;
}
if(bullets[i][2]<0 || bullets[i][2]>height-6){
bullets[i][4]*=-1;
}
}
private void paint(Graphics g,int i){//绘画出第i个子弹
updataspritepos(i);//更新位置
sprite.paint(g);//绘画Sprtie
}
public void paint(Graphics g) {//绘画整个子弹组
for (int i = 0; i < bullets.length; i++) {
if(bullets[i][5]==0){//死去的子弹不绘画
continue;
}
sprite.setPosition(bullets[i][1],bullets[i][2]); //更新位置
sprite.paint(g);
}
}
public void refreshBullets(Sprite planesprite, boolean needcollision){//刷新子弹数组的状态,并作碰撞处理
for (int i = 0; i < bullets.length; i++) {
if(bullets[i][5]==0){ //死去的子弹不更新
continue;
}
if(needcollision){//如果需要碰撞检测
if (isCollision(planesprite, i, 10)) {//如果碰撞,进行处理
Navigate.mc.gameover = true;
Navigate.mc.explosion.sprite.setPosition(bullets[i][1], bullets[i][2]);
bullets[i][5] = 0;//杀死碰撞的子弹
continue;
}
}
updata(i);//更新状态
}
}
private boolean isCollision(Sprite sprite,int i,int range){
//判断是否碰撞
boolean result=false;
int planeXCenter=sprite.getX()+11;
int planeYCenter=sprite.getY()+11;
int bulletXCenter=bullets[i][1]+3;
int bulletYCenter=bullets[i][2]+3;
if(Math.abs(planeXCenter-bulletXCenter) < range){
if (Math.abs(planeYCenter - bulletYCenter )< range) {
result = true;
}
}
return result;
}
private void updataspritepos(int i){//将sprite更新到i字弹的位置
sprite.setPosition(bullets[i][1],bullets[i][2]);
}
public void killbullets(Sprite planesprite,int range){//杀死一定区域内的子弹
for (int i = 0; i < bullets.length; i++) {
if(bullets[i][5]!=0){//alive bullets
if(isCollision(planesprite, i, range)){
bullets[i][5]=0;
initBullet(i,speed);
}
}
}
}
///////////////////////////////////////////
public boolean addbomb(Sprite planesprite, boolean needcollision){//刷新子弹数组的状态,并作碰撞处理
for (int i = 0; i < bullets.length; i++) {
if(bullets[i][5]==0){ //死去的子弹不更新
continue;
}
if(needcollision){//如果需要碰撞检测
if (bombCollision(planesprite, 0, 12)) {//如果碰撞,进行处理
//MyGameCanvas.flashplane();
//Navigate.mc.gameover = true;
//Navigate.mc.plane.sprite.setPosition(bullets[i][1], bullets[i][2]);
bullets[0][5]=0;
return true;
}
}
updata(0);//更新状态
}
return false;
}
private boolean bombCollision(Sprite sprite,int i,int range){
//判断是否碰撞
boolean result=false;
int planeXCenter=sprite.getX()+11;
int planeYCenter=sprite.getY()+11;
int bulletXCenter=bullets[i][1]+8;
int bulletYCenter=bullets[i][2]+8;
if(Math.abs(planeXCenter-bulletXCenter) < range){
if (Math.abs(planeYCenter - bulletYCenter )< range) {
result = true;
}
}
return result;
}
///////////////////////////////////////
public void initufoBullets(int bulletspeed,int bulletstotal,int x,int y){//初始化子弹状态数组
bullets=new int[bulletstotal][6];
speed=bulletspeed;
for (int i = 0; i < bullets.length; i++) {
initufoBullet(i,bulletspeed,x,y);
}
}
private void initufoBullet(int i,int m,int x,int y) {//初始化index号子弹
bullets[i][5] = 1; //alive 1表示存活, 0表示死去
bullets[i][1] = x+19; //横坐标
bullets[i][2] = y+15; //纵坐标
bullets[i][3] = (rnd.nextInt() & 0x7fffffff) % 8 + 1; //横向速度
switch((rnd.nextInt() & 0x7fffffff) % 2){
case(0): bullets[i][3]*=-1;break;
case(1): bullets[i][3]=bullets[i][3];break;
}
bullets[i][4] = (rnd.nextInt() & 0x7fffffff) % 8 + 1; //纵向速度
switch((rnd.nextInt() & 0x7fffffff) % 2){
case(0): bullets[i][4]*=-1;break;
case(1): bullets[i][4]=bullets[i][4];break;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -