飞机大战(Java)

深碍√TFBOYSˉ_ 2022-10-20 12:14 243阅读 0赞

学习笔记分享,引用请写明出处
Main类

  1. package PlaneWar;
  2. import javax.swing.*;
  3. public class Main extends JPanel
  4. {
  5. static int weight = 600;
  6. static int height = 800;
  7. public static void main(String[] args)
  8. {
  9. JFrame frame = new JFrame();
  10. frame.setTitle("PlaneWar By Hsing");
  11. frame.setSize(weight,height);
  12. frame.setDefaultCloseOperation(3);
  13. frame.setLocationRelativeTo(null);
  14. //创建并添加面板对象;
  15. GamePanel panel = new GamePanel();
  16. frame.add(panel);
  17. //添加监听器;
  18. frame.addMouseMotionListener(panel);
  19. frame.setVisible(true);
  20. //初始化游戏;
  21. panel.init();
  22. }
  23. }

GamePanel类

  1. package PlaneWar;
  2. import javax.swing.*;
  3. import java.awt.event.*;
  4. import java.awt.*;
  5. import java.util.List;
  6. import java.util.*;
  7. public class GamePanel extends JPanel implements MouseMotionListener
  8. {
  9. //创建英雄机画像对象;
  10. ImageIcon heroImageIcon = new ImageIcon("D:/JDK/PlaneWar/Domo/Hero.png");
  11. //创建logo画像对象;
  12. ImageIcon logo = new ImageIcon("D:/JDK/PlaneWar/Domo/logo.png");
  13. //list存在于java.util.List,这里使用了默认包中的泛类;
  14. //创建一个list,用于存放敌人对象;
  15. List<Enemy> enemy = new ArrayList<Enemy>();
  16. //创建一个list,用于存放子弹对象;
  17. List<Butter> butter = new ArrayList<Butter>();
  18. //创建一个list,用来存放爆炸物对象;
  19. List<Bumb> bomb = new ArrayList<Bumb>();
  20. //定义英雄机的初始坐标;
  21. int herox = 300;
  22. int heroy = 400;
  23. //定义一个专用来记分的变量nub;
  24. int nub = 0;
  25. //绘图方,继承于JPanel,类Graphics存在于java.awt.*中;
  26. public void paint(Graphics page)
  27. {
  28. super.paint(page);
  29. //logo进行绘图,通过getImage()方法得到logo对象的图像,置于(400,20)处;
  30. page.drawImage(logo.getImage(), 400, 20, null);
  31. //在page中创建一个标签对象,蓝色,30号大小,字体使用默认字体;
  32. page.setFont(new Font("",Color.blue.getBlue( ),30));
  33. //在标签上写上内容,置于(10,30)的位置;
  34. page.drawString("当前得分:"+nub, 10, 30);
  35. //绘制英雄机,调用getImage()方法得到英雄机图像,绘制在(herox,heroy)坐标上;
  36. page.drawImage(heroImageIcon.getImage(), herox, heroy, null);
  37. //遍历敌人list,在list中得到对应的敌人对象;
  38. for(int i=0;i<enemy.size();i++)
  39. {
  40. Enemy enemyone = enemy.get(i);
  41. //调用敌人类的drawImage()方法,在page上绘制敌人;
  42. enemyone.drawImage(page);
  43. }
  44. //遍历子弹list,在list中得到子弹对象;
  45. for(int i=0;i<butter.size();i++)
  46. {
  47. Butter butterone = butter.get(i);
  48. //调用子弹类中drawlist()方法,在page上绘制敌人;
  49. butterone.drawImage(page);
  50. }
  51. //遍历爆炸list,在list中得到爆炸对象;
  52. for(int i=0;i<bomb.size();i++)
  53. {
  54. Bumb bombone = bomb.get(i);
  55. //调用爆炸类中的drawImage()方法,在page上绘制敌人;
  56. bombone.drawImage(page);
  57. }
  58. }
  59. //构建关于英雄机的监听器方法,监听鼠标事件;
  60. public void mouseMoved(MouseEvent event)
  61. {
  62. //得到鼠标所在的坐标;
  63. int x = event.getX();
  64. int y = event.getY();
  65. //将英雄机的坐标更改为鼠标所在位置并调整,使鼠标永远在英雄机中心;
  66. herox = x - (heroImageIcon.getIconWidth()/2);
  67. heroy = y - (heroImageIcon.getIconHeight()/2);
  68. //重画英雄机;
  69. repaint();
  70. }
  71. public void mouseDragged(MouseEvent event){ }//其他鼠标事件;
  72. //游戏的主要部分(构造方法)
  73. public GamePanel()
  74. {
  75. //在敌人list中加入10个新的敌人对象;
  76. for(int i=0;i<10;i++)
  77. {
  78. enemy.add(new Enemy());
  79. }
  80. }
  81. //构建初始化方法
  82. public void init()
  83. {
  84. int flag=0;
  85. //无限循环
  86. for(;;)
  87. {
  88. flag++;
  89. //每循环十五次;
  90. if(flag%15==0)
  91. {
  92. //创建一个新的子弹对象;就在英雄机所在的位之上,并调整使在英雄机中间
  93. Butter butterone = new Butter(herox+(heroImageIcon.getIconWidth()/2), heroy);
  94. //将这个对象添加进子弹list;
  95. butter.add(butterone);
  96. }
  97. //实现敌人移动;
  98. for(int i=0;i<enemy.size();i++)
  99. {
  100. //从敌人list中的得到一个敌人对象;
  101. Enemy enemyone = enemy.get(i);
  102. //调用敌人类move()方法实现移动;
  103. enemyone.move();
  104. //当出现超过游戏界面时
  105. if(enemyone.getY()>Main.height)
  106. {
  107. //敌人list删除这个对象
  108. enemy.remove(enemyone);
  109. //删除后在list中添加一个新的敌人对象
  110. enemy.add(new Enemy());
  111. }
  112. }
  113. //实现子弹移动;
  114. for(int i=0;i<butter.size();i++)
  115. {
  116. //从子弹list中得到一个子弹对象;
  117. Butter tempbutter = butter.get(i);
  118. //调用子弹类move()方法实现子弹移动;
  119. tempbutter.move();
  120. //当子弹发射过界时,从子弹list中删除这个对象;
  121. if(tempbutter.getY()<0)
  122. {
  123. butter.remove(tempbutter);
  124. }
  125. }
  126. for(int i=0;i<enemy.size();i++)
  127. {
  128. //从敌人list中得到一个敌人对象;
  129. Enemy enemyone = enemy.get(i);
  130. for(int j=0;j<butter.size();j++)
  131. {
  132. //从子弹list中得到一个子弹对象;
  133. Butter onebutter = butter.get(j);
  134. //调用isHit()方法判断,如果出现子弹与敌人发生碰撞;
  135. if(isHit(enemyone,onebutter))
  136. {
  137. //删除这个子弹;
  138. butter.remove(onebutter);
  139. //创建一个爆炸对象,并添加到爆炸list中去,将爆炸定位在碰撞后的敌机位置;
  140. bomb.add(new Bumb(enemyone.getX(), enemyone.getY()));
  141. //得分+10;
  142. nub+=10;
  143. //删除这个被碰撞的敌人;
  144. enemy.remove(enemyone);
  145. //在增加一个新的敌人对象;
  146. enemy.add(new Enemy());
  147. }
  148. }
  149. }
  150. //删除爆炸
  151. for(int i=0;i<bomb.size();i++)
  152. {
  153. //得到爆炸list中一个爆炸对象;
  154. Bumb bombone = bomb.get(i);
  155. //调用爆炸类中的move()方法计数
  156. bombone.move();
  157. //十次循环后删除这个对象;
  158. if(bombone.getCount()>10)
  159. {
  160. bomb.remove(bombone);
  161. }
  162. }
  163. //睡眠5s后,将重绘界面;
  164. try
  165. {
  166. Thread.sleep(5);
  167. }
  168. catch(InterruptedException event)
  169. {
  170. event.printStackTrace();
  171. }
  172. repaint();
  173. }
  174. }
  175. //创建判断碰撞的方法;
  176. public boolean isHit(Enemy e, Butter b)
  177. {
  178. //子弹与敌人机的图片发生重合,即为击中敌人;
  179. Rectangle rect = new Rectangle(e.getX(),e.getY(),b.getWidth(),b.getHight());
  180. Point p = new Point(b.getX()+b.getWidth()/2,b.getY()+b.getHight());
  181. return rect.contains(p);//包含方法;
  182. }
  183. }

