The ninth day_打飞机项目
开发打飞机小游戏
7点半起床:
8点半学习:
11点半吃饭 3h
12点半学习
5点半散步: 5h
6点半学习
7点半吃饭 1h
8点学习
11点半洗漱 3.5h
12点睡觉
sum = 12.5h
8点半学习:
11点半吃饭 3h
12点半学习
5点半散步: 5h
6点半学习
7点半吃饭 1h
8点学习
11点半洗漱 3.5h
12点睡觉
sum = 12.5h
基本功能实现
AWT和Swing是Java中常见的GUI(图形用户界面)技术,不需要花时间掌握
1 package cn.zqf.game; 2 import java.awt.Window; 3 import java.awt.event.WindowAdapter; 4 import javax.swing.JFrame; 5 import com.sun.glass.events.WindowEvent; 6 //飞机游戏的主窗口 7 public class MyGameFrame extends JFrame { 8 //下面的这个方法用于初始化窗口 9 public void launchFrame(){ 10 this.setTitle("邹奇方的游戏"); 11 this.setVisible(true); 12 this.setSize(500, 500); //设置大小 13 this.setLocation(500,100); //设置出现在屏幕的位置,以左上角为标准 14 15 this.addWindowListener(new WindowAdapter(){ 16 public void windowClosing(WindowEvent e){ 17 System.exit(0); 18 } 19 }); 20 } 21 public static void main(String[] args) { 22 MyGameFrame f= new MyGameFrame(); 23 f.launchFrame(); 24 } 25 }
图形绘制_文本绘制_颜色改变_图像对象的加载
paint 方法 以下是打飞机项目的7个模块:都由包名开头:
1 package cn.sxt.game; 2 public class Constant { 3 public static final int GAME_WIDTH = 500; 4 public static final int GAME_HEIGHT = 500;
1 package cn.sxt.game; 2 import java.awt.Graphics; 3 import java.awt.Image; 4 // 爆炸类 5 6 public class Explode { 7 double x, y; 8 9 static Image[] imgs = new Image[16]; 10 static { 11 for (int i = 0; i < 16; i++) { 12 imgs[i] = GameUtil.getImage("images/explode/e" + (i + 1) + ".gif"); 13 imgs[i].getWidth(null); 14 } 15 } 16 int count; 17 public void draw(Graphics g) { 18 if (count <= 15) { 19 g.drawImage(imgs[count], (int) x, (int) y, null); 20 count++; 21 } 22 } 23 public Explode(double x, double y) { 24 this.x = x; 25 this.y = y; 26 } 27 }
1 package cn.sxt.game; 2 import java.awt.Graphics; 3 import java.awt.Image; 4 import java.awt.Rectangle; 5 //游戏物体的父类 6 public class GameObject { 7 Image img; 8 double x,y; 9 int speed; 10 int width, height; 11 12 public void drawSelf(Graphics g){ 13 g.drawImage(img, (int)x,(int) y, null); 14 } 15 public GameObject(Image img, double x, double y, int speed, int width, int height) { 16 super(); 17 this.img = img; 18 this.x = x; 19 this.y = y; 20 this.speed = speed; 21 this.width = width; 22 this.height = height; 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 34 //返回物体所在的矩形。便于后续的碰撞检测 35 public Rectangle getRect(){ 36 return new Rectangle((int)x, (int)y, width, height); 37 } 38 }
package cn.sxt.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() { } //返回指定路径文件的图片对象 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; } }
1 package cn.sxt.game; 2 import java.awt.Color; 3 import java.awt.Font; 4 import java.awt.Frame; 5 import java.awt.Graphics; 6 import java.awt.Image; 7 import java.awt.event.KeyAdapter; 8 import java.awt.event.KeyEvent; 9 import java.awt.event.WindowAdapter; 10 import java.awt.event.WindowEvent; 11 import java.util.Date; 12 // 飞机游戏的主窗口 13 14 public class MyGameFrame extends Frame { 15 16 Image planeImg = GameUtil.getImage("images/plane.png"); 17 Image bg = GameUtil.getImage("images/bg.jpg"); 18 19 Plane plane = new Plane(planeImg,250,250); 20 Shell[] shells = new Shell[50]; 21 22 Explode bao ; 23 Date startTime = new Date(); 24 Date endTime; 25 int period; //游戏持续的时间 26 27 @Override 28 public void paint(Graphics g) { //自动被调用。 g相当于一只画笔 29 Color c = g.getColor(); 30 g.drawImage(bg, 0, 0, null); 31 32 plane.drawSelf(g); //画飞机 33 34 //画出所有的炮弹 35 for(int i=0;i<shells.length;i++){ 36 shells[i].draw(g); 37 38 //飞机和炮弹的碰撞检测!!! 39 boolean peng = shells[i].getRect().intersects(plane.getRect()); 40 if(peng){ 41 plane.live = false; 42 if(bao ==null){ 43 bao = new Explode(plane.x, plane.y); 44 45 endTime = new Date(); 46 period = (int)((endTime.getTime()-startTime.getTime())/1000); 47 } 48 bao.draw(g); 49 } 50 51 //计时功能,给出提示 52 if(!plane.live){ 53 g.setColor(Color.red); 54 Font f = new Font("宋体", Font.BOLD, 50); 55 g.setFont(f); 56 g.drawString("时间:"+period+"秒", (int)plane.x, (int)plane.y); 57 } 58 59 } 60 61 g.setColor(c); 62 } 63 64 65 //帮助我们反复的重画窗口! 66 class PaintThread extends Thread { 67 @Override 68 public void run() { 69 while(true){ 70 repaint(); //重画 71 72 try { 73 Thread.sleep(40); //1s=1000ms 74 } catch (InterruptedException e) { 75 e.printStackTrace(); 76 } 77 } 78 } 79 80 } 81 82 //定义键盘监听的内部类 83 class KeyMonitor extends KeyAdapter { 84 @Override 85 public void keyPressed(KeyEvent e) { 86 plane.addDirection(e); 87 } 88 @Override 89 public void keyReleased(KeyEvent e) { 90 plane.minusDirection(e); 91 } 92 } 93 // 初始化窗口 94 public void launchFrame(){ 95 this.setTitle("尚学堂学员_程序猿作品"); 96 this.setVisible(true); 97 this.setSize(Constant.GAME_WIDTH , Constant.GAME_HEIGHT); 98 this.setLocation(300, 300); 99 100 this.addWindowListener(new WindowAdapter() { 101 @Override 102 public void windowClosing(WindowEvent e) { 103 System.exit(0); 104 } 105 }); 106 107 new PaintThread().start(); //启动重画窗口的线程 108 addKeyListener(new KeyMonitor()); //给窗口增加键盘的监听 109 110 111 //初始化50个炮弹 112 for(int i=0;i<shells.length;i++){ 113 shells[i] = new Shell(); 114 } 115 116 } 117 118 public static void main(String[] args) { 119 MyGameFrame f = new MyGameFrame(); 120 f.launchFrame(); 121 } 122 123 private Image offScreenImage = null; 124 125 public void update(Graphics g) { 126 if(offScreenImage == null) 127 offScreenImage = this.createImage(Constant.GAME_WIDTH,Constant.GAME_HEIGHT);//这是游戏窗口的宽度和高度 128 129 Graphics gOff = offScreenImage.getGraphics(); 130 paint(gOff); 131 g.drawImage(offScreenImage, 0, 0, null); 132 } 133 134 }
1 package cn.sxt.game; 2 import java.awt.Graphics; 3 import java.awt.Image; 4 import java.awt.event.KeyEvent; 5 //飞机类 6 public class Plane extends GameObject { 7 boolean left,up,right,down; 8 9 boolean live = true; 10 11 public void drawSelf(Graphics g){ 12 if(live){ 13 g.drawImage(img, (int)x,(int) y, null); 14 15 if(left){ 16 x -=speed; 17 } 18 if(right){ 19 x += speed; 20 } 21 if(up){ 22 y -=speed; //y = y-speed; 23 } 24 if(down){ 25 y += speed; 26 } 27 }else{ 28 } 29 } 30 public Plane(Image img, double x, double y){ 31 this.img = img; 32 this.x = x; 33 this.y = y; 34 this.speed = 3; 35 this.width = img.getWidth(null) ; 36 this.height = img.getHeight(null); 37 } 38 //按下某个键,增加相应的方向 39 public void addDirection(KeyEvent e){ 40 switch (e.getKeyCode()) { 41 case KeyEvent.VK_LEFT: 42 left = true; 43 break; 44 case KeyEvent.VK_UP: 45 up = true; 46 break; 47 case KeyEvent.VK_RIGHT: 48 right = true; 49 break; 50 case KeyEvent.VK_DOWN: 51 down = true; 52 break; 53 } 54 } 55 //按下某个键,取消相应的方向 56 public void minusDirection(KeyEvent e){ 57 switch (e.getKeyCode()) { 58 case KeyEvent.VK_LEFT: 59 left = false; 60 break; 61 case KeyEvent.VK_UP: 62 up = false; 63 break; 64 case KeyEvent.VK_RIGHT: 65 right = false; 66 break; 67 case KeyEvent.VK_DOWN: 68 down = false; 69 break; 70 } 71 } 72 }
1 package cn.sxt.game; 2 import java.awt.Color; 3 import java.awt.Graphics; 4 //炮弹类 5 public class Shell extends GameObject { 6 7 double degree; 8 9 public Shell(){ 10 x = 200; 11 y = 200; 12 width=10; 13 height = 10; 14 speed = 3; 15 degree = Math.random()*Math.PI*2; 16 } 17 public void draw(Graphics g){ 18 Color c = g.getColor(); 19 g.setColor(Color.YELLOW); 20 21 g.fillOval((int)x,(int) y, width, height); 22 23 //炮弹沿着任意角度去飞 24 x += speed*Math.cos(degree); 25 y += speed*Math.sin(degree); 26 27 if(x<0||x>Constant.GAME_WIDTH-width){ 28 degree = Math.PI - degree; 29 } 30 if(y<30||y>Constant.GAME_HEIGHT-height){ 31 degree = - degree; 32 } 33 g.setColor(c); 34 } 35 }