IDEA小游戏

游戏界面使用JFrame和JPanel构建。背景图通过BG类绘制。英雄机和敌机在界面上显示并移动。子弹从英雄机发射并在屏幕上移动。游戏有四种状态:READY、RUNNING、PAUSE、GAMEOVER。状态通过鼠标点击进行切换:点击开始游戏(从READY变为RUNNING)。点击重新开始游戏(从GAMEOVER变为READY)。鼠标移出窗口时游戏暂停(从RUNNING变为PAUSE)。鼠标移入窗口时游戏继续(从PAUSE变为RUNNING)。开局默认玩家生命值为3,分数为0。每隔一定时间随机生成敌机(小飞机、大飞机、小飞碟)。敌机在屏幕上移动,并与英雄机进行碰撞检测。英雄机可以发射子弹。子弹在屏幕上移动,并与敌机进行碰撞检测。英雄机与敌机发生碰撞时,英雄机会损失一条生命值。子弹与敌机发生碰撞时,敌机会被摧毁,玩家的分增加。当子弹与奖励机发生碰撞时,奖励机会被摧毁,玩家随机获得两张奖励(生命值加1、双倍子弹)。在屏幕左上角显示当前的生命值和得分。生命值减少到0时,游戏进入结束状态。再次点击,重新开始游戏。

1.图片素材(百度网盘分享)

链接:https://pan.baidu.com/s/10QiTcddI_Zxw5jbn8fmOpA?pwd=huan 
提取码:huan

2.主页面world.java

