Beispiel #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();
  }
Beispiel #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(); // ?
  }