Пример #1
0
  //	重写paint方法
  public void paint(Graphics g) {
    super.paint(g);
    //		float lineWidth = 3.0f;
    //	    ((Graphics2D)g).setStroke(new BasicStroke(lineWidth));
    //		将坦克的活动区域填充为默认黑色
    g.fillRect(0, 0, 800, 600);
    this.drawTank(hero.getX(), hero.getY(), g, hero.getDirection(), 1);

    for (int i = 0; i < hero.bombs.size(); i++) {
      Bomb myBomb = hero.bombs.get(i);
      // 画出一颗子弹
      if (myBomb != null && myBomb.isLive == true) {
        //			float lineWidth = 2.0f;
        //			((Graphics2D) g).setStroke(new BasicStroke(lineWidth));//设置线条为粗线
        g.draw3DRect(myBomb.x, myBomb.y, 2, 2, true);
      }
      if (myBomb.isLive == false) {
        hero.bombs.remove(myBomb);
      }
    }
    //		画出爆炸
    for (int i = 0; i < baozhas.size(); i++) {
      BaoZha bz = baozhas.get(i);
      System.out.println("baozhas.size()= " + baozhas.size());
      if (bz.life > 5) {
        g.drawImage(image3, bz.x, bz.y, 30, 30, this);
      } else if (bz.life > 3) {
        g.drawImage(image2, bz.x, bz.y, 30, 30, this);
      } else {
        g.drawImage(image1, bz.x, bz.y, 30, 30, this);
      }
      bz.liftDown();
      if (bz.life == 0) {
        baozhas.remove(bz);
      }
    }

    //		画出敌人的坦克
    for (int i = 0; i < ets.size(); i++) {
      EnemyTank et = ets.get(i);
      if (et.isLive) {

        this.drawTank(et.getX(), et.getY(), g, et.getDirection(), 0);
      }
    }
  }
Пример #2
0
  @Override
  public void timePassed() {
    // Moves if there is empty space underneath
    if (this.world.getTunnel().isPositionEmptySpaceUnder(this.getPosition())) {
      this.timeWaited++;
      // Delays the falling so it won't kill you if you empty the space
      // right under it
      if (this.timeWaited > 80) {
        this.emptySpacesUnder++;
        this.moveTo(new Point2D.Double(this.getPosition().getX(), this.getPosition().getY() + 3));
        Monster m = this.encounteredWithMonster();
        if (m != null) {
          m.die();
        }
        // Kills the hero if it encounters it while falling
        if (this.encounteredWithHero()) {
          this.getWorld().getHero().die();
        }
        Bag bag = this.bagEncounteredWithBag();
        // If a bag falls on top of another bag, they both break
        // Value of gold is doubled since it was two bags
        if (bag != null) {
          Gold gold = new Gold(this.getWorld(), this.getPosition());
          bag.die();
          this.die();
          gold.setValue(gold.getValue() * 2);
          this.world.addBlocks(gold);
          return;
        }
      }
    } else {
      this.timeWaited = 0;
      // If it dropped more than one block, it will break into gold
      if (this.emptySpacesUnder > 47) {
        Bag bag = this.bagEncounteredWithBag();
        Gold gold = new Gold(this.getWorld(), this.getPosition());

        // If a bag falls on top of another bag, they both break
        // Value of gold is doubled since it was two bags
        if (bag != null) {
          bag.die();
          gold.setValue(gold.getValue() * 2);
        }
        this.die();
        this.world.addBlocks(gold);
      }
      this.emptySpacesUnder = 0;
    }

    // Moves the bag if the hero encounters it
    int deltaX = 0;
    int deltaY = 0;
    if (this.encounteredWithHero()) {
      Hero hero = this.getWorld().getHero();
      char direction = hero.getDirection();
      if (direction == 'u') if (hero.getPosition().getX() > this.getPosition().getX()) deltaX -= 3;
      if (direction == 'd') if (hero.getPosition().getX() < this.getPosition().getX()) deltaX += 3;
      this.moveTo(
          new Point2D.Double(
              this.getPosition().getX() + deltaX, this.getPosition().getY() + deltaY));
    }

    // Adds an empty space if there isn't one already where the bag abject
    // is
    if (!this.getWorld().getTunnel().isInEmptySpace(this)) {
      this.getWorld().getFootprintsToAdd().add(new FootPrint(this.getWorld(), this.getPosition()));
    }
  }