Java小项目-飞机大战

本文详细介绍了使用Java开发飞机大战游戏的过程,包括初始化窗口、设置画笔、利用GameUtil加载图片、采用多线程刷新画面、面向对象设计飞机类、实现窗口监听和键盘控制飞机移动、炮弹的飞行和边界判断、双缓冲技术消除闪烁、物体碰撞检测以及爆炸效果和计时功能的添加。
摘要由CSDN通过智能技术生成

初始化窗口

public class MyGameFrame extends JFrame{
	/**
	 * 初始化窗口
	 */
	public void launchFrame(){
		this.setTitle("FYX");
		this.setVisible(true);
		this.setSize(500,500);
		this.setLocation(300, 300);
		
		this.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
	}
	public static void main(String[] args) {
		MyGameFrame f=new MyGameFrame();
		f.launchFrame();
	}

}

2.设置自动被调用的画笔

public void paint(Graphics g) {		//自动被调用。  g相当于一只画笔
		Color   c = g.getColor();
		Font   f =  g.getFont();
		g.setColor(Color.BLUE); 
		
		g.drawLine(100, 100, 300, 300);
		g.drawRect(100, 100, 300, 300);
		g.drawOval(100, 100, 300, 300);
		g.fillRect(100, 100, 40, 40);
		g.setColor(Color.red);
		g.setFont(new  Font("宋体",Font.BOLD,50));
		g.drawString("我是谁?", 200, 200);
		
		g.drawImage(ball, 250, 250, null);
		
		g.setColor(c);
		g.setFont(f);
	}

3.引入GameUtil类通过图片路径获得图片;
引入多线程多次重复画窗口

  Image   plane  = GameUtil.getImage("images/plane.png");
  Image   bg  = GameUtil.getImage("images/bg.jpg");


//帮助我们反复的重画窗口!
  class  PaintThread  extends  Thread  {
  	@Override
  	public void run() {
  		while(true){
  			repaint();		//重画
  			
  			try {
  				Thread.sleep(40);   	//1s=1000ms
  			} catch (InterruptedException e) {
  				e.printStackTrace();
  			}		
  		}
  	}
  	
  }

4.面向对象思想构造飞机:设置游戏物体基类,不同物体可以根据自己的需求去重写方法

/**
 * 游戏物体的父类
 * @author LENOVO
 *
 */
public class GameObject {
	Image img;
	double x,y;
	int speed;
	int width,height;
	public void drawSelf(Graphics g) {
		g.drawImage(img,(int)x,(int)y,null);
	}
	public GameObject(Image img, double x, double y, int speed, int width, int height) {
		super();
		this.img = img;
		this.x = x;
		this.y = y;
		this.speed = speed;
		this.width = width;
		this.height = height;
	}
	public GameObject(Image img, double x, double y) {
		super();
		this.img = img;
		this.x = x;
		this.y = y;
	}
	public GameObject() {
		
	}
	/**
	 *返回物体所在矩形,便于后续碰撞检测
	 * @return
	 */
	public Rectangle getRect() {
		return new Rectangle((int)x,(int)y,width,height);
	}

}

 Plane plane=new Plane(planeImage,250,250);
	
	@Override
	public void paint(Graphics g) {		//自动被调用。  g相当于一只画笔
		
		g.drawImage(bg, 0, 0, null);
		plane.drawSelf(g);//画飞机
	}

5.增加窗口监听,构造内部类

class KeyMonitor extends KeyAdapter{

		@Override
		public void keyPressed(KeyEvent e) {
			
			super.keyPressed(e);
		}

		@Override
		public void keyReleased(KeyEvent e) {
			
			super.keyReleased(e);
		}
		
	}

6.键盘控制飞机运动方向:增加键盘监听的内部类;plane增加按下,松开方法,在画的函数更改飞机运行方向

//按下某个键,增加相应的方向
	public void addDirection(KeyEvent e) {
		switch(e.getKeyCode()) {
		case KeyEvent.VK_LEFT:
			left=true;
			break;
		case KeyEvent.VK_UP:
			up=true;
			break;
		case KeyEvent.VK_RIGHT:
			right=true;
			break;
		case KeyEvent.VK_DOWN:
			down=true;
			break;
		}
	}
	//按下某个键,取消相应的方向
		public void minusDirection(KeyEvent e) {
			switch(e.getKeyCode()) {
			case KeyEvent.VK_LEFT:
				left=false;
				break;
			case KeyEvent.VK_UP:
				up=false;
				break;
			case KeyEvent.VK_RIGHT:
				right=false;
				break;
			case KeyEvent.VK_DOWN:
				down=false;
				break;
			}
		}