解释

  1. package plane.gzeu;

  2. import javax.swing.*;

  3. import java.awt.*;

  4. import java.awt.event.MouseAdapter;

  5. import java.awt.event.MouseEvent;

  6. import java.util.Arrays;

  7. import java.util.Timer;

  8. import java.util.TimerTask;

  9. public class World extends JPanel {

  10. //ImageIcon类:保存图片的路径----->读取图片

  11. // ImageIcon icon = new ImageIcon("images\\background.png");

  12. // BG bg = new BG(0,0,420,700,2);

  13. // Airplane airplane = new Airplane(100,200,300,620,2);

  14. // Bee bee = new Bee(200,100,200,700,2);

  15. BG bg = new BG();

  16. EnemyPlane [] planes;

  17. Hero hero;

  18. int index; //用来控制敌机的数量

  19. Bullet[] bullets; //创建子弹的数组

  20. int life; //英雄机的生命值

  21. int score; //分数

  22. //定义游戏的四种状态:准备,运行,暂停,结束(常量)

  23. public static final int READY = 0;

  24. public static final int RUNNING = 1;

  25. public static final int PAUSE = 2;

  26. public static final int GAMEOVER = 3;

  27. //游戏的初始状态

  28. int state = READY;

  29. // Airplane [] airPlanes = new Airplane[10];

  30. // Bigairplane [] bigAirPlanes = new Bigairplane[10];

  31. // Bee [] bee = new Bee[10];

  32. // Bom [] bom = new Bom[4];

  33. // Bullet [] bullets = new Bullet[3];

  34. // Hero [] hero =new Hero[2];

  35. //初始化方法

  36. public void init(){

  37. bg = new BG();

  38. hero = new Hero();

  39. planes = new EnemyPlane[0]; //默认一个敌机都没有

  40. bullets = new Bullet[0]; //默认一个子弹都没有

  41. index = 0; //默认从0开始

  42. life = 3; //默认3条命

  43. score = 0; //分数为0

  44. }

  45. public World() {

  46. init();

  47. // for(int i=0;i<planes.length;i++){

  48. // if(i%2==0){

  49. // planes[i]=new Airplane();

  50. // }

  51. // else if(i%5==0 && i%2!=0){

  52. // planes[i] = new Bee();

  53. // }

  54. // else {

  55. // planes[i] = new Bigairplane();

  56. // }

  57. // }

  58. // for(int i =0;i<airPlanes.length;i++){

  59. // airPlanes[i] = new Airplane();

  60. // bigAirPlanes[i]=new BigAirPlane();

  61. // bee[i]=new Bee();

  62. // bom[i]=new Bom();

  63. // bullets[i]=new Bullet();

  64. // hero[i]=new Hero();

  65. }

  66. //生成敌机的方法

  67. public void createPlane(){

  68. if(index%16==0){

  69. int n = (int)(Math.random()*10);

  70. EnemyPlane plane;

  71. switch (n){

  72. case 9 :

  73. plane = new Bee(); //10%生成小飞碟

  74. break;

  75. case 8 :

  76. case 7 :

  77. plane = new Bigairplane(); //20%生成大飞机

  78. break;

  79. default:

  80. plane = new Airplane(); //70%生成小飞机

  81. }

  82. //将敌机存入数组中之前,要先对数组进行扩容处理

  83. planes = Arrays.copyOf(planes,planes.length+1);

  84. //将生存的敌机放入数组的最后一个位置

  85. planes[planes.length-1] = plane;

  86. }

  87. }

  88. // for(int i =0;i<bigAirPlanes.length;i++){

  89. // bigAirPlanes[i]=new Bigairplane();

  90. // }

  91. // }

  92. //绘制图片printing

  93. @Override

  94. public void paint(Graphics g) { //paint挂载到当前类,当前类实例化(创建对象)时自动调用

  95. // super.paint(g);

  96. // icon.paintIcon(this,g,0,y1++); //绘制背景,默认第一个绘制

  97. // bg.icon.paintIcon(this,g,(int)bg.x,(int)bg.y);

  98. // bg.move();

  99. // airplane.icon.paintIcon(this,g,(int)airplane.x,(int)airplane.y);

  100. // bee.icon.paintIcon(this,g,(int)bee.x,(int)bee.y);

  101. bg.painting(g);

  102. hero.painting(g);

  103. for(int i=0;i<planes.length;i++){

  104. planes[i].painting(g);

  105. }

  106. for(int i=0;i<bullets.length;i++){

  107. bullets[i].painting(g);

  108. }

  109. g.setColor(new Color(255,255,255));

  110. //设置字体

  111. g.setFont(new Font("微软雅黑",Font.BOLD,20));

  112. g.drawString("生命值"+life,20,20);

  113. g.drawString("分数"+score,20,40);

  114. if(state==READY){

  115. Images.start.paintIcon(this,g,0,0);

  116. }

  117. if(state==PAUSE){

  118. Images.pause.paintIcon(this,g,0,0);

  119. }

  120. if(state==GAMEOVER){

  121. Images.gameover.paintIcon(this,g,0,0);

  122. }

  123. // for(int i =0;i<airPlanes.length;i++){

  124. // airPlanes[i].painting(g);

  125. // bigAirPlanes[i].painting(g);

  126. // bee[i].painting(g);

  127. // bom[i].painting(g);

  128. // bullets[i].painting(g);

  129. // hero[i].painting(g);

  130. // }

  131. // for(int i =0;i<bigAirPlanes.length;i++){

  132. // bigAirPlanes[i].painting(g);

  133. // }

  134. repaint();//刷新窗口

  135. }

  136. public static void main(String[] args) {

  137. //1.显示画框(外层):JFrame

  138. JFrame jf = new JFrame();

  139. //2.显示面板:Jpanel

  140. World jp =new World();

  141. //3.将面板放入画框中:

  142. jf.add(jp);

  143. //对窗口进行设置

  144. jf.setTitle("我的窗口");//设置标题

  145. jf.setSize(436,700); //设置窗口大小

  146. jf.setLocationRelativeTo(null); //居中显示

  147. jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //设置关闭窗口后自动结束程序

  148. //4.显示窗口

  149. jf.setVisible(true);

  150. jp.action();

  151. }

  152. //唤醒定时器的方法

  153. public void action(){

  154. java.util.Timer timer = new Timer();

  155. MyTesk mt = new MyTesk();

  156. timer.schedule(mt,1000,1000/50);

  157. this.addMouseListener(new MouseAdapter() {

  158. //监听鼠标点击

  159. @Override

  160. public void mouseClicked(MouseEvent e) {

  161. if(state==READY){

  162. state=RUNNING;

  163. }

  164. if(state==GAMEOVER){

  165. state=READY;

  166. init();

  167. }

  168. }

  169. //鼠标移除

  170. @Override

  171. public void mouseExited(MouseEvent e) {

  172. // System.out.println("鼠标移出");

  173. if(state==RUNNING){

  174. state=PAUSE;

  175. }

  176. }

  177. //鼠标移入

  178. @Override

  179. public void mouseEntered(MouseEvent e) {

  180. if(state==PAUSE){

  181. state=RUNNING;

  182. }

  183. }

  184. });

  185. //匿名内部类

  186. MouseAdapter mouseAdapter = new MouseAdapter() {

  187. @Override

  188. public void mouseMoved(MouseEvent e) {

  189. if(state==RUNNING){

  190. int x = e.getX();

  191. int y = e.getY();

  192. hero.move(x,y);

  193. }

  194. }

  195. };

  196. //安装监听器

  197. this.addMouseMotionListener(mouseAdapter);

  198. }

  199. public void createBullet(){

  200. if(!hero.isLiving()){ //英雄机死亡不发射子弹

  201. return;

  202. }

  203. if(index%17==0){

  204. Bullet[] bullets1 = hero.double_fire();

  205. //俩个数组的合并

  206. Bullet[] arr = Arrays.copyOf(bullets,bullets.length+bullets1.length);

  207. System.arraycopy(bullets1,0,arr,bullets.length,bullets1.length);

  208. bullets = arr;

  209. // Bullet bullet =hero.fire(); //英雄机调用fire方法生成子弹

  210. // //将子弹放入数组中

  211. // bullets = Arrays.copyOf(bullets,bullets.length+1); //子弹扩容

  212. // bullets[bullets.length-1] = bullet;

  213. }

  214. }

  215. //加分的方法

  216. public void addScore(FlyingObjesct plane){

  217. if(plane.isDead()){

  218. if(plane instanceof Enemy){

  219. Enemy enemy = (Enemy) plane; //需要做一个强制转化

  220. score += enemy.getScore();

  221. }

  222. if(plane instanceof Aword){

  223. Aword aword = (Aword) plane;

  224. int type =aword.getAword();

  225. if(type == Aword.DOUBLE_FIRE){

  226. hero.getdouble_fire();

  227. }

  228. if(type == Aword.LIFE){

  229. life++;

  230. }

  231. }

  232. }

  233. }

  234. //检测敌机与英雄机的碰撞

  235. public void hero_hit(){

  236. if(hero.isLiving()){

  237. for(int i=0;i<planes.length;i++){

  238. if(!planes[i].isLiving()){

  239. continue;

  240. }

  241. if(hero.bong(planes[i])){

  242. hero.goDead();

  243. planes[i].goDead();

  244. break;

  245. }

  246. }

  247. }

  248. else if(hero.isWait()){ //僵尸状态

  249. if(life>1){

  250. //重新开始

  251. hero = new Hero();

  252. //清屏

  253. for(int i=0;i<planes.length;i++){

  254. planes[i].goDead();

  255. }

  256. life--;

  257. }

  258. else{

  259. life--;

  260. state=GAMEOVER;

  261. }

  262. }

  263. }

  264. //检测每个敌机与子弹的碰撞情况

  265. public void hit(){

  266. for(int i=0;i<bullets.length;i++){

  267. Bullet bullet = bullets[i]; //拿出每一颗子弹

  268. if(!bullet.isLiving()){

  269. continue;

  270. }

  271. for(int j=0;j<planes.length;j++){

  272. FlyingObjesct p = planes[j];

  273. if(!p.isLiving()){

  274. continue;

  275. }

  276. if(p.bong(bullet)){ //被击中

  277. p.attack();

  278. bullet.goDead();

  279. addScore(p);

  280. }

  281. }

  282. }

  283. }

  284. //清理爆炸的飞机

  285. public void clean(){

  286. //清除飞机

  287. EnemyPlane[] living = new EnemyPlane[planes.length];

  288. int index = 0;

  289. for(int i=0;i<planes.length;i++){ //遍历敌机数组

  290. if(planes[i].isWait() || planes[i].outOfBound()){ //如果是等待状态的效果就跳过

  291. continue;

  292. }

  293. living[index++] = planes[i]; //将不是等待状态的敌机存在living里面

  294. }

  295. planes = Arrays.copyOf(living,index);

  296. //清除子弹

  297. Bullet[] livingBullet = new Bullet[bullets.length];

  298. index = 0;

  299. for(int i=0;i<bullets.length;i++){

  300. if(bullets[i].isDead() || bullets[i].outOfBound()){ //如果是已经爆炸的子弹就跳过

  301. continue;

  302. }

  303. livingBullet[index++] = bullets[i];

  304. }

  305. bullets = Arrays.copyOf(livingBullet,index);

  306. }

  307. class MyTesk extends TimerTask {

  308. @Override

  309. public void run() {

  310. index++;

  311. if(state == RUNNING){

  312. createPlane(); //调用生成敌机的方法

  313. createBullet(); //调用生成子弹的方法

  314. hit();//调用子弹击打的效果

  315. clean(); //调用清理敌机方法

  316. hero_hit();

  317. bg.move();

  318. for(int i =0;i<planes.length;i++){

  319. planes[i].move();

  320. }

  321. for(int i =0;i<bullets.length;i++){

  322. bullets[i].move();

  323. }

  324. }

  325. }

  326. }

  327. }

