一、多个坦克
一个坦克打起来不过瘾,因此要一群坦克,ArrayList走起,控制一下其数目,就相当于一个无尽模式了
ArrayList<EnemyTank> etanks = new ArrayList<>();
@Override
public void run() {
while (true) {
//控制生成六只坦克
if(etanks.size()<6){
etanks.add(new EnemyTank());
}
repaint();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
run中添加控制etank生成
然后画出所有的etank及其子弹
// 绘制敌方坦克及子弹
for (int i = 0; i < etanks.size(); i++) {
if (etanks.get(i).isIs_Dead()) {
etanks.remove(i);
} else {
etanks.get(i).draw();
g.drawImage(etanks.get(i).getTankImage(), etanks.get(i).getLocal_x(), etanks.get(i).getLocal_y(),
etanks.get(i).getWidth(), etanks.get(i).getHeight(), this);
etanks.get(i).move();
// 绘制子弹
for (int j = 0; j < etanks.get(i).getBullets().size(); j++) {
Bullet b = etanks.get(i).getBullets().get(j);
if (b.isIs_Dead()) {
etanks.get(i).getBullets().remove(j);
} else {
b.draw();
g.drawImage(b.getBulletIamge(), b.getLocal_x(), b.getLocal_y(), Bullet.width, Bullet.height,
this);
b.move();
}
}
}
}
二、子弹击中坦克处理
坦克大战当然要打中坦克,如何判断呢?可以用坦克和一个个子弹匹配,相撞时击中,也可以反过来让子弹和坦克匹配,这里采用后面一种减少代码长度
先写匹配的函数,这里采用Rectangle
public Rectangle getRec(){
return new Rectangle(local_x, local_y, width, height);//根据位置,大小返回一个矩形
}
需要获取位置的都要添加这个函数
然后在Bullet类中添加击中坦克代码
public boolean is_CollisonTank(Tank tank){
if(tank.getRec().intersects(getRec())&&(tank.isIs_enemy()!=isIs_enemy())){
tank.setIs_Dead(true);
setIs_Dead(true);
return true;
}
return false;
}
击中则坦克和子弹都死亡(我方暂时未处理死亡)
因为这个方法是两个类交互,因此放到两者均被实例化的TankPanel中判断
// 绘制我方子弹
for (int i = 0; i < mtank.getBullets().size(); i++) {
Bullet b = mtank.getBullets().get(i);
for(int j=0;j<etanks.size();j++){
if(b.is_CollisonTank(etanks.get(j))){
System.out.println("敌机被击中");
}
}
if (b.isIs_Dead() == true) {
mtank.getBullets().remove(i);// 死亡则移除
} else {
b.draw();
g.drawImage(b.getBulletIamge(), b.getLocal_x(), b.getLocal_y(), Bullet.width, Bullet.height, this);
b.move();
}
}
// 绘制敌方坦克及子弹
for (int i = 0; i < etanks.size(); i++) {
if (etanks.get(i).isIs_Dead()) {
etanks.remove(i);
} else {
etanks.get(i).draw();
g.drawImage(etanks.get(i).getTankImage(), etanks.get(i).getLocal_x(), etanks.get(i).getLocal_y(),
etanks.get(i).getWidth(), etanks.get(i).getHeight(), this);
etanks.get(i).move();
// 绘制子弹
for (int j = 0; j < etanks.get(i).getBullets().size(); j++) {
Bullet b = etanks.get(i).getBullets().get(j);
if(b.is_CollisonTank(mtank)){
System.out.println("我方被击中");
}
if (b.isIs_Dead()) {
etanks.get(i).getBullets().remove(j);
} else {
b.draw();
g.drawImage(b.getBulletIamge(), b.getLocal_x(), b.getLocal_y(), Bullet.width, Bullet.height,
this);
b.move();
}
}
}
}
源码如下:
因为代码改动主要在Bullet和TankPanel中,因此只贴这两个,其它仅有Tank中添加了getRec()
Bullet:
public class Bullet {
// 位置
int local_x;
int local_y;
// 大小
public static int width = 4;
public static int height = 4;
// 方向
Direction dir;
// 速度
int speed = 8;
// 存活
boolean is_Dead;
// 敌我
boolean is_enemy;
// 图片
Image BulletIamge;
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public int getLocal_x() {
return local_x;
}
public void setLocal_x(int local_x) {
this.local_x = local_x;
}
public int getLocal_y() {
return local_y;
}
public void setLocal_y(int local_y) {
this.local_y = local_y;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public Direction getDir() {
return dir;
}
public void setDir(Direction dir) {
this.dir = dir;
}
public boolean isIs_Dead() {
return is_Dead;
}
public void setIs_Dead(boolean is_Dead) {
this.is_Dead = is_Dead;
}
public boolean isIs_enemy() {
return is_enemy;
}
public void setIs_enemy(boolean is_enemy) {
this.is_enemy = is_enemy;
}
public Image getBulletIamge() {
return BulletIamge;
}
public void setBulletIamge(Image bulletIamge) {
BulletIamge = bulletIamge;
}
public Bullet(int local_x, int local_y, Direction dir, boolean is_enemy) {
super();
this.local_x = local_x;
this.local_y = local_y;
this.dir = dir;
this.is_enemy = is_enemy;
}
public void draw() {// 获取子弹图片
if (!isIs_enemy()) {
setBulletIamge(Toolkit.getDefaultToolkit().getImage("tankmissile.gif"));
} else {
setBulletIamge(Toolkit.getDefaultToolkit().getImage("enemymissile.gif"));
}
}
public void move() {
if (this.local_x < 0 || this.local_x > TankFrame.Frame_Width || this.local_y < 0
|| this.local_y > TankFrame.Frame_Height) {
setIs_Dead(true);// 触边界死亡
} else {
switch (this.dir) {
case U:
setLocal_y(local_y - speed);
break;
case D:
setLocal_y(local_y + speed);
break;
case L:
setLocal_x(local_x - speed);
break;
case R:
setLocal_x(local_x + speed);
break;
default:
setLocal_y(local_y - speed);
}
}
}
public Rectangle getRec(){
return new Rectangle(local_x, local_y, width, height);
}
public boolean is_CollisonTank(Tank tank){
if(tank.getRec().intersects(getRec())&&(tank.isIs_enemy()!=isIs_enemy())){
tank.setIs_Dead(true);
setIs_Dead(true);
return true;
}
return false;
}
}
TankPanel:
public class TankPanel extends JPanel implements Runnable {
/**
*
*/
private static final long serialVersionUID = 1L;
MyTank mtank = new MyTank(100, 100, false);// 我方坦克
ArrayList<EnemyTank> etanks = new ArrayList<>();// 敌方坦克
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.fillRect(0, 0, TankFrame.Frame_Width, TankFrame.Frame_Height);
// 绘制我方坦克
mtank.draw();
g.drawImage(mtank.getTankImage(), mtank.getLocal_x(), mtank.getLocal_y(), mtank.getWidth(), mtank.getHeight(),
this);
mtank.move();
// 绘制我方子弹
for (int i = 0; i < mtank.getBullets().size(); i++) {
Bullet b = mtank.getBullets().get(i);
for(int j=0;j<etanks.size();j++){
if(b.is_CollisonTank(etanks.get(j))){
System.out.println("敌机被击中");
}
}
if (b.isIs_Dead() == true) {
mtank.getBullets().remove(i);// 死亡则移除
} else {
b.draw();
g.drawImage(b.getBulletIamge(), b.getLocal_x(), b.getLocal_y(), Bullet.width, Bullet.height, this);
b.move();
}
}
// 绘制敌方坦克及子弹
for (int i = 0; i < etanks.size(); i++) {
if (etanks.get(i).isIs_Dead()) {
etanks.remove(i);
} else {
etanks.get(i).draw();
g.drawImage(etanks.get(i).getTankImage(), etanks.get(i).getLocal_x(), etanks.get(i).getLocal_y(),
etanks.get(i).getWidth(), etanks.get(i).getHeight(), this);
etanks.get(i).move();
// 绘制子弹
for (int j = 0; j < etanks.get(i).getBullets().size(); j++) {
Bullet b = etanks.get(i).getBullets().get(j);
if(b.is_CollisonTank(mtank)){
System.out.println("我方被击中");
}
if (b.isIs_Dead()) {
etanks.get(i).getBullets().remove(j);
} else {
b.draw();
g.drawImage(b.getBulletIamge(), b.getLocal_x(), b.getLocal_y(), Bullet.width, Bullet.height,
this);
b.move();
}
}
}
}
}
@Override
public void run() {
while (true) {
// 控制生成六只坦克
if (etanks.size() < 6) {
etanks.add(new EnemyTank());
}
repaint();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}