1.需求/要求:
1)固定框架:固定窗口-背景、背板;
2)程序运行状态:开始、暂停、结束--状态识别;
3)对象间的相互作用:
a.英雄机:鼠标控制移动--鼠标动作侦听;
发射子弹;
碰撞--原始3命,碰撞-1命;
b.敌机:移动--从上往下,直线下落;
碰撞--敌机消失;
被击中--消失,加分;
c.蜜蜂:移动--从上往下,斜线下落,碰到左右边框,反方向移动;
碰撞--敌机消失;
被击中--消失,奖励:加1命;
双倍子弹--有数量限制;
d.子弹:移动--从英雄机发射出。从下往上,直线上升;
碰撞--消失;
2.对象:英雄机、敌机、蜜蜂、子弹;
父类:飞行物:敌机、蜜蜂、英雄机;
接口:1)奖励,2)得分;
主程
1 package com.raizosight.starcraft; 2 import java.awt.image.BufferedImage; 3 4 /** 5 * FlyingObject.java description:飞行物的抽象类,体现敌机、蜜蜂、英雄机共同的属性、行为. 6 * @author raizoo 7 * Created on 17-7-21 下午2:53. 8 * @version 1.1 9 * @since JDK1.8 10 * @throws Exception: 11 * 12 */ 13 public abstract class FlyingObject { 14 //属性 15 protected int x; 16 protected int y; 17 protected int width; 18 protected int height; 19 protected int speed; 20 public BufferedImage image; 21 22 public abstract void step(int x,int y); 23 24 }
1 package com.raizosight.starcraft; 2 /** 3 * Enemy.java description: 敌机被击中得分的接口,可扩展不同的敌机,如boss、小敌机等 4 * @author raizoo 5 * Created on 17-7-21 下午3:01. 6 * @version 1.1 7 * @since JDK1.8 8 * @throws Exception: 9 * 10 */ 11 public interface Enemy { 12 public abstract int beat(); 13 }
1 package com.raizosight.starcraft; 2 /** 3 * Award.java description: 击中蜜蜂的奖励:+1生命、双倍子弹; 4 * @author raizoo 5 * Created on 17-7-21 下午3:04. 6 * @version 1.1 7 * @since JDK1.8 8 * @throws Exception: 9 * 10 */ 11 public interface Award { 12 public static final int LIFE = 0; //+1生命 13 public static final int DOUBLE_FIRE = 1; //双倍子弹 14 15 public abstract void awardType(); 16 }
1 package com.raizosight.starcraft; 2 import java.awt.image.BufferedImage; 3 4 /** 5 * AirPlane.java description: 敌机类 6 * @author raizoo 7 * Created on 17-7-21 下午3:09. 8 * @version 1.1 9 * @since JDK1.8 10 * @throws Exception: 11 * 12 */ 13 public class AirPlane extends FlyingObject implements Enemy{ 14 //自身属性 15 16 17 //初始化属性 18 public AirPlane(){ 19 int x; 20 int y; 21 int width; 22 int height; 23 int speed; 24 BufferedImage image; 25 } 26 27 //继承方法-移动 28 public void step(int x, int y){ 29 30 } 31 32 //实现方法 33 public int beat(){ 34 return 5; 35 } 36 }
1 package com.raizosight.starcraft; 2 import java.awt.image.BufferedImage; 3 /** 4 * Bee.java description: 蜜蜂类 5 * @author raizoo 6 * Created on 17-7-21 下午3:30. 7 * @version 1.1 8 * @since JDK1.8 9 * @throws Exception: 10 * 11 */ 12 public class Bee extends FlyingObject implements Award { 13 //自身属性 14 private int Xspeed; 15 private int yspeed; 16 private int awardType; 17 18 /** 19 * getType description: 获取奖励形式 20 * 21 * @param 22 * @return void 23 * @throws Exception: 24 * 25 */ 26 public void getType(){} 27 28 //初始化属性 29 public Bee(){ 30 int x; 31 int y; 32 int width; 33 int height; 34 int speed; 35 BufferedImage image; 36 } 37 38 //继承方法-移动 39 public void step(int x,int y){ 40 41 } 42 43 //实现方法-获取奖励形式 44 public void awardType(){ 45 46 } 47 }
1 package com.raizosight.starcraft; 2 3 import java.awt.image.BufferedImage; 4 5 /** 6 * Bullet.java description: 子弹类 7 * @author raizoo 8 * Created on 17-7-21 下午3:34. 9 * @version 1.1 10 * @since jDK1.8 11 * @throws Exception: 12 * 13 */ 14 public class Bullet extends FlyingObject{ 15 //自身属性 16 17 //初始化属性 18 public Bullet(){ 19 int x; 20 int y; 21 int width; 22 int height; 23 int speed; 24 BufferedImage image; 25 } 26 27 //继承方法-移动 28 public void step(int x,int y){ 29 this.x = x; 30 this.y = y; 31 } 32 33 }
1 package com.raizosight.starcraft; 2 3 import java.awt.image.BufferedImage; 4 5 /** 6 * Hero.java description: 英雄机 7 * @author raizoo 8 * Created on 17-7-21 下午3:40. 9 * @version 1.1 10 * @since JDK1.8 11 * @throws Exception: 12 * 13 */ 14 public class Hero extends FlyingObject{ 15 //自身属性 16 private int life = 3; 17 18 //初始化属性 19 public Hero(){ 20 int x; 21 int y; 22 int width; 23 int height; 24 int speed; 25 BufferedImage image; 26 } 27 28 //继承方法-移动 29 public void step(int x,int y){ 30 31 } 32 }
1 package com.raizosight.starcraft; 2 import java.awt.image.BufferedImage; 3 import java.io.IOException; 4 5 import javax.imageio.ImageIO; 6 import javax.swing.JFrame; 7 import javax.swing.JPanel; 8 9 /** 10 * StarCraft.java description: 主程序 11 * @author raizoo 12 * Created on 17-7-21 下午3:44. 13 * @version 1.1 14 * @since JDK1.8 15 * @throws Exception: IOException I/O流 16 * 17 */ 18 public class StarCraft extends JPanel{ 19 //定义自身属性 20 public static final int WIDTH = 400; //框架宽 21 public static final int HEIGHT = 654; //框架高 22 23 //定义图片 24 public static BufferedImage start; //开始图片 25 public static BufferedImage pause; //暂停图片 26 public static BufferedImage gameover; //结束图片 27 public static BufferedImage background; //背景图片 28 public static BufferedImage airplane; //敌机图 29 public static BufferedImage bee; //蜜蜂图 30 public static BufferedImage bullet; //子弹图 31 public static BufferedImage hero0; //英雄机图0 32 public static BufferedImage hero1; //英雄机图1 33 34 //静态资源导入(图片等资源)-程序加载静态方法时一次导入 35 static{ 36 try { 37 start = ImageIO.read(StarCraft.class.getResource("start.png")); //开始 38 pause = ImageIO.read(StarCraft.class.getResource("pause.png")); //暂停 39 gameover = ImageIO.read(StarCraft.class.getResource("gameover.png")); //结束 40 background = ImageIO.read(StarCraft.class.getResource("background.png")); //背景 41 airplane = ImageIO.read(StarCraft.class.getResource("airplane.png")); //敌机 42 bee = ImageIO.read(StarCraft.class.getResource("bee.png")); //蜜蜂 43 bullet = ImageIO.read(StarCraft.class.getResource("bullet.png")); //子弹 44 hero0 = ImageIO.read(StarCraft.class.getResource("hero0.png")); //英雄机0 45 hero1 = ImageIO.read(StarCraft.class.getResource("hero1.png")); //英雄机1 46 47 } catch (IOException e) { 48 e.printStackTrace(); 49 } 50 51 } 52 53 54 55 56 //主方法 57 public static void main(String[] args){} 58 }