📄 bullet.java
字号:
package chapter_11;
import javax.microedition.lcdui.* ;
import javax.microedition.lcdui.game.* ;
import java.util.Random;
public class Bullet extends Sprite{ //子弹类
boolean alive; //是否出边界
public int w,h; //子弹像素
double speed=2,mapx,mapy; //子弹速度和屏幕边界
private double bx,by; //子弹坐标
public double dx,dy; //子弹速度的分向量
private double degree; //子弹角度
public Random ran; //子弹位置
public Bullet(Image img,int w,int h,int feix,int feiy,int mapx,int mapy){ //构造方法
super(img,w,h); //父类构造方法
this.w=w; //设置像素大小
this.h=h; //设置像素大小
this.mapx=mapx;
this.mapy=mapy;
ran=new Random(); //产生随机数
this.reset_Bullet(feix,feiy);
}
public int get_x(){
return (int)bx;
}
public int get_y(){
return (int)by;
}
public void reset_Bullet(int aget_x,int aget_y){//初始化子弹
alive=true; //子弹未出屏幕
if(ran.nextDouble()>=0.5) //设置子弹出现方位和射出方向
{
bx=ran.nextDouble() * (mapx - w);
if(ran.nextDouble() >= 0.5){
by = 0;
degree = Float11.acos((aget_x - bx) /
Math.sqrt((bx - aget_x) * (bx-aget_x) + (by - aget_y) * (by - aget_y))) +
3.141592654 + 0.5235987757 / 3 * (ran.nextDouble() * 2 - 1.0);
}else{
by = (double)mapy - h;
degree = Float11.acos((bx - aget_x) /
Math.sqrt((bx - aget_x) * (bx-aget_x) + (by - aget_y) * (by - aget_y))) +
6.283185308 + 0.5235987757 / 3 * (ran.nextDouble() * 2 - 1.0);
}
}else{
by=ran.nextDouble() * (mapy - h);
if(ran.nextDouble() >= 0.5){
bx = 0;
degree = Float11.asin((aget_y - by) /
Math.sqrt((bx - aget_x) * (bx - aget_x) + (by-aget_y) * (by-aget_y))) +
3.141592654 + 0.5235987757 / 3 * (ran.nextDouble() * 2 - 1.0);
}else{
bx = (double)mapx - w;
degree = Float11.asin((by - aget_y) /
Math.sqrt((bx - aget_x) * (bx - aget_x) + (by - aget_y) * (by - aget_y))) +
6.283185308 + 0.5235987757 / 3 * (ran.nextDouble() * 2 - 1.0);
}
}
setPosition(get_x(),get_y());
dx = -Math.cos(degree) * speed; //设置X方向上的速度
dy = -Math.sin(degree) * speed; //设置Y方向上的速度s
}
public boolean alived(){
if(bx > mapx - w || bx < 0 || by > mapy - h || by < 0)
return false;
else
return true;
}
public void Bulletmove(){ //子弹移动
bx += dx;
by += dy;
setPosition(get_x(),get_y());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -