1 /**
2 * 常量类
3 * @author 罗摩衔那
4 *
5 */
6 public class Constant {
7 public static final int GAME_WIDTH=500;
8 public static final int GAME_HEIGHT=500;
9
10 }
1 import java.awt.Graphics;
2 import java.awt.Image;
3
4 public class Explode extends GameObject {
5 double x, y;
6 //声明一维静态数组长度
7 static Image[]img=new Image[16];
8 //用静态代码块加载数组图片
9 static {
10 for (int i = 0; i < 16; i++) {
11 img[i]=GameUtil.getImage("images/explode/e"+(i+1)+".gif");
12 img[i].getWidth(null);
13 }
14 }
15
16 int count;
17 /**
18 * 画出十六张图
19 */
20 public void draw(Graphics g) {
21 if(count<=15) {
22 g.drawImage(img[count], (int)x,(int) y, null);
23 count++;
24 }
25 }
26 public Explode(double x,double y) {
27 this.x=x;
28 this.y=y;
29 }
30 }
1 import java.awt.Graphics;
2 import java.awt.Image;
3
4 public class GameObject {
5 Image img;
6 double x,y;//图像位置参数
7 int speed;
8 int width,height;
9
10 public void drawSelf(Graphics g) {
11 g.drawImage(img,(int) x,(int) y, null);
12 }
13
14 public GameObject(Image img, double x, double y, int speed, int width, int height) {
15 super();
16 this.img = img;
17 this.x = x;
18 this.y = y;
19 this.speed = speed;
20 this.width = width;
21 this.height = height;
22 }
23
24 public GameObject(Image img, double x, double y) {
25 super();
26 this.img = img;
27 this.x = x;
28 this.y = y;
29 }
30
31 public GameObject() {
32 }
33 }
1 /**
2 * 工具类实现图片的获取
3 */
4 import java.awt.Image;
5 import java.awt.image.BufferedImage;
6 import java.io.IOException;
7 import java.net.URL;
8
9 import javax.imageio.ImageIO;
10
11 public class GameUtil {
12 //工具类最好将构造器私有化
13 private GameUtil() {
14
15 }
16
17 public static Image getImage(String path) {
18 BufferedImage bi=null;//图片缓存区为空
19 /**
20 *图片获取
21 */
22 try {
23 URL u=GameUtil.class.getClassLoader().getResource(path);
24 bi=ImageIO.read(u);
25 } catch (IOException e) {
26 e.printStackTrace();
27 }
28 return bi;
29 }
30 }
1 /**
2 * 飞机的图像自画
3 */
4 import java.awt.Graphics;
5 import java.awt.Image;
6 import java.awt.Rectangle;
7 import java.awt.event.KeyEvent;
8
9 public class Plane extends GameObject{
10 int speed=3;
11 boolean left,right,up,down;
12 boolean live=true;
13
14 public void drawSelf(Graphics g) {
15 if(live) {//飞机若活着自画出
16 g.drawImage(img,(int) x,(int) y, null);//父类方法的重写
17 if(left) {
18 x-=speed;
19 }
20 if(right) {
21 x+=speed;
22 }
23 if(up) {
24 y-=speed;
25 }
26 if(down) {
27 y+=speed;
28 }
29 }else {
30
31 }
32 }
33
34 public Plane(Image img,double x,double y) {
35 this.img=img;
36 this.x=x;
37 this.y=y;
38 this.speed=3;
39 this.width=img.getWidth(null);
40 this.height=img.getHeight(null);
41 }
42
43 //按下某个键,增加相应的方向
44 public void addDirection(KeyEvent e) {
45 switch(e.getKeyCode()) {
46 case KeyEvent.VK_LEFT:
47 left=true;
48 break;
49 case KeyEvent.VK_UP:
50 up=true;
51 break;
52 case KeyEvent.VK_RIGHT:
53 right=true;
54 break;
55 case KeyEvent.VK_DOWN:
56 down=true;
57 break;
58 }
59 }
60 //按下某个键,取消相应的方向
61 public void minusDirection(KeyEvent e){
62 switch (e.getKeyCode()) {
63 case KeyEvent.VK_LEFT:
64 left = false;
65 break;
66 case KeyEvent.VK_UP:
67 up = false;
68 break;
69 case KeyEvent.VK_RIGHT:
70 right = false;
71 break;
72 case KeyEvent.VK_DOWN:
73 down = false;
74 break;
75 }
76 }
77 /**
78 * 返回物体所在的矩形.便于后续的碰撞检测
79 */
80 public Rectangle getRect() {
81 return new Rectangle((int)x,(int)y,width,height);
82 }
83 }
1 import java.awt.Color;
2 /**
3 * 炮弹的设计及自画
4 */
5 import java.awt.Graphics;
6 import java.awt.Image;
7 import java.awt.Rectangle;
8
9 public class Shell extends GameObject{
10 double degree;
11
12
13
14 public Shell() {
15 x=200;
16 y=200;
17 width=10;//球的宽度
18 height=10;//球的高度
19 speed=3;//球的速度
20 degree=Math.random()*Math.PI*2;//生成随机的0~2π角度
21 }
22
23
24
25 public void draw(Graphics g) {
26 Color c=g.getColor();
27 g.setColor(Color.YELLOW);
28
29 g.fillOval((int)x, (int)y, width, height);
30
31 //炮弹沿着任意角度去飞
32 x+=speed*Math.cos(degree);
33 y+=speed*Math.sin(degree);
34
35 if(x<0 || x>Constant.GAME_WIDTH-width)
36 {
37 degree=Math.PI-degree;
38 }
39 if(y<0 || y>Constant.GAME_HEIGHT-height)
40 {
41 degree=-degree;
42 }
43
44 g.setColor(c);
45
46 }
47
48
49
50 public Rectangle getRect() {
51
52 return new Rectangle((int)x,(int)y,width,height);
53 }
54
55 }
1 import java.awt.Color;
2 import java.awt.Font;
3 import java.awt.Frame;
4 import java.awt.Graphics;
5 import java.awt.Image;
6 import java.awt.event.KeyAdapter;
7 import java.awt.event.KeyEvent;
8 import java.awt.event.WindowAdapter;
9 import java.awt.event.WindowEvent;
10 import java.util.Date;
11
12 /**
13 * 初始化窗口
14 * @author 罗摩衔那
15 *
16 */
17 public class MyGameFrame extends Frame{
18 //图片加载
19 Image bg=GameUtil.getImage("Image/bg.jpg");
20 Image planeImg=GameUtil.getImage("Image/plane.png");
21 Plane plane=new Plane(planeImg,250,250);//飞机对象的创建及构造参数的传入
22 Shell[]s=new Shell[30];//定义了炮弹的个数
23 Explode ex;//声明爆炸类对象
24 Date startTime=new Date();//飞机开始时间
25 Date endTime;//飞机死亡时间
26 int time;//存活时间计数
27
28 //图形的绘画
29 public void paint(Graphics g) {
30 Color c=g.getColor();
31 g.drawImage(bg, 0, 0, null);
32 plane.drawSelf(g);//调用飞机类的自画功能
33 /*
34 * 画出了炮弹的个数
35 */
36 for (int i = 0; i < s.length; i++) {
37 s[i].draw(g);
38
39 //飞机和炮弹的碰撞检测
40 boolean peng=s[i].getRect().intersects(plane.getRect());
41 if(peng) {
42 plane.live=false;
43 if(ex==null) {//在飞机的坐标发生爆炸
44 ex=new Explode(plane.x, plane.y);
45
46 endTime=new Date();
47 time=(int)((endTime.getTime()-startTime.getTime())/1000);
48 }
49 ex.draw(g);
50 }
51 if(!plane.live) {
52 g.setColor(Color.RED);
53 Font t=new Font("宋体", Font.BOLD, 50);
54 g.setFont(t);
55 g.drawString("时间:"+time+"秒" ,(int)plane.x, (int)plane.y);
56 }
57 }
58 }
59 //反复的画窗口
60 class PaintThread extends Thread{
61 public void run() {
62 while(true) {
63 repaint();//重画
64 try {
65 Thread.sleep(40);
66 } catch (InterruptedException e) {
67
68 e.printStackTrace();
69 }
70 }
71 }
72 }
73
74 //定义键盘监听的内部类
75 class KeyMonitor extends KeyAdapter{
76
77 @Override
78 public void keyPressed(KeyEvent e) {
79
80 plane.addDirection(e);
81 }
82
83 @Override
84 public void keyReleased(KeyEvent e) {
85
86 plane.minusDirection(e);
87 }
88
89 }
90 /**
91 * 初始化窗口
92 */
93 public void Initialization () {
94 this.setTitle("大应用最萌的程序狗子作品");//设置窗口标题
95 this.setVisible(true);//设置可见窗口
96 this.setSize(Constant.GAME_WIDTH, Constant.GAME_HEIGHT);//设置窗口大小
97 this.setLocation(300, 300);//设置显示位置
98 //设置监听事件
99 this.addWindowListener(new WindowAdapter() {
100 public void windowClosing(WindowEvent e) {
101 System.exit(0);
102 }
103 });
104 new PaintThread().start();//启动重画线程的方法
105 this.addKeyListener(new KeyMonitor());//增加键盘的监听
106
107
108 //初始化三十个炮弹的参数
109 for (int i = 0; i < s.length; i++) {
110 s[i]=new Shell();
111 }
112
113 }
114 public static void main(String[] args) {
115 MyGameFrame mg=new MyGameFrame();
116 mg.Initialization();
117
118 }
119 /**
120 * 双缓冲
121 */
122 private Image offScreenImage=null;
123
124 public void update(Graphics g) {
125 if(offScreenImage==null)
126 offScreenImage=this.createImage(Constant.GAME_WIDTH, Constant.GAME_HEIGHT);
127
128 Graphics gOff=offScreenImage.getGraphics();//offScreenImage离屏图像
129 paint(gOff);
130 g.drawImage(offScreenImage, 0, 0, null);
131 }
132 }