Hero类

  1. package PlaneWar;
  2. import javax.swing.*;
  3. public class Hero
  4. {
  5. private int width;
  6. private int hight;
  7. private int x;
  8. private int y;
  9. private ImageIcon heroImageIcon = new ImageIcon("D:/JDK/PlaneWar/Domo/Hero.png");
  10. public Hero()
  11. {
  12. this.width = heroImageIcon.getIconWidth();
  13. this.hight = heroImageIcon.getIconHeight();
  14. }
  15. public int getX()
  16. {
  17. return x;
  18. }
  19. public int getY()
  20. {
  21. return y;
  22. }
  23. public int getHight()
  24. {
  25. return hight;
  26. }
  27. public int getWidth()
  28. {
  29. return width;
  30. }
  31. }

敌人类

  1. package PlaneWar;
  2. import java.util.Random;
  3. import java.awt.*;
  4. import javax.swing.*;
  5. public class Enemy
  6. {
  7. private int width;
  8. private int hight;
  9. private int x;
  10. private int y;
  11. private ImageIcon enemyIcon = new ImageIcon("D:/JDK/PlaneWar/Domo/enemy.png");
  12. public Enemy()
  13. {
  14. width = enemyIcon.getIconWidth();
  15. hight = enemyIcon.getIconHeight();
  16. //设置敌机位置,在界面范位内随机;
  17. Random random = new Random();
  18. this.x = random.nextInt(Main.weight-(width/2));
  19. this.y = -random.nextInt(Main.height-(hight/2));
  20. }
  21. //设置敌机移动方法,每一次向下增加4个单位长度;
  22. public void move()
  23. {
  24. this.y += 4;
  25. }
  26. //敌人绘制方法
  27. public void drawImage(Graphics page)
  28. {
  29. //在page画布上,(x,y)坐标上绘出敌人的画像;
  30. page.drawImage(enemyIcon.getImage(), x, y, null);
  31. }
  32. public int getY()
  33. {
  34. return y;
  35. }
  36. public int getX()
  37. {
  38. return x;
  39. }
  40. }

