简易雷霆战机模型

此简易雷霆战机模型实现了雷霆战机游戏的核心功能,运行结果截图如下:


代码:
1.planegame包
(1):PlaneGameFrame类:
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Date;
import util.GameUtil;
import util.MyFrame;

public class PlaneGameFrame extends MyFrame{
Image bg = GameUtil.getImage("images/bg.jpg"); //背景
Plane player = new Plane("images/player.png",175,550); //飞机
ArrayList bulletList = new ArrayList(); //子弹
Explode boom; //爆炸
Date startTime;
Date endTime;

public void paint(Graphics g){
	g.drawImage(bg, 0, 0, null);  //背景
	player.draw(g);  //飞机
	for(int i=0;i<bulletList.size();i++){  //子弹
		Bullet b = (Bullet)bulletList.get(i);
		b.draw(g);
		
		//检测碰撞
		boolean bm = b.getRect().intersects(player.getRect());
		if(bm){	
			player.setLive(false);
		}
	}
	
	//死亡处理
	if(!player.isLive()){
		if(boom == null){
			boom = new Explode(player.x,player.y);
			endTime = new Date();
		}
		boom.draw(g);
		//打印结果
		printInfo(g,"GAME OVER",80,300,50,Color.white);
	    long period = (endTime.getTime()
			- startTime.getTime())/1000;
	    printInfo(g,"时间:"+period+"秒",100,350,30,Color.green);
        printInfo(g,"分数:"+period*13,50,50,10,Color.green);
        }
}

//打印信息:信息/位置(x/y)/尺寸/颜色
public void printInfo(Graphics g,String str,int x,int y,int size,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);
}

public static void main(String[] args) {
	new PlaneGameFrame().launchFrame();
}

public void launchFrame(){
	super.launchFrame();
	addKeyListener(new KeyMonitor());
	//生成子弹
	for(int i=0;i<20;i++){
		Bullet b = new Bullet();
		bulletList.add(b); 
	}
	//开始计时
	startTime = new Date();
}

//定义内部类
class KeyMonitor extends KeyAdapter{
	
	public void keyPressed(KeyEvent e){
		 player.changeDirection(e);
		System.out.println("按下:" + e.getKeyCode());
	}
	 
	public void keyReleased(KeyEvent e){
		player.recoveryDirection(e);
		System.out.println("释放:" + e.getKeyCode());
	}
}

}
(2):GameObject类:
import java.awt.Image;
import java.awt.Rectangle;

public class GameObject {
Image img;
double x,y;
int width,height;
int speed = 5;

public Rectangle getRect(){
	return new Rectangle((int)x,(int)y, width, height);
}

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

}
(3)Plane类:
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import util.GameUtil;

public class Plane extends GameObject{
private boolean left,up,right,down;
private boolean live = true;

public boolean isLive() {
	return live;
}

public void setLive(boolean live) {
	this.live = live;
}

public Plane(){
}

public Plane(String imgpath, double x, double y) {
	this.img = GameUtil.getImage("images/player.png");
	this.x = x;
	this.y = y;
	this.width = img.getWidth(null);
	this.height = img.getHeight(null);
}

public void draw(Graphics g){
	if(live){
		g.drawImage(img,(int)x,(int)y,null);
		move();
	}
}

public void changeDirection(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 recoveryDirection(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;![](http://images2015.cnblogs.com/blog/1096107/201702/1096107-20170214222741910-1089599422.jpg)


		break;
	}
}

public void move(){
	if(left){
		x -=speed;
	}
	if(up){
		y -= speed;
	}
	if(right){
		x += speed;
	}
	if(down){
		y += speed;
	}
}

}
(4)Bullet类:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;

import util.Constant;

public class Bullet extends GameObject{
double degree;

public Bullet(){
	x = Constant.WIDTH/2;
	y = Constant.HEIGHT/2;
	width = 6;
	height = 6;
	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.WIDTH - width){ 
		degree = Math.PI-degree;
	}
	if(y<0||y>Constant.HEIGHT - height){
		degree = -degree;
	}
	
	g.setColor(c);
}

}
(5)Explode类:
import java.awt.Graphics;
import java.awt.Image;
import util.GameUtil;

public class Explode {
double x,y;
static Image[] imgs = new Image[16];
int count;

static {
	for(int i=0;i<16;i++){
		imgs[i] = GameUtil.getImage("images/explode/e"+(i+1)+".gif");
		imgs[i].getWidth(null);
	}
}

public void draw(Graphics g){
	if(count<16){
		g.drawImage(imgs[count],(int)x,(int)y,null);
		count++;
	}
}    

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

}
2.工具包
(1)GameUtil类:
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){
URL u = GameUtil.class.getClassLoader().getResource(path);
BufferedImage img = null;
try {
img = ImageIO.read(u);
} catch (IOException e) {
e.printStackTrace();
}
return img;
}
}
(2)框架类:
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class MyFrame extends Frame {

// 加载窗口
public void launchFrame(){
	setSize(Constant.WIDTH,Constant.HEIGHT);
	setLocation(100,100);
	setVisible(true);
	new PaintThread().start();  //启动重画线程
	
	addWindowListener(new WindowAdapter(){
		public void windowClosing(WindowEvent e){
			System.exit(0);
		}
	});
}

//重画窗口
class PaintThread extends Thread{
	public void run(){
		while(true){
			repaint();
			try {
				Thread.sleep(40);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}

//消除闪烁
private Image offScreenImage = null;
public void update(Graphics g){
	if(offScreenImage == null){
		offScreenImage = this.createImage(Constant.WIDTH,Constant.HEIGHT);
		}
	Graphics gOff = offScreenImage.getGraphics();
	paint(gOff);
	g.drawImage(offScreenImage, 0, 0, null);
}

}
(3)常量类:
public class Constant {
public static final int WIDTH = 400;
public static final int HEIGHT = 600;
}
3.素材包
(1)背景:
(2)飞机:
(3)爆炸:

posted @ 2017-02-14 22:16  流水-_-  阅读(2617)  评论(0编辑  收藏  举报