3.小飞机类Airplane.java

解释

  1. package plane.gzeu;

  2. import javax.swing.*;

  3. //飞机类

  4. public class Airplane extends EnemyPlane implements Enemy{ // 实现接口时,要实现接口中的方法

  5. public Airplane(){

  6. this.images = Images.airplane;

  7. w = images[0].getIconWidth();

  8. h = images[0].getIconHeight();

  9. x=(int)(Math.random()*(420-images[0].getIconWidth()));

  10. y=-2*images[0].getIconHeight();

  11. this.step=(Math.random()*2.5+0.7);

  12. this.icon=new ImageIcon("images\\airplane1.png");

  13. // this.icon = Images.airplane[0];

  14. }

  15. public Airplane(double x, double y, double w, double h,double speed) {

  16. // super(x, y, w, h);

  17. this.step=speed;

  18. this.icon=Images.airplane[0];

  19. }

  20. @Override

  21. public int getScore() {

  22. return 10;

  23. }

  24. }

4.大飞机类Bigairplane

解释

  1. package plane.gzeu;

  2. //大飞机类

  3. public class Bigairplane extends EnemyPlane implements Enemy{

  4. public Bigairplane(double x, double y, double w, double h,double step) {

  5. // super(x, y, w, h);

  6. this.step=step;

  7. this.icon = Images.bigairplane[0];

  8. }

  9. public Bigairplane(){

  10. this.images = Images.bigairplane; //初始化数组

  11. w = images[0].getIconWidth();

  12. h = images[0].getIconHeight();

  13. x=(int)(Math.random()*(420-images[0].getIconWidth()));

  14. y=-2*images[0].getIconHeight();

  15. this.step=(Math.random()*3.5+0.7);

  16. // this.icon = Images.bigairplane[0];

  17. life = 4;

  18. }

  19. @Override

  20. public int getScore() {

  21. return 50;

  22. }

  23. }

