Esempio n. 1
0
  @Override
  public void actionPerformed(ActionEvent arg0) {
    ArrayList<Bullet> bs = boat.getBulletsList();

    for (int i = 0; i < bs.size(); i++) {
      Bullet b = (Bullet) bs.get(i);
      if (b.isVisible()) b.move();
      else bs.remove(i);
    }

    for (int i = 0; i < buoys.size(); i++) {
      Buoy b = (Buoy) buoys.get(i);
      if (!(b.isVisible())) {
        gamePoint += b.getPoint();
        buoys.remove(i);
        // System.out.println("Remaining Buoys: " + buoys.size());
      }
    }

    for (int i = 0; i < bonuses.size(); i++) {
      Bonus b = (Bonus) bonuses.get(i);
      if (!(b.isVisible())) {
        gamePoint += b.getPoint();
        bonuses.remove(i);
      }
    }
    // level olayı burada hallediliyor!!
    // en son level kalmadığında highscore' a bak yazdır
    if (nextLevelFlag) {
      System.out.println("next Level a geçtin");
      currentLevel++;
      System.out.println("level: " + currentLevel);
      initGame(); // oyunu restart edecek
      nextLevelFlag = false;
      if (currentLevel
          > LAST_LEVEL) { // tüm levellar bittikten sonra terminate/ana menüye dönüş, tam bu anda
                          // high score yazdır. Ancak oyunu bitirenler highscore table a girmeye hak
                          // kazanır.
        // High scores to table
        try {
          writeHighScoreToFile(computeTotalScore());
        } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
        currentLevel = 1;
        gamePoint = 0;
        initGame();
        CardLayout c2 = (CardLayout) (mgf.getCanvas().getLayout());
        c2.show(mgf.getCanvas(), "GUI");
        // System.exit(0);
      }
    } else {

    }

    boat.move();
    checkCollisions();
    repaint();
  }
Esempio n. 2
0
  //	private void initLevel(int level){//initializes levels according to the level parameter
  //
  //	}
  public void paint(Graphics g) {
    super.paint(g);

    Graphics2D g2d = (Graphics2D) g;

    // Windmill drawing
    g2d.rotate(wind.getDirection() * Math.PI / 180, 636, 64);
    g2d.drawImage(wind.getImage(), 572, 0, this);
    g2d.rotate(-wind.getDirection() * Math.PI / 180, 636, 64);

    // Boat drawing
    if (boat.isVisible()) {
      g2d.rotate(
          TIGHT_TURN_FACTOR * boat.getDirection() * Math.PI / 180,
          boat.getX() + 16,
          boat.getY() + 16);
      // g2d.drawImage(boat.getImage(), boat.getX(), boat.getY(), this);
      g2d.drawImage(boat.getImage(), boat.getX(), boat.getY(), this);
      // TEST-sınırları görmek için
      //			Rectangle r1 = boat.getBounds();
      //			g2d.drawRect(r1.x, r1.y, r1.width, r1.height);
      g2d.rotate(
          -TIGHT_TURN_FACTOR * boat.getDirection() * Math.PI / 180,
          boat.getX() + 16,
          boat.getY() + 16);
      if ((buoys.size() == 0)
          && (currentLevel
              <= LAST_LEVEL)) { // level atlama ve başarılı bir şekilde oyunu bitirme burada olacak
        System.out.println("All buoys finished, well done!");
        int tp = computeTotalScore();
        System.out.println("Total Score: " + tp);
        nextLevelFlag = true; // Actionlistenera tek seferlik girebilmek için bir flag
        // System.exit(0);
      }
    } else { // If boat is dead then check for remaining lives
      if (boat.getNumOfLives() > 0) {
        boat.decrementNumOfLives();
        boat.setPosition(0, 20);
        boat.setDirection(0);
        boat.setVisible(true);
      } else { // No remaining lives case
        // g2d.drawString("Game Over!", 80, 15);
        System.out.println("Game Over");
        int tp = computeTotalScore();
        System.out.println("Total Score: " + tp);
        currentLevel = 1;
        CardLayout c2 = (CardLayout) (mgf.getCanvas().getLayout());
        c2.show(mgf.getCanvas(), "GUI");
        // System.exit(0);
      }
    }

    // Island drawing
    for (int i = 0; i < islands.size(); i++) {
      Island is = (Island) islands.get(i);
      g2d.drawImage(is.getImage(), is.getX(), is.getY(), this);
    }

    // Bullet drawing
    ArrayList<Bullet> bs = boat.getBulletsList();
    for (int i = 0; i < bs.size(); i++) {
      Bullet b = (Bullet) bs.get(i);
      g2d.drawImage(b.getImage(), b.getX(), b.getY(), this);
    }

    // Buoy drawing
    for (int i = 0; i < buoys.size(); i++) {
      Buoy b = (Buoy) buoys.get(i);
      if (b.isVisible()) {
        g2d.drawImage(b.getImage(), b.getX(), b.getY(), this);
      }
    }

    // Bonus drawing
    for (int i = 0; i < bonuses.size(); i++) {
      Bonus b = (Bonus) bonuses.get(i);
      if (b.isVisible()) {
        g2d.drawImage(b.getImage(), b.getX(), b.getY(), this);
      } else {
        if (b.getBonusType().equalsIgnoreCase("lifebonus")) {
          boat.incrementNumOfLives();
        }
        if (b.getBonusType().equalsIgnoreCase("speedbonus")) {
          boat.setFlashForward(true);
        }
        if (b.getBonusType().equalsIgnoreCase("bulletbonus")) {
          boat.addBonusBullets();
        }
      }
    }

    // Information
    g2d.drawString("Buoys left: " + buoys.size(), 5, 15);

    if (boat.getNumOfLives() == 0) g2d.drawString("Last Chance!", 80, 15);
    else g2d.drawString("Lives: " + boat.getNumOfLives(), 90, 15);
    // g2d.drawString("-" + (int)System.currentTimeMillis()/1000, 5, 25);

    g2d.drawString("Score: " + gamePoint, 160, 15);
    g2d.drawString("Bullets: " + boat.getRemainingNumOfBullets(), 235, 15);
    g2d.drawString("Level: " + currentLevel, 320, 15);

    // Generates new random wind
    wind.generateWindDirection();

    // Default system methods
    Toolkit.getDefaultToolkit().sync(); // ?
    g.dispose(); // ?
  }