演示视频
JAVA练手小游戏——飞机大战
代码实现
AirPlane类
1. public class AirPlan extends Role implements Enemy { 2. private int speed = 3; 3. 4. /** 5. * 初始化数据 6. */ 7. public AirPlan() { 8. Random rand = new Random(); 9. this.bfImg = Resources.airplane; 10. this.width = bfImg.getWidth(); 11. this.height = bfImg.getHeight(); 12. this.y = -height; 13. this.x = rand.nextInt(Resources.WIDTH - width); 14. } 15. 16. /** 17. * 获取分数 18. */ 19. public int getScore() { 20. return 5; 21. } 22. 23. /** 24. * //越界处理 25. */ 26. @Override 27. public boolean outOfBounds() { 28. return y > Resources.HEIGHT; 29. } 30. 31. /** 32. * 移动 33. */ 34. @Override 35. public void step() { 36. y += speed; 37. } 38. 39. }
Bee类
1. public class Bee extends Role implements Award { 2. private int xSpeed = 1; 3. private int ySpeed = 2; 4. private int awardType; 5. 6. public Bee() { 7. this.bfImg = Resources.bee; 8. width = bfImg.getWidth(); 9. height = bfImg.getHeight(); 10. y = -height; 11. Random rand = new Random(); 12. x = rand.nextInt(Resources.WIDTH - width); 13. awardType = rand.nextInt(2); 14. } 15. 16. /** 17. * 获得奖励类型 18. */ 19. public int getType() { 20. return awardType; 21. } 22. 23. /** 24. * 越界处理 25. */ 26. @Override 27. public boolean outOfBounds() { 28. return y > Resources.HEIGHT; 29. } 30. 31. /** 32. * 移动,可斜着飞 33. */ 34. @Override 35. public void step() { 36. x += xSpeed; 37. y += ySpeed; 38. if (x > Resources.WIDTH - width) { 39. xSpeed = -1; 40. } 41. if (x < 0) { 42. xSpeed = 1; 43. } 44. } 45. 46. 47. }
Bullet类
1. public class Bullet extends Role { 2. private int speed = 3; 3. 4. /** 5. * 初始化数据 6. */ 7. public Bullet(int x, int y) { 8. this.x = x; 9. this.y = y; 10. this.bfImg = Resources.bullet; 11. } 12. 13. 14. /** 15. * 移动 16. */ 17. @Override 18. public void step() { 19. y -= speed; 20. } 21. 22. /** 23. * 越界处理 24. */ 25. @Override 26. public boolean outOfBounds() { 27. return y < -height; 28. } 29. }
Hero类
1. public class Hero extends Role { 2. private BufferedImage[] images = {}; 3. private int index = 0; 4. 5. private int doubleFire; 6. private int life; 7. 8. /** 9. * 初始化数据 10. */ 11. public Hero() { 12. life = 3; 13. doubleFire = 0; 14. images = new BufferedImage[]{Resources.hero0, Resources.hero1}; 15. bfImg = Resources.hero0; 16. width = bfImg.getWidth(); 17. height = bfImg.getHeight(); 18. x = 150; 19. y = 400; 20. 21. } 22. 23. 24. /** 25. * 设置双倍火力 26. */ 27. public void setDoubleFire(int doubleFire) { 28. this.doubleFire = doubleFire; 29. } 30. 31. /** 32. * 增加火力 33. */ 34. public void addDoubleFire() { 35. doubleFire = 40; 36. } 37. 38. /** 39. * 增命 40. */ 41. public void addLife() { //增命 42. life++; 43. } 44. 45. /** 46. * 减命 47. */ 48. public void subtractLife() { //减命 49. life--; 50. } 51. 52. /** 53. * 获取命 54. */ 55. public int getLife() { 56. return life; 57. } 58. 59. /** 60. * 当前物体移动了一下,相对距离,x,y鼠标位置 61. */ 62. public void moveTo(int x, int y) { 63. this.x = x - width / 2; 64. this.y = y - height / 2; 65. } 66. 67. /** 68. * 越界处理 69. */ 70. @Override 71. public boolean outOfBounds() { 72. return false; 73. } 74. 75. /** 76. * 发射子弹 77. */ 78. public ArrayList<Bullet> shoot() { 79. ArrayList<Bullet> bullets = new ArrayList<>(); 80. 81. int xStep = width / 4; 82. int yStep = 20; 83. if (doubleFire > 0) { 84. bullets.add(new Bullet(x + xStep, y - yStep)); 85. bullets.add(new Bullet(x + 3 * xStep, y - yStep)); 86. return bullets; 87. } else { 88. bullets.add(new Bullet(x + 2 * xStep, y - yStep)); 89. return bullets; 90. } 91. } 92. 93. /** 94. * 移动 95. */ 96. @Override 97. public void step() { 98. if (images.length > 0) { 99. bfImg = images[index++ / 10 % images.length]; 100. } 101. } 102. 103. /** 104. * 英雄机碰撞 105. */ 106. public boolean hit(Role other) { 107. int x1 = other.x - this.width / 2; 108. int x2 = other.x + this.width / 2 + other.width; 109. int y1 = other.y - this.height / 2; 110. int y2 = other.y + this.height / 2 + other.height; 111. 112. int herox = this.x + this.width / 2; 113. int heroy = this.y + this.height / 2; 114. 115. return herox > x1 && herox < x2 && heroy > y1 && heroy < y2; 116. } 117. }
Role类
1. public abstract class Role { 2. protected int x; 3. protected int y; 4. protected int width; 5. protected int height; 6. protected BufferedImage bfImg; 7. 8. public int getX() { 9. return x; 10. } 11. 12. public void setX(int x) { 13. this.x = x; 14. } 15. 16. public int getY() { 17. return y; 18. } 19. 20. public void setY(int y) { 21. this.y = y; 22. } 23. 24. public int getWidth() { 25. return width; 26. } 27. 28. public void setWidth(int width) { 29. this.width = width; 30. } 31. 32. public int getHeight() { 33. return height; 34. } 35. 36. public void setHeight(int height) { 37. this.height = height; 38. } 39. 40. public BufferedImage getImage() { 41. return bfImg; 42. } 43. 44. public void setImage(BufferedImage image) { 45. this.bfImg = image; 46. } 47. 48. 49. /** 50. * 检查是否出界 51. * 52. * @return true 出界与否 53. */ 54. public abstract boolean outOfBounds(); 55. 56. /** 57. * 飞行物移动一步 58. */ 59. public abstract void step(); 60. 61. /** 62. * 是否被击中 63. * 64. * @param bullet 65. * @return 66. */ 67. public boolean shootBy(Bullet bullet) { 68. boolean flag = false; 69. int bulletX = bullet.getX(); 70. int bulletY = bullet.getY(); 71. 72. 73. if (this.x < bulletX && bulletX < (this.x + this.width) 74. && this.y < bulletY && bulletY < (this.y + this.height)) { 75. flag = true; 76. } 77. return flag; 78. } 79. }
StateEnum
1. public enum StateEnum { 2. START, RUNNING, PAUSE, OVER 3. }
Resource类
1. public class Resources { 2. 3. public static final int WIDTH = 400; 4. public static final int HEIGHT = 654; 5. 6. public static BufferedImage background; 7. public static BufferedImage start; 8. public static BufferedImage airplane; 9. public static BufferedImage bee; 10. public static BufferedImage bullet; 11. public static BufferedImage hero0; 12. public static BufferedImage hero1; 13. public static BufferedImage pause; 14. public static BufferedImage gameover; 15. 16. /** 17. * 加载资源 18. * @param resourceDir 19. */ 20. public void load(String resourceDir) { 21. try { 22. background = ImageIO.read(this.getClass().getClassLoader(). 23. getResourceAsStream(resourceDir + "background.png")); 24. start = ImageIO.read(this.getClass().getClassLoader(). 25. getResourceAsStream(resourceDir + "start.png")); 26. airplane = ImageIO.read(this.getClass().getClassLoader(). 27. getResourceAsStream(resourceDir + "airplane.png")); 28. bee = ImageIO.read(this.getClass().getClassLoader(). 29. getResourceAsStream(resourceDir + "bee.png")); 30. bullet = ImageIO.read(this.getClass().getClassLoader(). 31. getResourceAsStream(resourceDir + "bullet.png")); 32. hero0 = ImageIO.read(this.getClass().getClassLoader(). 33. getResourceAsStream(resourceDir + "hero0.png")); 34. hero1 = ImageIO.read(this.getClass().getClassLoader(). 35. getResourceAsStream(resourceDir + "hero1.png")); 36. pause = ImageIO.read(this.getClass().getClassLoader(). 37. getResourceAsStream(resourceDir + "pause.png")); 38. gameover = ImageIO.read(this.getClass().getClassLoader(). 39. getResourceAsStream(resourceDir + "gameover.png")); 40. } catch (Exception e) { 41. e.printStackTrace(); 42. } 43. } 44. 45. }
PlaneWarFarme类
1. public class PlaneWarGameFrame extends JPanel { 2. // 分数值与生命值坐标 3. private static final int SCORE_X = 10; 4. private static final int SCORE_Y = 25; 5. private static final int LIFE_X = 10; 6. private static final int LIFE_Y = 45; 7. 8. // 游戏资源 9. private RulesGame rulesGame; 10. private ArrayList<Role> shootObjects; 11. private ArrayList<Bullet> bullets; 12. private Hero hero; 13. 14. public PlaneWarGameFrame(RulesGame rulesGame) { 15. this.rulesGame = rulesGame; 16. this.shootObjects = rulesGame.getShootObjects(); 17. this.hero = rulesGame.getHero(); 18. this.bullets = rulesGame.getBullets(); 19. 20. JFrame jFrame = new JFrame("飞机大战"); 21. jFrame.add(this); 22. jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 23. jFrame.setSize(Resources.WIDTH, Resources.HEIGHT); 24. jFrame.setAlwaysOnTop(true); 25. jFrame.setLocationRelativeTo(null); 26. jFrame.setVisible(true); 27. MouseHandler mouseHandler = new MouseHandler(rulesGame); 28. this.addMouseListener(mouseHandler); 29. this.addMouseMotionListener(mouseHandler); 30. } 31. 32. 33. /** 34. * repaint方法会调用paint方法, 35. */ 36. @Override 37. public void paint(Graphics g) { 38. super.paint(g); 39. paintBackground(g); 40. paintHero(g); 41. paintShootObjects(g); 42. paintBullets(g); 43. paintScore(g); 44. paintState(g); 45. } 46. 47. /** 48. * 画游戏状态 49. */ 50. public void paintState(Graphics g) { 51. switch (STATE) { 52. case START: 53. g.drawImage(Resources.start, 0, 0, null); 54. break; 55. case PAUSE: 56. g.drawImage(Resources.pause, 0, 0, null); 57. break; 58. case OVER: 59. g.drawImage(Resources.gameover, 0, 0, null); 60. break; 61. case RUNNING: 62. break; 63. } 64. } 65. 66. /** 67. * 画分数 68. */ 69. public void paintScore(Graphics g) { 70. Font font = new Font(Font.SANS_SERIF, Font.BOLD, 14); 71. g.setColor(new Color(0x3A3B3B)); 72. g.setFont(font); 73. g.drawString("SCORE:" + rulesGame.getScore(), SCORE_X, SCORE_Y); 74. g.drawString("LIFE:" + hero.getLife(), LIFE_X, LIFE_Y); 75. } 76. 77. /** 78. * 画子弹 79. */ 80. public void paintBullets(Graphics g) { 81. Iterator<Bullet> iterator = bullets.iterator(); 82. while (iterator.hasNext()) { 83. Bullet b = iterator.next(); 84. g.drawImage(b.getImage(), b.getX() - b.getWidth() / 2, 85. b.getY(), null); 86. } 87. } 88. 89. 90. private void paintHero(Graphics g) { 91. g.drawImage(hero.getImage(), hero.getX(), 92. hero.getY(), null); 93. } 94. 95. private void paintBackground(Graphics g) { 96. g.drawImage(Resources.background, 97. 0, 0, null); 98. } 99. 100. /** 101. * 画飞行物 102. */ 103. private void paintShootObjects(Graphics g) { 104. Iterator<Role> iterator = shootObjects.iterator(); 105. while (iterator.hasNext()) { 106. Role shootObj = iterator.next(); 107. g.drawImage(shootObj.getImage(), shootObj.getX(), 108. shootObj.getY(), null); 109. } 110. } 111. }
MainAPP类
1. public class MainApp { 2. public static void main(String[] args) { 3. PlanWarGameFactory planWarGameFactory = new PlanWarGameFactory(); 4. Game game = planWarGameFactory.creatGame(); 5. game.start(); 6. } 7. }