5.英雄机类Hero.java

解释

  1. package plane.gzeu;

  2. import javax.swing.*;

  3. import static plane.gzeu.Images.bullet;

  4. public class Hero extends EnemyPlane{

  5. public Hero(){

  6. this.images = Images.hero;

  7. x=431/2-images[0].getIconWidth()/2;

  8. y=510;

  9. w=images[0].getIconWidth();

  10. h=images[0].getIconHeight();

  11. // this.speed=0.2;

  12. this.icon = Images.hero[0];

  13. }

  14. public Hero(double x, double y, double w, double h,double step) {

  15. // super(x, y, w, h);

  16. this.step=step;

  17. // this.icon = new ImageIcon("images\\hero1.png"); //设置了Images类直接调用类名就可以引用图片

  18. this.icon = Images.hero[0];

  19. }

  20. @Override

  21. public void move() {}

  22. //接收鼠标的坐标

  23. public void move(int x,int y) {

  24. this.x= x-images[0].getIconWidth()/2;

  25. this.y = y-images[0].getIconHeight()/2;

  26. }

  27. /*

  28. * 子弹的位置(在英雄机上) b.x = h.x-images[0].getIconWidth()/2; b.y=h.y

  29. * 子弹的移动:向上:b.y-=b.step;

  30. * 子弹是无限的:数序扩容

  31. * 射击方法:当英雄机调用一次射击方法时,就发射一个子弹

  32. * */

  33. //射击的方法

  34. public Bullet fire(){

  35. double x = this.x+w/2-4; //获取英雄机的x坐标

  36. Bullet bullet = new Bullet(x,y);//将处理过后的坐标传给子弹

  37. return bullet; //将子弹返回

  38. }

  39. //获取双倍子弹的方法

  40. int doubleTime = 0; // 双倍子弹的时间,20次

  41. public void getdouble_fire(){

  42. doubleTime = 20;

  43. }

  44. //双倍子弹方法

  45. public Bullet[] double_fire(){

  46. if(doubleTime>0){

  47. double x = this.x+w/2-7;

  48. double x1 = this.x+w/2-2; //获取英雄机的x坐标

  49. Bullet bullet1 = new Bullet(x,y);

  50. Bullet bullet2 = new Bullet(x1,y);//将处理过后的坐标传给子弹

  51. Bullet [] bullets = new Bullet[]{bullet1,bullet2};

  52. // return new Bullet[]{bullet1,bullet2};

  53. return bullets;

  54. }

  55. else {

  56. double x = this.x+w/2-4; //获取英雄机的x坐标

  57. Bullet bullet1 = new Bullet(x,y);

  58. return new Bullet[]{bullet1};

  59. }

  60. //

  61. }

  62. //测试

  63. // public static void main(String[] args) {

  64. // Hero hero = new Hero();

  65. // hero.move(200,300);

  66. // Bullet bullet = hero.fire();

  67. // System.out.println(bullet.x+" "+bullet.y);

  68. // }

  69. }

