Exemple #1
3
  // 专门判断子弹是否击中坦克的函数
  public boolean hitTank(Bullet b, Tank et) {
    boolean b1 = false;

    // 判断该坦克的方向
    switch (et.direct) {
        // 向上和向下范围一样
      case 0:
      case 2:
        if (b.x >= et.x && b.x <= et.x + 20 && b.y >= et.y && b.y <= et.y + 30) {
          // 击中1.子弹死亡 2.目标死亡
          b.alive = false;
          et.alive = false;
          b1 = true;
          // 创建一颗炸弹,放入Vector中
          Bomb bomb = new Bomb(et.x, et.y);
          bombs.add(bomb);
        }
        break;
      case 1:
      case 3:
        if (b.x >= et.x && b.x <= et.x + 30 && b.y >= et.y && b.y <= et.y + 20) {
          // 击中
          b.alive = false;
          et.alive = false;
          b1 = true;
          // 创建一颗炸弹,放入Vector中
          Bomb bomb = new Bomb(et.x, et.y);
          bombs.add(bomb);
        }
        break;
        //                default:System.out.println("xxxxxxxxx");
    }

    return b1;
  }
Exemple #2
0
 // public void setColor(Color color){	this.color=color;}
 public Tank copy() {
   Tank tank = new Tank(client, world);
   tank.angle.set(angle);
   tank.speed = speed;
   tank.location.set(getLocation());
   tank.speedToggle = speedToggle;
   tank.rotateToggle = rotateToggle;
   return tank;
 }
Exemple #3
0
  public boolean hitTanks(List<Tank> ts) {

    Tank t = null;
    for (int i = 0; i < ts.size(); i++) {
      t = ts.get(i);
      if (t.isLive() && t != null) {
        if (hitTank(t)) {
          return true;
        }
      }
    }
    return false;
  }
 private void illegaleMove(Direction direction) {
   System.out.println(
       "[illegal move] "
           + "derection:"
           + direction
           + "||"
           + "tankX:"
           + tank.getX()
           + "||"
           + "tankY:"
           + tank.getY());
   return;
 }
 public int fireLeft() throws Exception {
   String str = getQuadrantXY(tank.getX(), tank.getY());
   int tankV = Integer.valueOf(str.substring(0, 1));
   int tankH = Integer.valueOf(str.substring(2, str.length()));
   tank.turn(Direction.LEFT);
   for (int idx = tankV; idx < tankV + 1; idx++) {
     for (int j = tankH; j >= 0; j--) {
       if (battleField.scanQuadrant(idx, j) == "B") {
         tank.fire();
       }
     }
   }
   return tankH;
 }
 public int fireDown() throws Exception {
   String str = getQuadrantXY(tank.getX(), tank.getY());
   int tankV = Integer.valueOf(str.substring(0, 1));
   int tankH = Integer.valueOf(str.substring(2, str.length()));
   tank.turn(Direction.DOWN);
   for (int idx = tankV; idx < battleField.getBattleField().length; idx++) {
     for (int j = tankH; j < tankH + 1; j++) {
       if (battleField.scanQuadrant(idx, j) == "B") {
         tank.fire();
       }
     }
   }
   return tankV;
 }
 private void moveDown(Direction direction) {
   if (direction == Direction.DOWN) {
     tank.updateY(step);
     System.out.println(
         "[move down] "
             + "derection:"
             + direction
             + "||"
             + "tankX:"
             + tank.getX()
             + "||"
             + "tankY:"
             + tank.getY());
   }
 }
 private void moveUp(Direction direction) {
   if (direction == Direction.UP) {
     tank.updateY(-step);
     System.out.println(
         "[move up] "
             + "derection:"
             + direction
             + "||"
             + "tankX:"
             + tank.getX()
             + "||"
             + "tankY:"
             + tank.getY());
   }
 }
 private void moveRight(Direction direction) {
   if (direction == Direction.RIGHT) {
     tank.updateX(step);
     System.out.println(
         "[move right] "
             + "derection:"
             + direction
             + "||"
             + "tankX:"
             + tank.getX()
             + "||"
             + "tankY:"
             + tank.getY());
   }
 }
 private void moveLeft(Direction direction) {
   if (direction == Direction.LEFT) {
     tank.updateX(-step);
     System.out.println(
         "[move left] "
             + "derection:"
             + direction
             + "||"
             + "tankX:"
             + tank.getX()
             + "||"
             + "tankY:"
             + tank.getY());
   }
 }
Exemple #11
0
  public void paint(Graphics g) {

    myTank.draw(g);
    enenmyTank.draw(g);

    for (int i = 0; i < arrMissile.size(); i++) {
      Missile m = arrMissile.get(i);
      if (m.hitTank(enenmyTank)) {
        enenmyTank.live = false;
        m.live = false;
        arrMissile.remove(m);
      }

      m.draw(g);
    }

    g.drawString("arrMissile count:" + arrMissile.size(), 10, 50);
  }
