求助java初学者,请问怎么在飞机大战中实现爆炸效果

ckmy 2017-06-16 10:07:59
java入门小白,还在培训,用java做飞机大战游戏,现在做出的效果是子弹击中敌机两个都消失!
想达到的效果是,子弹击中敌机,产生爆炸效果。万分感谢

主程序代码:
public class ShootGame extends JPanel {

//游戏界面大小固定 400*700——常量
public static final int WIDTH=400;
public static final int HEIGHT=700;


public static BufferedImage background;
public static BufferedImage start;
public static BufferedImage airplane;
public static BufferedImage bee;
public static BufferedImage bullet;
public static BufferedImage hero0;
public static BufferedImage hero1;
public static BufferedImage pause;
public static BufferedImage gameover;
/*
* 为游戏中的角色定义数据结构
* 1个英雄机对象
* 1个存储所有敌人(包括蜜蜂和敌机)对象的数组
* 1个存储所有子弹对象的数组
*/
public Hero hero=new Hero();
/*
* 因为既要保存敌机类型对象,又要保存蜜蜂类型对象
* 用父类型定义数组
*/
public Flyer[] flyers={};//存储所有敌人对象的数组
public Bullet[] bullets={};//存储所有子弹对象的数组

//定义游戏状态:当前状态变量:默认是开始状态
private int state=START;
//定义游戏状态备选项的常量:
public static final int START=0;
public static final int RUNNING=1;
public static final int PAUSE=2;
public static final int GAME_OVER=3;

static{//静态块 仅在类首次加载到方法区时执行一次,专门加载静态资源
try {
background=ImageIO.read(ShootGame.class.getResource("background.png"));
start=ImageIO.read(ShootGame.class.getResource("start.png"));
airplane=ImageIO.read(ShootGame.class.getResource("airplane.png"));
bee=ImageIO.read(ShootGame.class.getResource("bee.png"));
bullet=ImageIO.read(ShootGame.class.getResource("bullet.png"));
hero0=ImageIO.read(ShootGame.class.getResource("hero0.png"));
hero1=ImageIO.read(ShootGame.class.getResource("hero1.png"));
pause=ImageIO.read(ShootGame.class.getResource("pause.png"));
gameover=ImageIO.read(ShootGame.class.getResource("gameover.png"));



} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

public static void main(String[] args) {

JFrame frame=new JFrame("Fly");//创建JFrame对象
frame.setSize(WIDTH, HEIGHT);
frame.setAlwaysOnTop(true);//设置窗体总在最上层,不被其他窗体挡住
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//设置窗体关闭同时,退出程序
frame.setLocationRelativeTo(null);
//设置窗体初始位置,null为居中
/*在窗体中嵌入背景面板对象-JPanel*/
ShootGame game=new ShootGame();
frame.add(game);//将背景面板对象嵌入窗体对象
/*窗体默认不可见!必须显示调用setVisible方法才能显示出窗体*/
frame.setVisible(true);//自动调用窗体的paint方法
game.action();
}
/**
* 游戏启动时的动作
*/
public void action(){
//定义鼠标监听
MouseAdapter mouse=new MouseAdapter() {

@Override
public void mouseMoved(MouseEvent e) {
if(state==RUNNING){
int x=e.getX();
int y=e.getY();
hero.move(x, y);
}
}

@Override
public void mouseClicked(MouseEvent e) {
if(state==START){
state=RUNNING;
}else if(state==GAME_OVER){
state=START;
hero=new Hero();
bullets=new Bullet[0];
flyers=new Flyer[0];

}
}

@Override
public void mouseExited(MouseEvent e) {
if(state==RUNNING){
state=PAUSE;
}
}

@Override
public void mouseEntered(MouseEvent e) {
if(state==PAUSE){
state=RUNNING;
}
}




};
this.addMouseListener(mouse);
this.addMouseMotionListener(mouse);

//设置定时器
Timer timer=new Timer();

timer.scheduleAtFixedRate(new TimerTask() {
private int runTimes=0;
@Override
public void run() {
if(state==RUNNING){
runTimes++;
if(runTimes%50==0){
nextOne();
}
for(int i=0;i<flyers.length;i++){
flyers[i].step();
}

if(runTimes%30==0){
shooting();
}
for(int i=0;i<bullets.length;i++){
bullets[i].step();
}
outOfBound();
hit();
//bang();
hero.step();
}
repaint();

}
}, 10, 10);
}
/**
* 越界检测
*/
public void outOfBound(){
Flyer[] flives=new Flyer[flyers.length];
int index=0;
for(int i=0;i<flyers.length;i++){
if(!flyers[i].outOfbound()){
flives[index]=flyers[i];
index++;
}
}
flyers=Arrays.copyOf(flives, index);
index=0;
Bullet[] blives=new Bullet[bullets.length];
for(int j=0;j<bullets.length;j++){
if(!bullets[j].outOfbound()){
blives[index]=bullets[j];
index++;
}
}
bullets=Arrays.copyOf(blives, index);
}


/**
* 检测英雄机是否与飞行物碰撞
*/
public void hit(){
Flyer[] lives=new Flyer[flyers.length];
int index=0;
for(int i=0;i<flyers.length;i++){
if(!hero.hit(flyers[i])){
lives[index]=flyers[i];
index++;
}
}
if(hero.getLife()==0){
state=GAME_OVER;
}
flyers=Arrays.copyOf(lives, index);
}

/**
* 检测子弹是否击中飞行物
*/
public void bang(){
for(int i=0;i<bullets.length;i++){
for(int j=0;j<flyers.length;j++){
if(Flyer.bang(bullets[i], flyers[j])){
hero.getScore_Award(flyers[j]);
flyers[j]=flyers[flyers.length-1];
flyers=Arrays.copyOf(flyers, flyers.length-1);
bullets[i]=bullets[bullets.length-1];
bullets=Arrays.copyOf(bullets, bullets.length-1);
i=0;
break;
}
}
}
}
/**
* 生成子弹
*/
public void shooting(){
Bullet[] newBullet=hero.shoot();
bullets=Arrays.copyOf(bullets, bullets.length+newBullet.length);
System.arraycopy(newBullet, 0, bullets, bullets.length-newBullet.length, newBullet.length);
}

/**
* 随机产生飞行物
*/
public void nextOne(){
Random random=new Random();
Flyer f=null;
if(random.nextInt(20)==0){
f=new Bee();
}else{
f=new Airplane();
}
flyers=Arrays.copyOf(flyers, flyers.length+1);
flyers[flyers.length-1]=f;
}


/**
* 绘制背景和飞行物
*/

@Override
public void paint(Graphics g) {
g.drawImage(background, 0, 0, null);
paintHero(g);
paintFly(g);
paintBullets(g);
paintScore_Life(g);
if(state==START){
g.drawImage(start, 0, 0, null);
}else if(state==PAUSE){
g.drawImage(pause, 0, 0, null);
}else if(state==GAME_OVER){
g.drawImage(gameover, 0, 0, null);
}
}
/**
* 绘制英雄机
*/
public void paintHero(Graphics g){
g.drawImage(hero.image,hero.x, hero.y , null);
}
/**
* 绘制飞行物
*/
public void paintFly(Graphics g){
for(int i=0;i<flyers.length;i++){
g.drawImage(flyers[i].image,flyers[i].x,flyers[i].y, null);
}
}
/**
* 绘制子弹
*/
public void paintBullets(Graphics g){
for(int i=0;i<bullets.length;i++){
g.drawImage(bullets[i].image, bullets[i].x, bullets[i].y, null);
}
}
/**
* 绘制左上角的生命,分数
*/
public void paintScore_Life(Graphics g){
int x=10;
int y=15;
Font font=new Font(Font.SANS_SERIF,Font.BOLD,14);
g.setFont(font);
g.drawString("Score :"+hero.getScore(), x, y);
y+=20;
g.drawString("Life :"+hero.getLife(), x, y);
y+=20;
g.setColor(Color.red);
g.drawRect(x, y, 60, 10);
if(hero.getLife()>=3){
g.fillRect(x, y, 60, 10);
}else if(hero.getLife()==2){
g.fillRect(x, y, 40, 10);
}else if(hero.getLife()==1){
g.fillRect(x, y, 20, 10);
}else{
g.fillRect(x, y, 0, 0);
}
Font scoreFont=new Font(Font.SANS_SERIF,Font.BOLD,33);
g.setFont(scoreFont);
if(state==GAME_OVER){
g.drawString("总 得 分 : "+hero.getScore(), 100, 350);

}
}
}
...全文
1875 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
木易小次郎 2019-04-24
  • 打赏
  • 举报
回复
这飞机大战和坦克大战基本是同一个思路和搞法哈,准备好十来张爆炸图片,不同大小的,然后依次渲染,弄完最后一针就结束,还可以搞点爆破声音啥的~
WwwanQing 2019-04-22
  • 打赏
  • 举报
回复
用Image数组,爆炸效果的每一帧都当作一张图片,在静态代码块中完成数组的初始化,在检测炮弹与飞机的碰撞之后调用draw方法

	int count = 0;
	public void draw(Graphics g) {
		if(count<=15) {
			g.drawImage(bangImages[count], (int)x,  (int)y, null);
			count++;
		}
	}
戎码一生灬 2017-06-19
  • 打赏
  • 举报
回复
一看就是达内的,我可是你的学长啊,呵呵
reupe 2017-06-17
  • 打赏
  • 举报
回复
子弹击中飞机消失你已经会了,爆炸效果就很容易了。准备一张爆炸效果的图片,当子弹击中飞机时,就把飞机对象的图片换成爆炸效果的图,然后再消失。就可以了。
ckmy 2017-06-17
  • 打赏
  • 举报
回复
为什么这么说
ckmy 2017-06-17
  • 打赏
  • 举报
回复
引用 1 楼 soton_dolphin 的回复:
你的这个培训班就是坑人的啊,退学吧
为什么这么说,不是科班出身,不培训没什么好办法
soton_dolphin 2017-06-17
  • 打赏
  • 举报
回复
你的这个培训班就是坑人的啊,退学吧

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