(根据尚学堂学习制作,侵删)
飞机游戏的主窗口:
package cn.ldd.game;
import java.awt.Color;
import java.awt.Font;
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;
import javax.swing.JFrame;
/**
* 飞机游戏的主窗口
*
*/
public class MyGameFrame extends Frame {
Image planeImg = GameUtil.getImage("images/plane.png");
Image bg = GameUtil.getImage("images/bg.jpg");
Plane plane = new Plane(planeImg,250,250);
Shell[] shells = new Shell[50]; //炮弹数组
Explode bao; //爆炸对象
Date startTime = new Date(); //起始时间
Date endTime; //结束时间
int period; //游戏持续的时间
@Override
public void paint(Graphics g) { //自动被调用。 g相当于一只画笔
Color c=g.getColor();
g.drawImage(bg, 0, 0, null);
plane.drawSelf(g); //画飞机
//画50个炮弹
for (int i = 0; i < shells.length; i++) {
shells[i].draw(g);
//飞机和炮弹的碰撞检测
boolean peng=shells[i].getRect().intersects(plane.getRect());
if(peng){
plane.live=false; //如果相撞了,飞机就死了
//爆炸
if(bao==null){
bao = new Explode(plane.x,plane.y); //在飞机碰撞坐标爆炸
endTime=new Date(); //飞机爆炸时为结束时间
period=(int)((endTime.getTime()-startTime.getTime())/1000); //游戏持续时间计算
}
bao.draw(g); //画出爆炸
}
//计时功能,如果飞机死了,显示时间提示
if(!plane.live){
g.setColor(Color.red); //提示时间的字体颜色设为红色
Font f = new Font("宋体",Font.BOLD,50); //设置字体
g.setFont(f);
g.drawString("时间:"+period+"秒", (int)plane.x,(int)plane.y);
}
}
g.setColor(c); //初始化画笔的颜色
}
//帮助我们反复的重画窗口!
class PaintThread extends Thread {
@Override
public void run() {
while(true){
repaint(); //重画
try {
Thread.sleep(40); //1s=1000ms
} 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("学员 ldd作品");
this.setVisible(true);
this.setSize(Constant.GAME_WIDTH,Constant.GAME_HEIGHT);
this.setLocation(300, 150);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
new PaintThread().start(); //启动重画窗口的线程
addKeyListener(new KeyMonitor()); //给窗口增加键盘的监听
//初始化50个炮弹
for (int i = 0; i < shells.length; i++) {
shells[i]=new Shell();
}
}
public static void main(String[] args) {
MyGameFrame f = new MyGameFrame();
f.launchFrame();
}
private Image offScreenImage = null;
public void update(Graphics g) {
if(offScreenImage == null)
offScreenImage = this.createImage(Constant.GAME_WIDTH,Constant.GAME_HEIGHT);//游戏窗口的宽度和高度
Graphics gOff = offScreenImage.getGraphics();
paint(gOff);
g.drawImage(offScreenImage, 0, 0, null);
}
}
常量类:固定窗体的高度和宽度
package cn.ldd.game;
/*
* 常量类
*/
public class Constant {
public static final int GAME_WIDTH=500;
public static final int GAME_HEIGHT=500;
}
游戏物体的父类
package cn.ldd.game;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
/*
* 游戏物体的父类
*/
public class GameObject {
Image img;
double x,y;
int speed;
int width,height;
public void drawSelf(Graphics g){
g.drawImage(img, (int)x, (int)y, null);
}
public GameObject(Image img, double x, double y, int speed, int width,
int height) {
super();
this.img = img;
this.x = x;
this.y = y;
this.speed = speed;
this.width = width;
this.height = height;
}
public GameObject(Image img, double x, double y) {
super();
this.img = img;
this.x = x;
this.y = y;
}
public GameObject() {
}
//返回物体所在的矩形,便于后续的碰撞检测
public Rectangle getRect(){
return new Rectangle((int)x,(int)y,width,height);
}
}
返回指定路径的图片对象
package cn.ldd.game;
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 bi = null;
try {
URL u = GameUtil.class.getClassLoader().getResource(path);
bi = ImageIO.read(u);
} catch (IOException e) {
e.printStackTrace();
}
return bi;
}
}
飞机类
package cn.ldd.game;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
public class Plane extends GameObject {
boolean left,right,up,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;
}
}else
{
}
}
public Plane(Image img,double x,double y){
this.img = img;
this.x = x;
this.y = y;
this.speed=3;
this.width=img.getWidth(null);
this.height=img.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;
}
}
//取消相应的方向
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;
}
}
}
炮弹类
package cn.ldd.game;
import java.awt.Color;
import java.awt.Graphics;
/*
* 炮弹类
*/
public class Shell extends GameObject {
double degree; //弧度
public Shell(){
x=200; //炮弹位置x
y=200; //炮弹位置y
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); //用完画笔变为初始值
}
}
爆炸类
package cn.ldd.game;
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) {
this.x = x;
this.y = y;
}
}
创建的项目
图片素材
效果图