java飞机大战

此游戏实现通过操作上下左右方向键来操控飞机的移动,子弹从中间圆上任意方向发射,碰到墙壁时镜面反弹,当碰到飞机时,飞机爆炸,并输出战绩。

先看效果图(含爆炸效果):

源码如下:

工具包:

 

package com.uwo9.util;

/**
 * 游戏项目中的常量
 *
 */
public class Constant {
	/**
	 * 窗体宽度
	 */
	public static final int GAME_WIDTH=500;
	/**
	 * 窗体高度
	 */
	public static final int GAME_HEIGHT=500;
}

 

工具类

 

 

package com.uwo9.util;

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() {// 工具类通常会将构造方法私有
	}

	/**
	 * 
	 * @param path
	 *            图片地址
	 * @return
	 */
	public static Image getImage(String path) {
		BufferedImage image = null;
		try {
			URL u = GameUtil.class.getClassLoader().getResource(path);
			image = ImageIO.read(u);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return image;
	}
}

窗体:

 

 

package com.uwo9.util;

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 {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 4419278382442876209L;

	protected MyFrame(){}
	protected MyFrame(String s){
		super(s);
	}
	
	/**
	 * 加载窗口
	 */
	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);
			}
		});
	}

	private Image offScreenImage = null;

	/**
	 * 利用双缓冲技术处理闪烁
	 */
	@Override
	public void update(Graphics g) {
		if (offScreenImage == null) {
			offScreenImage = this.createImage(this.getSize().width, this.getSize().height);
		}
		Graphics gOff = offScreenImage.getGraphics();
		print(gOff);
		g.drawImage(offScreenImage, 0, 0, null);
	}

	/**
	 * 定义一个重画窗口的线程类
	 *
	 */
	class PaintThread extends Thread {
		@Override
		public void run() {
			while (true) {
				repaint();
				try {
					Thread.sleep(40);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}

}


游戏功能:

 

主窗体

 

package com.uwo9.plane;

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 com.uwo9.util.GameUtil;
import com.uwo9.util.MyFrame;

public class PlaneGameFrame extends MyFrame {
	private static final long serialVersionUID = -2627677621770317384L;
	Image bg = GameUtil.getImage("images/bg.jpg");
	plane p = new plane("images/plane.png", 50, 50);

	ArrayList<Bullet> bulletList = new ArrayList<Bullet>();//子弹

	Date startTime;//开始时间
	Date endTime;//结束时间
	Explode bao;//爆炸类

	public PlaneGameFrame() {
	}

	public PlaneGameFrame(String str) {
		super(str);
	}

	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){
					endTime = new Date();
					bao = new Explode(p.x, p.y);
				}
				bao.draw(g);
				break;
			}
		}
		if (!p.isLive()) {
			int period = (int) (endTime.getTime() - startTime.getTime()) / 1000;
			printInfo(g, "时间:" + period + "秒", 20, 100, 230, Color.white);
			switch (period / 10) {
			case 0:
			case 1:
				printInfo(g, "菜鸟", 50, 100, 200, Color.white);
				break;
			case 2:
				printInfo(g, "入门", 50, 100, 200, Color.white);
				break;
			case 3:
				printInfo(g, "熟练", 50, 100, 200, Color.white);
				break;
			case 4:
				printInfo(g, "精通", 50, 100, 200, Color.yellow);
				break;
			default:
				printInfo(g, "大神", 50, 100, 200, Color.yellow);
				break;
			}
		}
	}

	/**
	 * 在窗口上打印信息
	 * 
	 * @param g
	 * @param str
	 * @param size
	 */
	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);
	}

	public static void main(String[] args) {
		new PlaneGameFrame("飞机游戏").launchFrame();
	}

	@Override
	public void launchFrame() {
		super.launchFrame();
		// 增加键盘的监听
		addKeyListener(new keyMonitor());

		// 生成一堆子弹
		for (int i = 0; i < 10; 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);
		}

	}
}

游戏

package com.uwo9.plane;

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

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

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

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

子弹

package com.uwo9.plane;

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

import com.uwo9.util.Constant;

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

	public void draw(Graphics g) {
		Color c = g.getColor();
		g.setColor(Color.yellow);
		g.fillOval((int) x, (int) y, width, height);

		if (y > Constant.GAME_HEIGHT - height || y < 30) {
			degree = -degree;
		}
		if (x < 0 || x > Constant.GAME_WIDTH - width) {
			degree = Math.PI - degree;
		}

		x += speed * Math.cos(degree);
		y += speed * Math.sin(degree);
		g.setColor(c);
	}
}

飞机

 

 

package com.uwo9.plane;

import java.awt.Graphics;
import java.awt.event.KeyEvent;

import com.uwo9.util.Constant;
import com.uwo9.util.GameUtil;

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

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

	public void move() {
		if (left && x > 0) {
			x -= speed;
		}
		if (right && x < Constant.GAME_WIDTH - width) {
			x += speed;
		}
		if (up && y > 30) {
			y -= speed;
		}
		if (down && y < Constant.GAME_HEIGHT - height) {
			y += speed;
		}
	}

	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(String imgpath, double x, double y) {
		this.img = GameUtil.getImage(imgpath);
		this.width = 30;
		this.height = 30;
		this.x = x;
		this.y = y;
	}

	public boolean isLive() {
		return live;
	}

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

}

爆炸

 

 

package com.uwo9.plane;

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

import com.uwo9.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 <= 15) {
			g.drawImage(imgs[count], (int) x, (int) y, null);
			count++;
		}
	}

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

 

源码地址:链接:https://pan.baidu.com/s/1rE35rUE_Dfyr8KsDRockjQ 提取码:546w

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值