6.敌机类Bee.java

解释

  1. package plane.gzeu;

  2. import javax.swing.*;

  3. public class Bee extends EnemyPlane implements Aword {

  4. double speed1;

  5. public Bee(){

  6. this.images = Images.bee;

  7. w = images[0].getIconWidth();

  8. h = images[0].getIconHeight();

  9. x=(int)(Math.random()*(420-images[0].getIconWidth()));

  10. y=-2*images[0].getIconHeight();

  11. this.step=(Math.random()*4.5+2.5);

  12. this.speed1=(Math.random()*3.5+0.5);

  13. // this.icon=new ImageIcon("images\\bee0.png");

  14. this.icon=Images.bee[0];

  15. life = 6;

  16. }

  17. public Bee(double x, double y, double w, double h,double step) {

  18. // super(x, y, w, h);

  19. this.step=step;

  20. this.icon = Images.bee[0];

  21. }

  22. @Override

  23. public void move() {

  24. y+=speed1;

  25. x+=step;

  26. if(x>431-images[0].getIconWidth()){

  27. step = -step;

  28. }

  29. if(x<=0){

  30. step = -step;

  31. }

  32. }

  33. @Override

  34. public int getAword() {

  35. return Math.random()>0.5?DOUBLE_FIRE:LIFE;

  36. }

  37. }

7.背景类BG.java

解释

  1. package plane.gzeu;

  2. import javax.swing.*;

  3. import java.awt.*;

  4. //背景类(子类)

  5. //继承FlyingObjesct类,得到FlyingObjesct类的方法及属性

  6. public class BG extends FlyingObjesct {

  7. double y0;

  8. public BG(){

  9. x = 0;

  10. y = 0;

  11. icon = Images.bg;

  12. w = icon.getIconWidth();

  13. h = icon.getIconHeight();

  14. y0 = -h;

  15. step = 2;

  16. }

  17. public BG(double x, double y, double w, double h,double step) {

  18. super(x, y, w, h); //重载:调用父类的构造方法,实现方法复用

  19. this.step = step;

  20. this.icon = Images.bg; //背景是固定的

  21. }

  22. public void painting(Graphics g){

  23. icon.paintIcon(null,g,(int)x,(int)y);

  24. icon.paintIcon(null,g,(int)x,(int)y0);

  25. }

  26. @Override

  27. public void move() {

  28. y+=step;

  29. y0+=step;

  30. if(y>=h){

  31. y=-h;

  32. }

  33. if(y0>=h){

  34. y0=-h;

  35. }

  36. }

  37. }

8.爆炸类Bom.java

