四、子弹类的创建,飞机子弹发射与无用子弹的销毁(雷霆战机)
·
GitHub地址:https://github.com/WingedCat/AirWar
子弹类的创建:
package cn.xpu.hcp.entity;
import java.awt.Graphics;
import java.awt.Image;
import java.util.Random;
import cn.xpu.hcp.game.GameFrame;
import cn.xpu.hcp.tools.Constant;
import cn.xpu.hcp.tools.GameImage;
public class Bullet {
private static Random r = new Random();
int randIndex;
private int x,y;//子弹的位置
int WIDTH,HEIGHT;//子弹的宽和高
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
private boolean good;//子弹是我还是对方(敌机)发射的;我设置为true,敌机设置为false
private boolean isAlive = true;//表明子弹是否应该消失,击中活着出界就设置为false
private int power;//子弹的杀伤力
private int speed;//子弹的移动速度
private boolean isBoss;//是否为boss机型子弹
private Direction dir;//子弹的方向,我是向上,敌方向下
private GameFrame gf;
private static Image[] myImgs=new Image[13];//存放我方子弹图片的数组
private static Image[] enemyImgs = new Image[8];//存放敌方普通机型子弹的图片
private static Image[] bossImgs = new Image[5];//存放敌方boss机型子弹的图片
private Image ensureImg;//具体确定的图片
static{
//我方飞机子弹图片
//低级子弹
myImgs[0]=GameImage.getImage("resources/m5.png");
myImgs[1]= GameImage.getImage("resources/m6.png");
myImgs[2]= GameImage.getImage("resources/m7.png");
//中级子弹
myImgs[3]= GameImage.getImage("resources/m1.png");
//高级子弹
myImgs[4]= GameImage.getImage("resources/m2.png");
myImgs[5]= GameImage.getImage("resources/m3.png");
myImgs[6]= GameImage.getImage("resources/m4.png");
myImgs[7]= GameImage.getImage("resources/m8.png");
myImgs[8]= GameImage.getImage("resources/m9.png");
//超级炮弹
myImgs[9]= GameImage.getImage("resources/m10.png");
myImgs[10]= GameImage.getImage("resources/m11.png");
myImgs[11]= GameImage.getImage("resources/m12.png");
myImgs[12]= GameImage.getImage("resources/m13.png");
//敌方普通飞机子弹图片
enemyImgs[0]=GameImage.getImage("resources/em1.png");
enemyImgs[1]= GameImage.getImage("resources/em2.png");
enemyImgs[2]= GameImage.getImage("resources/em3.png");
enemyImgs[3]= GameImage.getImage("resources/em4.png");
enemyImgs[4]= GameImage.getImage("resources/em5.png");
enemyImgs[5]= GameImage.getImage("resources/em6.png");
enemyImgs[6]= GameImage.getImage("resources/em7.png");
enemyImgs[7]= GameImage.getImage("resources/em8.png");
//boss子弹图片
//低级子弹
bossImgs[0]=GameImage.getImage("resources/bm3.png");
bossImgs[1]= GameImage.getImage("resources/bm5.png");
//高级子弹
bossImgs[2]= GameImage.getImage("resources/bm4.png");
//大招
bossImgs[3]= GameImage.getImage("resources/bm1.png");
bossImgs[4]= GameImage.getImage("resources/bm2.png");
}
public int getWIDTH() {
return WIDTH;
}
public int getHEIGHT() {
return HEIGHT;
}
public Bullet(int x,int y,int speed,int randIndex,boolean good,GameFrame gf){
this.x = x;
this.y = y;
this.speed = speed;
this.good = good;
this.gf = gf;
this.randIndex = randIndex;
if(good==true){
//开始只能是低级子弹,通过拾取宝贝升级子弹
ensureImg = myImgs[randIndex];
dir = Direction.U;
WIDTH = ensureImg.getWidth(null);
HEIGHT = ensureImg.getHeight(null);
}else{
dir = Direction.D;
if(isBoss){
//开始发射低级子弹,随机发射高级和大招
ensureImg = bossImgs[randIndex];
WIDTH = ensureImg.getWidth(null);
HEIGHT = ensureImg.getHeight(null);
}else{
ensureImg = enemyImgs[randIndex];
WIDTH = ensureImg.getWidth(null);
HEIGHT = ensureImg.getHeight(null);
}
}
}
public void draw(Graphics g){
if(!isAlive){
gf.bs.remove(this);//销毁子弹
return;//子弹消失了,不画
}
g.drawImage(ensureImg,x,y,null);
move();
}
private void move() {
switch(dir) {
case U:
y -= speed;
break;
case D:
y += speed;
break;
}
if(x < 0 || y < 0 || x > Constant.GAME_WIDTH || y > Constant.GAME_HEIGHT) {
isAlive = false;//出界就设置为false
}
}
}
飞机子弹的发射:
fire()方法:
public void fire() {
if(isAlive==false)return;//不存活,也就没开火的必要了
new PlaySound("Beam.mp3", false).start();
int xPos = -10;
int yPos = -10;
Bullet m = new Bullet(xPos, yPos,10,randIndex, good,this.gf);
m.setX(this.x + Plane.WIDTH/2 - m.getWIDTH()/2);
m.setY(this.y - m.getHEIGHT()+20);
gf.bs.add(m);//集合中添加子弹
}
public void fire(long t){//鼠标调用此方法
if(r.nextInt(40)>36)//不要每一次都开火
fire();
}
private class MouseMonitor extends MouseAdapter{
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
if(e.getModifiers()==16)//左键释放就开火
myplane.fire();
}
@Override
public void mouseMoved(MouseEvent e) {
if(useMouse){
myplane.setX(e.getX());
myplane.setY(e.getY());
}
}
@Override
public void mouseDragged(MouseEvent e) {
if(useMouse){
if(e.getModifiers()==16){
myplane.fire(0);//开火
}
myplane.setX(e.getX());
myplane.setY(e.getY());
}
}
子弹的绘制:
//绘制子弹
for(int i=0; i<bs.size(); i++) {//将集合中的子弹都绘制出来
Bullet b = bs.get(i);
b.draw(g);
}
子弹销毁:
if(x < 0 || y < 0 || x > Constant.GAME_WIDTH || y > Constant.GAME_HEIGHT) {
isAlive = false;//出界就设置为false
}
public void draw(Graphics g){
if(!isAlive){
gf.bs.remove(this);//销毁子弹
return;//子弹消失了,不画
}
g.drawImage(ensureImg,x,y,null);
move();
}
更多推荐
已为社区贡献4条内容
所有评论(0)