Java小项目-飞机大战

初始化窗口

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);	
	}

你可能感兴趣的:(Java小项目-飞机大战)