子弹的图片和坦克的图片一样的做法,就不在赘述了。
子弹从炮筒打出 这个需要计算:首先获取坦克的宽度和高度,以及子弹的宽度和高度:
子弹的宽度和高度:
public static final int WIDTH = ResourceMgr.bulletD.getWidth();
public static final int HEIGHT = ResourceMgr.bulletD.getHeight();
坦克的宽度和高度:
public static final int WIDTH = ResourceMgr.tankD.getWidth();
public static final int HEIGHT = ResourceMgr.tankD.getHeight();
在创建子弹的时候需要计算,用位置加上坦克的宽度的一半,代码如下:
public void fire() {
int bx = this.x + Tank.WIDTH / 2 - Bullet.WIDTH / 2;
int by = this.y + Tank.HEIGHT / 2 - Bullet.HEIGHT / 2;
tankFrame.bulletList.add(new Bullet(bx,by, this.dir, tankFrame));
}
搞定