[Awt]——太空猛禽

文件目录:

   工具类:

   

public class Constant {
	public static final int GAME_WIDTH=300;//游戏中的常量
	public static final int GAME_HEIGHT=500;
	public static final int Time_lapse=50;
}
public class GameUtil {
	public static Image getImage(String path){
		BufferedImage bi=null;
		try {
			URL u=GameUtil.class.getClassLoader().getResource(path);
			bi=javax.imageio.ImageIO.read(u);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return bi;
	}
}
public class MyFrame extends Frame{
	public void launchFrame(){//加载窗口
		setSize(Constant.GAME_WIDTH,Constant.GAME_HEIGHT);
		setLocation(100,100);
		setVisible(true);
		new PaintThread().start();
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		
	}
	class PaintThread extends Thread{
		@Override
		public void run() {
			while(true){
				repaint();
				try {
					Thread.sleep(Constant.Time_lapse);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
   实体类:
   
public class Bullet extends GameObject{
	double degree;
	public Bullet(){
		degree=Math.random()*Math.PI*2;
		x=Constant.GAME_WIDTH/2;
		y=Constant.GAME_HEIGHT/2;
		width=10;
		height=10;
		speed=5;
	}
	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 (y>Constant.GAME_HEIGHT-17||y<30) {
			degree=-degree;
		}
		if (x>Constant.GAME_WIDTH-16||x<7) {
			degree=Math.PI-degree;
		}
		g.setColor(c);
	}
}
public class Explode {
	double x,y;
	static Image[] images=new Image[16];
	int count;
	static{
		for (int i = 0; i < 16; i++) {
			images[i]=GameUtil.getImage("Explode/e"+(i+1)+".gif");
			images[i].getWidth(null);
		}
	}
	public void draw(Graphics g){
		if (count<=15) {
			g.drawImage(images[count],(int)x,(int)y, null);
			count++;
		}
	}
	public Explode(double x,double y) {
		this.x=x;
		this.y=y;
	}
}
public class GameObject {
	Image image;
	double x,y;
	int speed=10;
	int width,height;
	public Rectangle getRect(){
		return new Rectangle((int)x,(int)y,width,height);
	}
	public GameObject(Image image, double x, double y, int speed, int width, int height) {
		super();
		this.image = image;
		this.x = x;
		this.y = y;
		this.speed = speed;
		this.width = width;
		this.height = height;
	}
	public GameObject(){
	}
}
public class plane extends GameObject{
	private boolean left,up,right,down;
	private boolean live=true;
	public void draw(Graphics g){
		if (live) {
			g.drawImage(image,(int)x,(int)y, null);
			move();
		}
	}
	public void move(){
		if (left) {
			x-=speed;
		}
		if (right) {
			x+=speed;
		}
		if (up) {
			y-=speed;
		}
		if (down) {
			y+=speed;
		}
	}
	public plane(String imagePath, double x, double y) {
		this.image =GameUtil.getImage(imagePath);
		this.x = x;
		this.y = y;
		this.width=image.getWidth(null);
		this.height=image.getHeight(null);
	}
	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;
		default:
			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;
		default:
			break;
		}
	}
	public plane(){
	}
	public boolean isLive() {
		return live;
	}
	public void setLive(boolean live) {
		this.live = live;
	}
}
public class PlaneGameFrame extends MyFrame{
	Image bg=GameUtil.getImage("images/bg.jpg");
	plane p=new plane("images/plane.png",50,50);
	ArrayList bulletList=new ArrayList();
	Date startTime;
	Date endTime;
	Explode bao;
	public void paint(Graphics g){
		g.drawImage(bg, 0, 0, null);
		p.draw(g);
		for (int i = 0; i < bulletList.size(); i++) {
			Bullet b =(Bullet)bulletList.get(i);
			b.draw(g);
			//检测与飞机的碰撞
			boolean peng=b.getRect().intersects(p.getRect());
			if (peng) {
				p.setLive(false);//飞机死掉
				if (bao==null) {
					bao=new Explode(p.x, p.y);
					endTime=new Date();
				}
				bao.draw(g);
				break;
		  	}
		}
		if (!p.isLive()) {
			int period=(int)((endTime.getTime()-startTime.getTime())/1000);
			printInfo(g,"时间"+period+"秒",20,20,220, Color.white);
			
			switch (period/10) {
			case 0:
			case 1:
				printInfo(g,"菜鸟",20,20,160, Color.white);
				break;
			case 2:
				printInfo(g,"小鸟",20,20,160, Color.white);
				break;
			case 3:
				printInfo(g,"鸟王子",20,20,160, Color.white);
				break;
			case 4:
				printInfo(g,"鸟人",20,20,160, Color.white);
				break;	
			default:
				break;
			}
		}
	}
	public void printInfo(Graphics g,String str,int size,int x,int y,Color color){
		Color c=g.getColor();
		g.setColor(color);
		Font f=new Font("宋体",Font.BOLD,size);
		g.setFont(f);
		g.drawString(str,x,y);
		g.setColor(c);
	}
			//双缓冲
	private Image offScreenImage=null;
			public void update(Graphics g){
				if (offScreenImage==null){ 
					offScreenImage=createImage(Constant.GAME_WIDTH,Constant.GAME_HEIGHT);
				}
				Graphics gOff=offScreenImage.getGraphics();
				paint(gOff);
				g.drawImage(offScreenImage, 0, 0, null);
				
	}
	public static void main(String[] args) {//主函数在这里
		new PlaneGameFrame().launchFrame();
	}
	public void launchFrame(){
		super.launchFrame();
		addKeyListener(new KeyMonitor());
		for (int i = 0; i <6; i++) {//生成子弹
			Bullet b=new Bullet();
			bulletList.add(b);//装入容器
		}
		startTime=new Date();
	}
	//定义为内部类,可以方便的使用外部属性
	class KeyMonitor extends KeyAdapter{
		@Override
		public void keyPressed(KeyEvent e) {
			p.addDirection(e);
		}
		@Override
		public void keyReleased(KeyEvent e) {
			p.minusDirection(e);
		}
	}
}
   效果图:










  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值