Exemple #12
0
 public boolean hitTank(Tank t) {
   boolean bHitted = false;
   if (this.getRec().intersects(t.getRec()) && this.bGood != t.isGood()) {
     Explode e = new Explode(x, y);
     tc.explodes.add(e);
     if (t.isGood()) {
       int i = t.getLives();
       i--;
       t.setLives(i);
       if (i <= 0) {
         t.setLive(false);
       }
     } else {
       t.setLive(false);
     }
     bHitted = true;
     this.bLive = false;
   }
   return bHitted;
 }
 private void repaintMove() throws InterruptedException {
   repaint();
   Thread.sleep(tank.getSpeed());
 }
 public void processMove(Tank tank) throws Exception {
   this.tank = tank;
   if (tank.getDirection() == Direction.LEFT && tank.getX() <= minimumFieldSize
       || tank.getDirection() == Direction.RIGHT && tank.getX() >= maximumFieldSize
       || tank.getDirection() == Direction.UP && tank.getY() <= minimumFieldSize
       || tank.getDirection() == Direction.DOWN && tank.getY() >= maximumFieldSize) {
     illegaleMove(tank.getDirection());
   } else {
     tank.turn(tank.getDirection());
     for (int i = 0; i < cellSize; i++) {
       moveLeft(tank.getDirection());
       moveRight(tank.getDirection());
       moveUp(tank.getDirection());
       moveDown(tank.getDirection());
       repaintMove();
     }
   }
 }
  @Override
  protected void paintComponent(Graphics g) {
    super.paintComponent(g);

    int i = 0;
    Color cc;
    for (int v = 0; v < 9; v++) {
      for (int h = 0; h < 9; h++) {
        if (COLORDED_MODE) {
          if (i % 2 == 0) {
            cc = new Color(252, 241, 177);
          } else {
            cc = new Color(233, 243, 255);
          }
        } else {
          cc = new Color(180, 180, 180);
        }
        i++;
        g.setColor(cc);
        g.fillRect(h * 64, v * 64, 64, 64);
      }
    }

    for (int j = 0; j < battleField.getDimentionY(); j++) {
      for (int k = 0; k < battleField.getDimentionX(); k++) {
        if (battleField.scanQuadrant(j, k).equals("B")) {
          String coordinates = getQuadrant(j + 1, k + 1);
          int separator = coordinates.indexOf("_");
          int y = Integer.parseInt(coordinates.substring(0, separator));
          int x = Integer.parseInt(coordinates.substring(separator + 1));
          g.setColor(new Color(0, 0, 255));
          g.fillRect(x, y, 64, 64);
        }
      }
    }

    g.setColor(new Color(255, 0, 0));
    g.fillRect(tank.getX(), tank.getY(), 64, 64);

    g.setColor(new Color(0, 255, 0));
    if (tank.getDirection() == Direction.UP) {
      g.fillRect(tank.getX() + 20, tank.getY(), 24, 34);
    } else if (tank.getDirection() == Direction.DOWN) {
      g.fillRect(tank.getX() + 20, tank.getY() + 30, 24, 34);
    } else if (tank.getDirection() == Direction.LEFT) {
      g.fillRect(tank.getX(), tank.getY() + 20, 34, 24);
    } else {
      g.fillRect(tank.getX() + 30, tank.getY() + 20, 34, 24);
    }

    g.setColor(new Color(255, 0, 255));
    g.fillRect(agressor.getX(), agressor.getY(), 64, 64);

    g.setColor(new Color(255, 255, 0));
    if (agressor.getDirection() == Direction.UP) {
      g.fillRect(agressor.getX() + 20, agressor.getY(), 24, 34);
    } else if (agressor.getDirection() == Direction.DOWN) {
      g.fillRect(agressor.getX() + 20, agressor.getY() + 30, 24, 34);
    } else if (agressor.getDirection() == Direction.LEFT) {
      g.fillRect(agressor.getX(), agressor.getY() + 20, 34, 24);
    } else {
      g.fillRect(agressor.getX() + 30, agressor.getY() + 20, 34, 24);
    }

    g.setColor(new Color(255, 255, 0));
    g.fillRect(bullet.getX(), bullet.getY(), 14, 14);
  }
Exemple #16
0
 public double getDirection(Tank that) {
   Coordinate other = that.getLocation();
   return Math.atan2(other.y() - location.y(), other.x() - location.x());
 }
Exemple #17
0
 public double getDistance(Tank that) {
   Coordinate other = that.getLocation();
   return Math.sqrt(Math.pow(other.x() - location.x(), 2) + Math.pow(other.x() - location.y(), 2));
 }