Пример #1
0
  /** Constructor */
  public GameManager(MainGameFrame mgf1) {
    // to be able to reach main menu from the game we need to pass it to the actual game manager to
    // know its caller.
    mgf = mgf1;

    setSize(GAME_WIDTH, GAME_HEIGHT);
    addKeyListener(new TAdapter());
    addKeyListener(new PauseAdapter(this));
    setFocusable(true);
    setBackground(Color.CYAN);
    setDoubleBuffered(true);

    // ingame = true;
    pauseFlag = false;
    nextLevelFlag = false;
    directionKeysFlag = mgf.getControls();
    difficultyFlag = mgf.getDifficulty();
    currentLevel = 1;

    // Init game objects
    wind = WindManager.getInstance(); // Initialize singleton WindManager class
    boat = new Boat(0, 20);

    initGame();
    // gamePoint = 0;

    // Init Timer
    timer = new Timer(40, this);
    startTimer();
  }
Пример #2
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();
  }
Пример #3
0
  /** Level initialization handled here */
  public void initGame() {
    // First level initializations-burada can da sıfırlanmalı, oyunu restart yapan method bu olmalı
    if (currentLevel == 1) {
      boat.setFlashForward(false); // init bonus state
      boat.replenishUsedNumOfBullets(); // init bonus state
      // boat.setNumOfLives(INITIAL_BOAT_LIFE_AMOUNT);//init bonus state
      boat.setBoatImage(mgf.getBoatColor()); // To set the proper image from the main game frame

      // setDifficultInBoat(mgf.getDifficulty());
      // System.out.println("difffi: " + mgf.getDifficulty());
      if (mgf.getDifficulty()) {
        boat.setNumOfLives(INITIAL_BOAT_LIFE_AMOUNT - 2);
      } else {
        boat.setNumOfLives(INITIAL_BOAT_LIFE_AMOUNT);
      }

      boat.setDirectionKeysOn(mgf.getControls());

      boat.setPosition(0, 20);
      boat.setDirection(0);
      boat.setVisible(true);
      // Game object positions
      buoyPositions =
          new int[][] {
            {250, 500}, {300, 100}, {500, 300}, {450, 125}, {400, 200}, {350, 400}, {200, 250}
          };
      islandPositions =
          new int[][] {
            {50, 175}, // small
            {550, 300}, // medium
            {50, 450} // medium
          };
      islandTypes = new String[] {"small", "medium", "medium"};
      bonusPositions = new int[][] {{60, 350}, {500, 550}};
      gamePoint = 0;
      initBuoys();
      initIslands();
      initBonuses();
      System.out.println("inside init game level-1");
    }
    // other level initializations
    if (currentLevel == 2) {
      initGameLevel2();
    }
    if (currentLevel == 3) {
      initGameLevel3();
    }
  }
Пример #4
0
  private void initGameLevel3() {
    boat.setFlashForward(false); // init bonus state
    boat.replenishUsedNumOfBullets(); // init bonus state
    boat.setNumOfLives(INITIAL_BOAT_LIFE_AMOUNT); // init bonus state
    boat.setBoatImage(mgf.getBoatColor());
    boat.setPosition(0, 20);
    boat.setDirection(0);
    boat.setVisible(true);
    // Game object positions
    buoyPositions =
        new int[][] {
          {400, 500}, {350, 120}, {630, 400}, {500, 145}, {50, 550},
          {650, 200}, {250, 400}, {200, 250}, {600, 630}, {60, 375}
        };
    islandPositions =
        new int[][] {
          {50, 100}, // medium
          {550, 500}, // small
          {100, 500}, // medium
          {300, 200} // large
        };
    islandTypes = new String[] {"medium", "small", "medium", "large"};

    bonusPositions = new int[][] {{250, 100}, {80, 310}, {450, 550}, {600, 330}};
    initBuoys();
    initIslands();
    initBonuses();
    System.out.println("inside init game level-3");
  }
Пример #5
0
  private void initGameLevel2() {
    boat.setFlashForward(false); // init bonus state
    boat.replenishUsedNumOfBullets(); // init bonus state
    boat.setNumOfLives(INITIAL_BOAT_LIFE_AMOUNT); // init bonus state
    boat.setBoatImage(mgf.getBoatColor());
    boat.setPosition(0, 20);
    boat.setDirection(0);
    boat.setVisible(true);
    // Game object positions
    buoyPositions =
        new int[][] {{100, 100}, {200, 300}, {100, 150}, {200, 600}, {400, 400}, {500, 650}};
    islandPositions =
        new int[][] {
          {40, 300}, // Small islands
          {500, 200}, // Medium islands
          {400, 400} // Large islands
        };
    islandTypes = new String[] {"small", "medium", "large"};
    bonusPositions = new int[][] {{100, 500}, {500, 100}, {400, 200}};

    initBuoys();
    initIslands();
    initBonuses();
    System.out.println("inside init game level-2");
  }
Пример #6
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(); // ?
  }