解释

  1. package plane.gzeu;

  2. import javax.swing.*;

  3. public class Bom extends EnemyPlane{

  4. public Bom(){

  5. x=(int)(Math.random()*370);

  6. y=(int)(Math.random()*370);

  7. this.step = 0.2;

  8. this.icon = Images.bom[0];

  9. }

  10. public Bom(double x, double y, double w, double h, double speed) {//要重写父类的构造方法。新增一个速度

  11. // super(x, y, w, h);//重载:调用父类的构造方法,实现方法复用

  12. this.step=speed;

  13. this.icon=Images.bom[0];

  14. }

  15. }

9.子弹类Bullet.java

解释

  1. package plane.gzeu;

  2. import javax.swing.*;

  3. //继承FlyingObjesct类,得到FlyingObjesct类的方法及属性

  4. public class Bullet extends EnemyPlane {

  5. public Bullet(double x,double y){

  6. this.icon = Images.bullet;

  7. w = icon.getIconWidth();

  8. h = icon.getIconHeight();

  9. this.x = x;

  10. this.y = y;

  11. step = 2;

  12. }

  13. public Bullet(){

  14. x=(int)(Math.random()*370);

  15. y=(int)(Math.random()*370);

  16. this.step = 0.2;

  17. this.icon = Images.bullet;

  18. }

  19. public Bullet(double x, double y, double w, double h, double step) {

  20. // super(x, y, w, h); //重载:调用父类的构造方法,实现方法复用

  21. this.step = step;

  22. this.icon = Images.bullet; //背景是固定的

  23. }

  24. @Override

  25. public void move() {

  26. y-=step;

  27. }

  28. }

10.三种敌机总类EnemyPlane.java

解释

  1. package plane.gzeu;

  2. public abstract class EnemyPlane extends FlyingObjesct {

  3. @Override

  4. public void move() {

  5. y+=step;

  6. }

  7. }

11.飞行物的总类(父类)FlyingObjesct.java

解释

  1. package plane.gzeu;

  2. import javax.swing.*;

  3. import java.awt.*;

  4. //飞行物的总类(父类)

  5. public abstract class FlyingObjesct {

  6. public static final int LIVING = 1; //活着

  7. public static final int DEAD = 0; //死亡

  8. public static final int WAIT = -1; //等待死亡(播放爆炸动画)

  9. //飞机默认是活着的

  10. public int state = LIVING;

  11. //飞机的生命值

  12. public int life = 2;

  13. public double x,y,step,w,h;//step速度

  14. public ImageIcon icon; //图片

  15. // Images images;

  16. public ImageIcon[] images; //数组

  17. public int index = 0;

  18. public ImageIcon [] boms = Images.bom; //存放爆炸效果的图片

  19. //赋初值:构造方法

  20. //快速生成构建方法:快捷键Alt+Insert

  21. public FlyingObjesct(double x, double y, double w, double h) {

  22. this.x = x;

  23. this.y = y;

  24. this.w = w;

  25. this.h = h;

  26. }

  27. //当飞机击打一次,生命值减一,当生命值为0时,进入死亡状态

  28. public boolean attack(){

  29. if(life>0){

  30. life--;

  31. if(life==0){

  32. state = DEAD;

  33. }

  34. return true;

  35. }

  36. return false;

  37. }

  38. //让敌机快速去死

  39. public boolean goDead(){

  40. if(state==LIVING){

  41. life = 0;

  42. state = DEAD;

  43. return true;

  44. }else{

  45. return false;

  46. }

  47. }

  48. //判断飞机的三种状态

  49. public boolean isLiving(){

  50. return state==LIVING; //判断是否活着

  51. }

  52. public boolean isDead(){

  53. return state==DEAD; //判断是否死了

  54. }

  55. public boolean isWait(){

  56. return state==WAIT; //判断是否等待死亡

  57. }

  58. /*实现动画效果

  59. * 1.用数组存储动画效果图片

  60. * 2.初始化数组

  61. * 3.定义一个计数器,作为切换图片的控制条件

  62. * 4.写一个方法,实现切换图片的操作

  63. * */

  64. int i;

  65. public void nextImage(){

  66. switch (state){

  67. case LIVING:

  68. if(images == null) {

  69. return;

  70. }

  71. icon = images[index++/300%images.length]; //0-[数组的长度-1]

  72. break;

  73. case DEAD:

  74. if(boms==null){

  75. return;

  76. }

  77. if(i++/300==boms.length){

  78. state = WAIT;

  79. return;

  80. }

  81. icon = boms[i++/300];

  82. }

  83. }

  84. public FlyingObjesct(){}//无参构造

  85. //移动

  86. public abstract void move();

  87. //绘制图形 方法

  88. public void painting(Graphics g){

  89. nextImage();

  90. icon.paintIcon(null,g,(int)x,(int)y);

  91. }

  92. //检测敌机是否碰到子弹:判断条件中心距c=H/2+h/2

  93. public boolean bong(FlyingObjesct bullet){

  94. FlyingObjesct p =this;

  95. double a = Math.min(p.w,p.h)/2;

  96. double b = Math.min(bullet.w,bullet.h)/2;

  97. double x1=p.x+p.w/2;

  98. double y1=p.y+p.h/2;

  99. double x2=bullet.x+bullet.w/2;

  100. double y2=bullet.y+bullet.h/2;

  101. double c = Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));

  102. return c<=a+b;

  103. }

  104. //清除越界的敌机和子弹条件: y<-h-100 || y>700+100

  105. public boolean outOfBound(){

  106. if(y<-h-100){

  107. return true;

  108. }else if(y>700+100){

  109. return true;

  110. }

  111. return false;

  112. }

  113. }

