java基础 小游戏 飞机大战(下)

14 爆炸类,图片数组轮播处理

通过新建一个爆炸类,加载e1.gif到e16.gif的图片,当碰撞发生时再循环画出,就完成了游戏结束飞机爆炸的效果
在这里插入图片描述
新建一个爆炸类,通过GameUtil来加载图片,碰撞时轮播16张图片

package cn.coisini.game;

import java.awt.Graphics;
import java.awt.Image;

public class Explode {
	
	double x,y; //爆炸位置
	static Image[] imgs = new Image[16];
	
	//静态初始化块循环加载图片
	static {
		for(int i=0; i<16; i++) {
			imgs[i] = GameUtil.getImage("images/explode/e" + (i+1) + ".gif");
			imgs[i].getWidth(null);
		}
	}
	
	int count;
	//通过count来依序轮播16张图片
	public void draw(Graphics g) {
		if(count <= 15) {
			g.drawImage(imgs[count], (int)x, (int)y, null);
			count++;
		}
	}
	
	
	public Explode(double x, double y) {
		this.x = x;
		this.y = y;
	}
		
}

15 主窗口内画出爆炸类

完成爆炸类后,在主窗口内实现碰撞爆炸,先声明爆炸类,之后在碰撞检测中当碰撞时,在飞机碰撞的地方新建一个爆炸类,并调用爆炸类中的draw方法,轮播爆炸效果图

/**
 * @author coisini1999
 * @飞机游戏的主窗口
 */

package cn.coisini.game;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;


import java.awt.Frame;

public class MyGameFrame extends Frame {
	
	Image bg = GameUtil.getImage("images/bg.jpg"); //加载图片对象,指定路径
	Image planeImg = GameUtil.getImage("images/plane.png"); //加载图片对象,指定路径
	
	Plane plane = new Plane(planeImg, 250 ,250); //创建飞机对象
	
	
	Shell[] shells = new Shell[50]; //创建炮弹数组
	
	Explode bao; //声明爆炸对象

	public void paint(Graphics g) { //自动被调用,g 相当于一支笔
		
		g.drawImage(bg, 0, 0, null);
		
		plane.drawSelf(g); //画飞机
		
		//画50个炮弹
		for(int i=0; i < shells.length; i++) {
			shells[i].draw(g);
			
			//每画出一个炮弹进行炮弹和飞机的矩形相交检测
			boolean peng = shells[i].getRect().intersects(plane.getRect());
			
			//碰撞后令飞机死亡
			if(peng) {
				plane.live = false;
				
				//只在第一次碰撞时生成爆炸对象
				if(bao == null) {
					bao = new Explode(plane.x, plane.y);
				}
				
				bao.draw(g);
				
			}
		}
			
	}

在碰撞检测中 if(bao == null) 这个条件是必要的,如果没有该条件,那么会一直生成爆炸对象,draw的时候无法实现轮播

16 飞机死亡和计时

游戏时希望新增一个计时功能,可以清晰看到玩了多长时间,这个功能的核心有两点:
1,时间计算,即当前时间 - 游戏开始的时间
2,显示时间窗口

在初始化窗口时,就保存一个起始时间,当飞机死亡时,保存一个结束时间

创建起始时间,声明结束时间:

public class MyGameFrame extends Frame {
	
	Image bg = GameUtil.getImage("images/bg.jpg"); //加载图片对象,指定路径
	Image planeImg = GameUtil.getImage("images/plane.png"); //加载图片对象,指定路径
	
	Plane plane = new Plane(planeImg, 250 ,250); //创建飞机对象
	Shell[] shells = new Shell[50]; //创建炮弹数组
	Explode bao; //声明爆炸对象
	
	Date startTime = new Date(); //新建起始时刻
	Date endTime; //声明结束时刻
	int period; //游戏持续时间

显示时间:

public void paint(Graphics g) { //自动被调用,g 相当于一支笔
		Color c =g.getColor();
		
		g.drawImage(bg, 0, 0, null);
		
		plane.drawSelf(g); //画飞机
		
		//画50个炮弹
		for(int i=0; i < shells.length; i++) {
			shells[i].draw(g);
			
			//每画出一个炮弹进行炮弹和飞机的矩形相交检测
			boolean peng = shells[i].getRect().intersects(plane.getRect());
			
			//碰撞后令飞机死亡
			if(peng) {
				plane.live = false;
				
				//只在第一次碰撞时生成爆炸对象
				if(bao == null) {
					bao = new Explode(plane.x, plane.y);
					
					endTime = new Date(); //新建结束时间
					period = (int)((endTime.getTime() - startTime.getTime())/1000);
				}
				
				bao.draw(g);
			}
			
			//计时功能,给出提示
			if(!plane.live) {
				g.setColor(Color.red);
				Font f = new Font("宋体",Font.BOLD,50);
				g.setFont(f);
				g.drawString("时间:" + period + "秒", (int)plane.x, (int)plane.y);
			}
			
		}
		
		g.setColor(c);
			
	}

在这里插入图片描述

17 总结

实践出真知

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
public abstract class FlyingObject { public static final int LIFE = 0; //活着呢 public static final int DEAD = 1; //死了的(先爆破) public static final int REMOVE = 2; //删除了(爆破后删除) protected int state = LIFE; //当前状态(默认活着) protected int width; //宽 protected int height; //高 protected int x; //x坐标 protected int y; //y坐标 /** 无参构造方法 */ public FlyingObject(){ } /**专门给小敌机、大敌机、小蜜蜂提供的构造方法 */ public FlyingObject(int width,int height){ this.width = width; this.height = height; Random rand = new Random(); x = rand.nextInt(World.WIDTH-width); //x:0到(窗口-小敌机的宽)之间的随机数 y = -height; //y:负的小敌机的高 } /** 专门给英雄机、子弹、天空提供的构造方法 */ public FlyingObject(int width,int height,int x,int y){ this.width = width; this.height = height; this.x = x; this.y = y; } /** 读取图片 */ public static BufferedImage loadImage(String fileName){ try{ BufferedImage img = ImageIO.read(FlyingObject.class.getResource(fileName)); return img; }catch(Exception e){ e.printStackTrace(); throw new RuntimeException(); } } /** 飞行物移动了 */ public abstract void step(); public abstract BufferedImage getImage(); /** 判断是否活着 */ public boolean isLife(){ return state==LIFE; } /** 判断是否死了的 */ public boolean isDead(){ return state==DEAD; } /** 判断是否删除的 */ public boolean isRemove(){ return state==REMOVE; } /** 画对象 g:画笔 */ public void paintObject(Graphics g){ g.drawImage(getImage(), x, y, null); } /** 检测飞行物是否越界 */ public abstract boolean outOfBounds(); /** 敌人与子弹/英雄机的碰撞 this:敌人 other:子弹或英雄机 */ public boolean hit(FlyingObject other){ int x1 = this.x-other.width; //x1:敌人的x int x2 = this.x+this.width; //x2:敌人的x int y1 = this.y-other.height; //y1:敌人的y int y2 = this.y+this.height; //y2:敌人的y int x = other.x; //x:子弹的x int y = other.y; //y:子弹的y return x>=x1 && x=y1 && y<=y2; } public void goDead(){ state = DEAD; //修改当前状态为死了的 } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值