实现坦克连发

mac2022-06-30  91

  如题所述,此前的坦克只能发射一颗子弹,现在要让他可以同时连续发射

这里用到了 vector集合类存储多颗子弹。

//绘制炮弹 if(this.hero.s != null && hero.s.isLive ==true) { g.draw3DRect(hero.s.x, hero.s.y, 2, 2, false); }

绘制炮弹的方法改为:

//绘制炮弹 for(int i=0;i<this.hero.ss.size();i++) { Shot Myshot = hero.ss.get(i); if(Myshot != null && Myshot.isLive ==true) { g.draw3DRect(Myshot.x, Myshot.y, 2, 2, false); } if(Myshot.isLive == false) { this.hero.ss.remove(Myshot); } }

 

  声明一个存储子弹的集合来存放子弹

Vector <Shot> ss=new Vector<Shot>(); //存储子弹 Shot s=null; public Hero(int x,int y) { super(x,y); } //开火 上下左右移动,这是我的坦克的所有行为 /** * 关于开火,也取决于我的坦克的朝向 * */ public void shotEnmpy() { switch(this.direct) { case 0: s=new Shot(x,y-15,0); ss.add(s); break; case 1: s=new Shot(x+20,y,1); ss.add(s); break; case 2: s=new Shot(x,y+20,2); ss.add(s); break; case 3: s=new Shot(x-20,y,3); ss.add(s); break; }

最多存储五发子弹

if(e.getKeyCode() == KeyEvent.VK_J) { //开火 if(this.hero.ss.size() < 5) { //控制我方坦克发射子弹的数量 this.hero.shotEnmpy(); }

 

转载于:https://www.cnblogs.com/ybzmy/p/9714935.html

最新回复(0)