尚学堂java300集飞机小游戏实战

/*“##########”为不同类的分界*/
package game.plane04;
import java.awt.Color;
import java.awt.Frame;
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.util.Date;


/**
 * 
* @project_name:MyGame   
* @type_name:MyGameFrame  
* @version:2019年4月22日 上午11:53:52 
* @author:zhagnqiang 
* @class_description:飞机游戏主窗口
*
 */

public class MyGameFrame extends Frame{
	
	//加载图像
    Image planeimg=GameUtil.getImage("images/plane.png");
    Image bg=GameUtil.getImage("images/bg.jpg");
    
    Plane plane=new Plane(planeimg,400,400);
//	int x=250,y=250;
    Shell[] shells=new Shell[30];
    Explode bao;
    
    Date startTime=new Date();
    Date endTime;
    int period;//游戏持续时间

	@Override
	public void paint(Graphics g) {//paint自动被调用,g相当于一只画笔

		//绘制图像
		g.drawImage(bg, 0, 0, null);
		plane.drawSeft(g);//画飞机
		
		for (int i = 0; i < shells.length; i++) {
			shells[i].draw(g);//画炮弹
			
			//碰撞检测
			boolean peng=shells[i].getRect().intersects(plane.getRect());
			if (peng) {
				System.out.println("相撞了");
				plane.live=false;				
				if(bao==null) {//只检测一次碰撞
					bao=new Explode(plane.imgX, plane.imgY);
					bao.draw(g);
					
					endTime=new Date();
					period=(int)(endTime.getTime()-startTime.getTime())/1000;
				}				


			}
			if (!plane.live) {
				Color color=g.getColor();
				g.setColor(Color.white);
				g.drawString("游戏持续时间:"+period+"s", (int)plane.imgX, (int)plane.imgY);
				g.setColor(color);
			}

		}
		
//		g.drawImage(planeimg, x, y, null);
//		x++;

	}
	
	//JFrame还是有点闪烁问题,这里使用Frame利用双缓冲的解决闪烁问题
	private Image offScreenImage=null;
	
	public void update(Graphics g) {//update自动调用
		if (offScreenImage==null) {
			offScreenImage=this.createImage(Constant.GAME_WEIGHT,Constant.GAME_HEIGHT);
		}
		Graphics gOffGraphics=offScreenImage.getGraphics();
		paint(gOffGraphics);
		g.drawImage(offScreenImage, 0, 0, null);
		
	}
	
	class PaintThread extends Thread{//内部类,帮助我们重复画窗口
		@Override
		public void run() {
			while(true) {
//				System.out.println("窗口重画...");
				repaint();//重画
				try {
					Thread.sleep(40);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	//定义键盘监听内部类
	class KeyMonitor extends KeyAdapter{
		@Override
		public void keyPressed(KeyEvent e) {
			plane.addDirection(e);
		}
		@Override
		public void keyReleased(KeyEvent e) {
			plane.minusDirection(e);
		}
	}
    /*        初始化窗口                  */
	public void launchFrame() {//画窗口
		this.setTitle("@@飞机游戏小练习@@");
		this.setLocation(100,100);
		this.setSize(Constant.GAME_WEIGHT,Constant.GAME_HEIGHT);
		this.setVisible(true);//默认不可见
		
		this.addWindowListener(new WindowAdapter() {//用于接收窗口事件的侦听器接口。
			@Override
			public void windowClosing(WindowEvent e) {//方法重写,实现“关闭窗口时程序自动关闭”的功能
				System.exit(0);
			}
		});
		
		new PaintThread().start();//启动重画窗口
		addKeyListener(new KeyMonitor());//给窗口增加键盘监听
		
		for (int i = 0; i < shells.length; i++) {//初始化50个炮弹
			shells[i]=new Shell();
		}
	}
	

	
	
	public static void main(String[] args) {
		MyGameFrame start=new MyGameFrame();
		start.launchFrame();
		
	}
}
#####################################################################################################

``package game.plane04;
/*
 * 定义一些常量
 */
public class Constant {
	public static final int GAME_WEIGHT=500;
	public static final int GAME_HEIGHT=500;

}
#####################################################################################################
package game.plane04;
/*
 * 工具类,用于加载图像
 */
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;

public class GameUtil {
	
	private GameUtil() {}
	
	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;
	}
}
#############################################################################################
package game.plane04;
/*
*飞机类
*/
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;

public class Plane extends GameObject{
	boolean left,right,up,down;
	int speed=3;
	
	boolean live=true;
	
	public void drawSeft(Graphics g) {
//		System.out.println("Plane:"+imgX);
		if (live) {
			g.drawImage(imgImage, (int)imgX, (int)imgY, null);

			if (left) {
				imgX-=speed;
			}
			if (right) {
				imgX+=speed;
			}
			if (up) {
				imgY-=speed;
			}
			if (down) {
				imgY+=speed;
			}
		}else {
			System.out.println("飞机爆炸");
		}

		
	}
	
	//按下某个键增加相应的方向
	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;

		default:
			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;	
	}
	}
	public Plane(Image imgImage, double imgX, double imgY) {
		this.imgImage=imgImage;
		this.imgX = imgX;
		this.imgY = imgY;
		
		this.imgWidth=imgImage.getWidth(null);
		this.imgHeight=imgImage.getHeight(null);
	}

}
############################################################################################
package game.plane04;

import java.awt.Color;
import java.awt.Graphics;

/*
 * 炮弹类
 */
public class Shell extends GameObject{
	double degree;
	
	public Shell() {
		imgX=200;
		imgY=200;
		imgWidth=10;
		imgHeight=3;
		speed=5;
		
		degree=2*Math.PI*Math.random();
	}
	
	public void draw(Graphics g) {
		Color color=g.getColor();
		g.setColor(Color.YELLOW);
		
		g.fillOval((int)imgX, (int)imgY, imgWidth, imgHeight);
		imgX+=speed*Math.cos(degree);
		imgY+=speed*Math.sin(degree);
		
		if(imgY>Constant.GAME_HEIGHT+25-1.5||imgY<25+1.5) {//标题栏高度40
			degree=-degree;
		}

		if(imgX>Constant.GAME_WEIGHT-10+5||imgX<5) {
			degree=Math.PI-degree;
		}
		
		g.setColor(color);
	}

}
#########################################################################################
package game.plane04;
/*爆炸类*/
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;
	
	public void draw(Graphics g) {
		if (count<=15) {
			g.drawImage(imgs[count], (int)x, (int)y, null);
			count++;
		}
	}

	public Explode(double x, double y) {
		super();
		this.x = x;
		this.y = y;
	}
	


}
############################################################################################
package game.plane04;
/*
 * 游戏物体的父类
 */

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

public class GameObject {
	Image imgImage;
	double imgX,imgY;
	int speed;
	int imgWidth,imgHeight;
	
	public GameObject() {
		
	}
	public GameObject(Image imgImage, double imgX, double imgY, int speed, int imgWidth, int imgHeight) {
		super();
		this.imgImage = imgImage;
		this.imgX = imgX;
		this.imgY = imgY;
		this.speed = speed;
		this.imgWidth = imgWidth;
		this.imgHeight = imgHeight;
	}


	public void drawSeft(Graphics g) {
//		System.out.println("gameobject:"+imgX);
		g.drawImage(imgImage, (int)imgX, (int)imgY, null);
	}

	//返回物体所在矩形,便于后续的碰撞检测
	public Rectangle getRect() {
		return new Rectangle((int)imgX, (int)imgY, imgWidth, imgHeight);
	}
	
}

你可能感兴趣的:(尚学堂java300集飞机小游戏实战)