/*重写方法*/
	public void drawSelf(Graphics g) {
		g.drawImage(img,(int)x,(int)y,null);
		if(left) {
			x-=speed;
		}
		if(right) {
			x+=speed;
		}
		if(up) {
			y-=speed;
		}
		if(down) {
			y+=speed;
		}
			}
//增加键盘监听的内部类
	class KeyMonitor extends KeyAdapter{

		@Override
		public void keyPressed(KeyEvent e) {
			plane.addDirection(e);
		}

		@Override
		public void keyReleased(KeyEvent e) {
			
		plane.minusDirection(e);
		}
		
	}

7.炮弹以任意方向飞行
Constant记录常量,shell判断是否越界进行翻转

package cn.sxt.game;

public class Constant {
  public static final int GAME_WIDTH=500;
  public static final int GAME_HEGHT=500;

}
/**
 * 炮弹类
 * @author LENOVO
 *
 */

public class Shell extends GameObject{
	double degree;
	
	public Shell() {
		x=200;
		y=200;
		width=10;
		height=10;
		speed=3;
		
		degree=Math.random()*Math.PI*2;
	}
	public void draw(Graphics g) {
		Color c=g.getColor();
		g.setColor(Color.YELLOW);
		
		g.fillOval((int)x, (int)y, width, height);
		
		//炮弹沿着任意角度去飞
		x+=speed*Math.cos(degree);
		y+=speed*Math.sin(degree);
		
		if(x<0||x>Constant.GAME_WIDTH-width) {
				degree=Math.PI-degree;
			}
		if(y<30||y>Constant.GAME_HEGHT-height) {
			degree=-degree;
		}
		
		
		g.setColor(c);
	}

}

8.初始化很多炮弹:初始化窗口时就初始化炮弹,画炮弹用数组形式遍历

Shell[]   shells = new Shell[50];

//画出所有的炮弹
		for(int i=0;i<shells.length;i++){
			shells[i].draw(g);
	//初始化50个炮弹
		for(int i=0;i<shells.length;i++){
			shells[i] = new Shell();
		}

extends frame和增加双缓冲,解决闪烁问题

public void update(Graphics g) {
	    if(offScreenImage == null)
	        offScreenImage = this.createImage(Constant.GAME_WIDTH,Constant.GAME_HEGHT);//这是游戏窗口的宽度和高度
	     
	    Graphics gOff = offScreenImage.getGraphics();
	    paint(gOff);
	    g.drawImage(offScreenImage, 0, 0, null);
	}

物体碰撞检测(记得要设置Plane中的长宽高

	//飞机和炮弹的碰撞检测!!!
			boolean  peng = shells[i].getRect().intersects(plane.getRect());
			if(peng){
				plane.live = false;
			}

	boolean live=true;
	
	/*重写方法*/
	public void drawSelf(Graphics g) {
		if(live) {
		g.drawImage(img,(int)x,(int)y,null);
		if(left) {
			x-=speed;
		}
		if(right) {
			x+=speed;
		}
		if(up) {
			y-=speed;
		}
		if(down) {
			y+=speed;
		}
		}

9.发生爆炸

package cn.sxt.game;

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


public class Explode {
	double x, y;//爆炸位置
	
	static Image[] imgs = new Image[16];//只声明一次,避免反复new,浪费空间
	static {
		for (int i = 0; i < 16; i++) {
			imgs[i] = GameUtil.getImage("images/explode/e" + (i + 1) + ".gif");
			imgs[i].getWidth(null);
		}
	}//静态初始化块

	int count;

	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;
	}
}
//飞机和炮弹的碰撞检测!!!
			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);
			}

10.计时功能
startTime在声明师new,endTine在发生碰撞的时候new.

//飞机和炮弹的碰撞检测!!!
			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();
				    peroid=(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("时间"+peroid+"秒",(int) plane.x,(int)plane.y);
			}
			
		}
		
	g.setColor(c);	
	}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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; //修改当前状态为死了的 } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值