(附素材)飞机大战


public class Constant {
	public static final int GAME_WIDTH = 400;
	public static final int GAME_HEIGHT = 400;
}
import java.awt.*;
/**
 * 爆炸类
 * @author Administrator
 *
 */
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;
	
	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;
	}
}
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
/**
 * 游戏物体的父类
 * @author Administrator
 *
 */
public class GameObject {
	 Image img;
	 double y;
	 double x;
	 int speed=20;
	 int width,height;
	
	public void drawSelf(Graphics g) {
		g.drawImage(img, (int)x,(int)y,null);
	}

	public GameObject(Image img, double y, double x, int speed, int width, int height) {
		super();
		this.img = img;
		this.y = y;
		this.x = x;
		this.speed = speed;
		this.width = width;
		this.height = height;
	}

	public GameObject(Image img, double y, double x) {
		super();
		this.img = img;
		this.y = y;
		this.x = x;
	}
	public GameObject() {
		
	}
	/**
	 * 返回物体所在的矩形,便于后续的碰撞检测
	 * @return
	 */
	public Rectangle getRect() {
		return new Rectangle((int)x,(int)y,width,height);
	}
}

 



import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;

class GameUtil {
	private GameUtil() {
		//工具类最好将构造器私有
	}
/**
 * 指定路径的图片对象	
 * @param path
 * @return
 */
	public static Image getImage(String path) {
		BufferedImage bi =null;
		try {
			URL u =GameUtil.class.getClassLoader().getResource(path);
			bi = ImageIO.read(u);
		}catch (IOException e) {
			e.printStackTrace();
		}
		return bi;
	}
}

import java.awt.*;
import java.awt.event.*;
import java.util.Date;

/**
 * 飞机游戏的主窗口
 * @author Administrator
 *
 */
public class MyGameFrame extends Frame{
	Image planeImg = GameUtil.getImage("images/plane.png");
	Image bg = GameUtil.getImage("images/bg.jpg");
	
	Plane plane = new Plane(planeImg,10,100);
	Shell[] shells = new Shell[50];
	
	Explode bao;
	Date startTime = new Date();
	Date endTime;
	
	int period;//游戏持续的时间 
	
	@Override
	//自动被系统调用
	//帮助我们做 窗口的绘制,自动被动用,g这个变量相当于画笔,我们想画的内容都是通过g这个画笔来画的
	public void paint(Graphics g) {
		//清除黑色背景
		g.clearRect(0, 0, this.getWidth(), this.getHeight());
		Color   c =  g.getColor();
		g.drawImage(bg, 0,0,null);
		plane.drawSelf(g);
		
		//飞机和炮弹的碰撞检测
		for(int i=0;i
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;

public class Plane extends GameObject{
	
	boolean left,up,right,down;
	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;
		}			
		}
	}
	
	public Plane(Image img,double x, double y) {
		this.img = img;
		this.x = x;
		this.y = y;
		this.speed = 5;
		this.width = img.getWidth(null);
		this.height = img.getHeight(null);
	}
	
	//按下
	public void addDirection(KeyEvent e) {
		System.out.println("####"+e.getKeyCode());
		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) {
		System.out.println("####"+e.getKeyCode());
		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;
		}
	}
}
import java.awt.Color;
import java.awt.Graphics;

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_HEIGHT-height){
			degree  = - degree;
		}
		
		g.setColor(c);
	}
	
}

 

背景素材: 

(附素材)飞机大战_第1张图片

飞机素材:

爆炸素材:

(需将后缀名改为gif格式)(其实质是利用了连载做出了效果)

(附素材)飞机大战_第2张图片

(附素材)飞机大战_第3张图片

 (附素材)飞机大战_第4张图片

(附素材)飞机大战_第5张图片

(附素材)飞机大战_第6张图片

(附素材)飞机大战_第7张图片

(附素材)飞机大战_第8张图片

(附素材)飞机大战_第9张图片

(附素材)飞机大战_第10张图片

你可能感兴趣的:(Java)