子弹类

  1. package PlaneWar;
  2. import javax.swing.ImageIcon;
  3. import java.awt.*;
  4. public class Butter
  5. {
  6. private int x;
  7. private int y;
  8. private int hight;
  9. private int width;
  10. private ImageIcon butterIcon = new ImageIcon("D:/JDK/PlaneWar/Domo/buttle.png");
  11. public Butter(int x,int y)
  12. {
  13. this.x = x;
  14. this.y = y;
  15. this.hight = butterIcon.getIconHeight();
  16. this.width = butterIcon.getIconWidth();
  17. }
  18. //子弹的移动方法,每次向上移动四个单位长度;
  19. public void move()
  20. {
  21. this.y -= 4;
  22. }
  23. public void drawImage(Graphics page)
  24. {
  25. page.drawImage(butterIcon.getImage(), x, y, null);
  26. }
  27. public int getY()
  28. {
  29. return y;
  30. }
  31. public int getX()
  32. {
  33. return x;
  34. }
  35. public int getHight()
  36. {
  37. int temp = hight;
  38. return temp;
  39. }
  40. public int getWidth()
  41. {
  42. int temp = width;
  43. return temp;
  44. }
  45. }

爆炸类

  1. package PlaneWar;
  2. import javax.swing.*;
  3. import java.awt.*;
  4. public class Bumb
  5. {
  6. private int x;
  7. private int y;
  8. private int width;
  9. private int height;
  10. private int count;
  11. private ImageIcon bombIcon = new ImageIcon("D:/JDK/PlaneWar/Domo/Boon.png");
  12. //将爆炸坐标定为碰撞后的敌机;
  13. public Bumb(int x,int y)
  14. {
  15. this.x = x;
  16. this.y = y;
  17. this.width = bombIcon.getIconWidth();
  18. this.height = bombIcon.getIconHeight();
  19. }
  20. public int getX()
  21. {
  22. return x;
  23. }
  24. public int getY()
  25. {
  26. return y;
  27. }
  28. public void drawImage(Graphics page)
  29. {
  30. page.drawImage(bombIcon.getImage(), x, y, null);
  31. }
  32. //计数方法;
  33. public void move()
  34. {
  35. count++;
  36. }
  37. public int getCount()
  38. {
  39. return count;
  40. }
  41. public int getWidth()
  42. {
  43. return width;
  44. }
  45. public int getHight()
  46. {
  47. return height;
  48. }
  49. }

有些方法根本没用,是我自己强迫症忍不住写的

发表评论

表情:
评论列表 (有 0 条评论,243人围观)

还没有评论,来说两句吧...

相关阅读

    相关 飞机大战Java版完整版

    这是一款闯关的射击游戏,人物在不同的关 卡里面会触发不同的技能与对应的特效操作,有三个关卡与四个随机事件,每个关卡里面都会触发不同数量的怪物与能量血瓶,通过打败怪物与 到达特点

    相关 Java飞机大战项目

    飞机大战游戏是一款十分有趣的射击类小游戏,流畅的画面,高难度的挑战。游戏中,玩家驾驶英雄机,在空中进行战斗。点击并移动自己的英雄机,发射炮弹,打掉敌飞机以及蜜蜂,来获得分数和奖