子弹超出界面删除子弹对象

实现思路:

    通过循环遍历获取子弹,调用子弹销毁方法.

    如果子弹的x坐标小于0或者大于界面宽度删除子弹对象.

    如果子弹的y坐标小于0或者大于界面高度删除子弹对象.

 

代码实现:

  1、设置类循环遍历子弹

 1 package Itheima;
 2 
 3 import java.util.List;
 4 import java.util.concurrent.CopyOnWriteArrayList;
 5 
 6 import org.itheima.game.Window;
 7 import org.lwjgl.input.Keyboard;
 8 
 9 import Itheima.bean.Bullet;
10 import Itheima.bean.Direction;
11 import Itheima.bean.Element;
12 import Itheima.bean.Tank;
13 import Itheima.bean.Wall;
14 
15 public class TankWorld extends Window {
16     Tank tank;
17     Wall wall;
18     List<Element> list;
19 
20     public TankWorld(String title, int width, int height, int fps) {
21         super(title, width, height, fps);
22         // TODO 自动生成的构造函数存根
23     }
24 
25     @Override
26     protected void onCreate() {
27         list = new CopyOnWriteArrayList<>();
28         tank = new Tank(0, 0);
29         wall = new Wall(64, 64);
30         list.add(tank);
31         list.add(wall);
32     }
33 
34     @Override
35     protected void onMouseEvent(int key, int x, int y) {
36         // TODO 自动生成的方法存根
37 
38     }
39 
40     @Override
41     protected void onKeyEvent(int key) {
42         switch (key) {
43         case Keyboard.KEY_UP:
44             tank.move(Direction.UP);
45             break;
46 
47         case Keyboard.KEY_DOWN:
48             tank.move(Direction.DOWN);
49             break;
50         case Keyboard.KEY_LEFT:
51             tank.move(Direction.LEFT);
52             break;
53         case Keyboard.KEY_RIGHT:
54             tank.move(Direction.RIGHT);
55             break;
56         case Keyboard.KEY_SPACE:
57             Bullet bullet = tank.fire();
58             list.add(bullet);
59             break;
60         }
61     }
62 
63     @Override
64     protected void onDisplayUpdate() {
65         for (Element e : list) {
66             e.draw();
67             System.out.println(list.size());
68         }
69 
70         
71         for (Element e1 : list) {
72             if(e1 instanceof Tank){    //遍历到Tank对象
73                 Tank t = (Tank) e1;    //将e1转换成Tank类
74                 for (Element e2 : list) {    
75                     if(e2 instanceof Wall){    //遍历到Wall对象
76                         Wall w = (Wall) e2;    //将e2转换成Wall对象
77                         boolean result = t.impact(w);    //判断Tank与Wall是否发生碰撞
78                         if(result){
79                             break;  //如果撞上了就跳出循环 不继续和下一个墙做判断
80                         }
81                     }
82                 }
83             }
84         }
85         
86         for (Element e1 : list) {
87             if(e1 instanceof Bullet){    //如果e1是子弹
88                 Bullet b = (Bullet) e1;    //将e1转换成子弹对象
89                 boolean result = b.isDestroy(); //调用子弹销毁方法
90                 if(result){    
91                     list.remove(b); //从集合删除子弹对象
92                 }
93             }
94         }
95     }
96 
97 }
TankWorld

  2、子弹类销毁方法

 1 package Itheima.bean;
 2 
 3 import java.io.IOException;
 4 
 5 import org.itheima.game.utils.DrawUtils;
 6 
 7 import Itheima.Constants;
 8 
 9 public class Bullet extends Element {
10     Direction dir;    
11     private int speed = 32;    //子弹速度
12     public Bullet(Tank tank) {
13         int x1 = tank.x;
14         int y1 = tank.y;
15         int w1 = tank.width;
16         int h1 = tank.height;
17         this.dir = tank.dir;
18         switch (dir) {    //根据tank的方向计算子弹坐标
19         case UP:
20             this.imagePath = "res/img/bullet_u.gif";
21             getSize();
22             this.x = x1 + (w1 - this.width) / 2;
23             this.y = y1 - this.height;
24             break;
25 
26         case DOWN:
27             this.imagePath = "res/img/bullet_d.gif";
28             getSize();
29             this.x = x1 + (w1 - this.width) / 2;
30             this.y = y1 + w1;
31             break;
32         case LEFT:
33             this.imagePath = "res/img/bullet_l.gif";
34             getSize();
35             this.x = x1 - this.width;
36             this.y = y1 + (h1 - this.height) / 2;
37             break;
38         case RIGHT:
39             this.imagePath = "res/img/bullet_r.gif";
40             getSize();
41             this.x = x1 + w1;
42             this.y = y1 + (h1 - this.height) / 2;
43             break;
44         }
45     }
46 
47     public void draw() {    //重写draw方法
48         try {
49             DrawUtils.draw(imagePath, x, y);
50         } catch (IOException e) {
51             // TODO 自动生成的 catch 块
52             e.printStackTrace();
53         }
54         switch (dir) {
55         case UP:
56             y -= speed;
57             break;
58 
59         case DOWN:
60             y += speed;
61             break;
62         case LEFT:
63             x -= speed;
64             break;
65         case RIGHT:
66             x += speed;
67             break;
68         }
69     }
70 
71     public boolean isDestroy() {    //子弹销毁方法
72         if(x<0 || x>Constants.WIDTH || y<0 || y>Constants.HEIGHT){
73             return true;
74         }
75         return false;
76     }
77 }
Bullet

 

  

随笔说:

      如果不进行删除无效的对象,运行时间越长,则游戏数据会越来越大。

    所以还是有必要优化一下的。

 

posted @ 2017-09-12 00:09  B_Lasting_尊  阅读(324)  评论(0编辑  收藏  举报