12.图片类Images.java

解释

  1. package plane.gzeu;

  2. import javax.swing.*;

  3. public class Images {

  4. public static ImageIcon bg;

  5. public static ImageIcon bullet;

  6. public static ImageIcon start;

  7. public static ImageIcon pause;

  8. public static ImageIcon gameover;

  9. public static ImageIcon[] hero;

  10. public static ImageIcon[] bigairplane;

  11. public static ImageIcon[] bee;

  12. public static ImageIcon[] airplane;

  13. public static ImageIcon[] bom;

  14. static {

  15. airplane = new ImageIcon[2];

  16. airplane[0] = new ImageIcon("images/airplane0.png");

  17. airplane[1] = new ImageIcon("images/airplane1.png");

  18. bigairplane = new ImageIcon[2];

  19. bigairplane[0] = new ImageIcon("images/bigairplane0.png");

  20. bigairplane[1] = new ImageIcon("images/bigairplane1.png");

  21. bee = new ImageIcon[2];

  22. bee[0] = new ImageIcon("images\\bee0.png");

  23. bee[1] = new ImageIcon("images\\bee1.png");

  24. bom = new ImageIcon[4];

  25. bom[0] = new ImageIcon("images/bom1.png");

  26. bom[1] = new ImageIcon("images/bom2.png");

  27. bom[2] = new ImageIcon("images/bom3.png");

  28. bom[3] = new ImageIcon("images/bom4.png");

  29. bg = new ImageIcon("images\\background.png");

  30. bullet = new ImageIcon("images\\bullet.png");

  31. start = new ImageIcon("images\\start.png");

  32. pause = new ImageIcon("images\\pause.png");

  33. gameover = new ImageIcon("images\\gameover.png");

  34. hero = new ImageIcon[2];

  35. hero[0] = new ImageIcon("images\\hero0.png");

  36. hero[1] = new ImageIcon("images\\hero1.png");

  37. }

  38. //测试图片是否传过来了

  39. public static void main(String[] args) {

  40. System.out.println(start.getImageLoadStatus()); //如果输出8那图片传过来了,输出其他数字有问题

  41. System.out.println(bg.getImageLoadStatus());

  42. System.out.println(bom[0].getImageLoadStatus());

  43. System.out.println(bee[1].getImageLoadStatus());

  44. System.out.println(airplane[1].getImageLoadStatus());

  45. }

  46. }

13.获得奖励的接口Aword.java

解释

  1. package plane.gzeu;

  2. //获得奖励的接口

  3. public interface Aword {

  4. int DOUBLE_FIRE = 1; //第一种奖励,双倍火力

  5. int LIFE = 2; //加生命值

  6. int getAword(); //获得奖励的方法

  7. }

14.获得分数的接口Enemy.java

 
  1. package plane.gzeu;

  2. //获得分数的接口

  3. public interface Enemy { //加分

  4. // int a = 5;//都是静态常量

  5. // public abstract void add(); //都是抽象方法

  6. public int getScore(); //获得分数的方法

  7. }

运行界面:

开始界面

存活界面:

暂停界面

结束界面

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值