|
|
@ -17,7 +17,7 @@ import java.util.Objects;
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public class TankFrame extends Frame {
|
|
|
|
public class TankFrame extends Frame {
|
|
|
|
|
|
|
|
|
|
|
|
Tank tank = new Tank(100, 500, DirEnum.RIGHT, this);
|
|
|
|
Tank myTank = new Tank(100, 500, DirEnum.RIGHT, GroupEnum.GOOD, this);
|
|
|
|
// Bullet bullet = new Bullet(200, 200, DirEnum.DOWN);
|
|
|
|
// Bullet bullet = new Bullet(200, 200, DirEnum.DOWN);
|
|
|
|
List<Bullet> bullets = new ArrayList<>();
|
|
|
|
List<Bullet> bullets = new ArrayList<>();
|
|
|
|
List<Tank> tanks = new ArrayList<>(); //敌方坦克
|
|
|
|
List<Tank> tanks = new ArrayList<>(); //敌方坦克
|
|
|
@ -52,7 +52,7 @@ public class TankFrame extends Frame {
|
|
|
|
g.drawString("子弹的数量:" + bullets.size(), 10, 60);
|
|
|
|
g.drawString("子弹的数量:" + bullets.size(), 10, 60);
|
|
|
|
g.drawString("敌方坦克数量:" + tanks.size(), 10, 90);
|
|
|
|
g.drawString("敌方坦克数量:" + tanks.size(), 10, 90);
|
|
|
|
g.setColor(color);
|
|
|
|
g.setColor(color);
|
|
|
|
tank.paint(g);
|
|
|
|
myTank.paint(g);
|
|
|
|
|
|
|
|
|
|
|
|
//画出敌方坦克
|
|
|
|
//画出敌方坦克
|
|
|
|
for (int i=0; i<tanks.size(); i++) {
|
|
|
|
for (int i=0; i<tanks.size(); i++) {
|
|
|
@ -117,7 +117,7 @@ public class TankFrame extends Frame {
|
|
|
|
case KeyEvent.VK_RIGHT : bR = false; break;
|
|
|
|
case KeyEvent.VK_RIGHT : bR = false; break;
|
|
|
|
case KeyEvent.VK_UP : bU = false; break;
|
|
|
|
case KeyEvent.VK_UP : bU = false; break;
|
|
|
|
case KeyEvent.VK_DOWN : bD = false; break;
|
|
|
|
case KeyEvent.VK_DOWN : bD = false; break;
|
|
|
|
case KeyEvent.VK_SPACE: tank.fire(); break;
|
|
|
|
case KeyEvent.VK_SPACE: myTank.fire(); break;
|
|
|
|
default: break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
setDir();
|
|
|
|
setDir();
|
|
|
@ -126,16 +126,16 @@ public class TankFrame extends Frame {
|
|
|
|
private void setDir() {
|
|
|
|
private void setDir() {
|
|
|
|
//当没有任何方向键按下的时候,那么让坦克停止
|
|
|
|
//当没有任何方向键按下的时候,那么让坦克停止
|
|
|
|
if( !bL && !bR && !bU && !bD ) {
|
|
|
|
if( !bL && !bR && !bU && !bD ) {
|
|
|
|
tank.setMove(false);
|
|
|
|
myTank.setMove(false);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
tank.setMove(true);
|
|
|
|
myTank.setMove(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//为什么不用if-else 因为有可能是一起按两个方向键,那最后拿到哪个算哪个
|
|
|
|
//为什么不用if-else 因为有可能是一起按两个方向键,那最后拿到哪个算哪个
|
|
|
|
if(bL) tank.setDir(DirEnum.LEFT);
|
|
|
|
if(bL) myTank.setDir(DirEnum.LEFT);
|
|
|
|
if(bR) tank.setDir(DirEnum.RIGHT);
|
|
|
|
if(bR) myTank.setDir(DirEnum.RIGHT);
|
|
|
|
if(bU) tank.setDir(DirEnum.UP);
|
|
|
|
if(bU) myTank.setDir(DirEnum.UP);
|
|
|
|
if(bD) tank.setDir(DirEnum.DOWN);
|
|
|
|
if(bD) myTank.setDir(DirEnum.DOWN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|