Beispiel #1
0
 private void movePlayers() {
   onePlayerAlive = false;
   Iterator i = players.iterator();
   while (i.hasNext()) {
     Player p = (Player) i.next();
     if (p.getLives() == 0) {
       p.setActive(false);
     }
     if (p.isActive()) {
       onePlayerAlive = true;
       p.move();
     }
   }
 }
Beispiel #2
0
  private void drawLayout(Graphics g) {
    g.setColor(Color.blue);
    g.drawRect(10, 10, width - 20, height - 20);

    g.setColor(Color.red.brighter());
    Iterator f = players.iterator();
    int lifeDisplayPosition = 0;
    while (f.hasNext()) {
      Player h = (Player) f.next();
      if (h.isActive()) {
        lifeDisplayPosition++;
        String lifeInformation = "Player " + lifeDisplayPosition + ": ";
        for (int i = 0; i < h.getLives(); i++) {
          lifeInformation += "\u2606";
        }
        g.drawString(lifeInformation, 20, lifeDisplayPosition * 40);
      }
    }

    borders[2] = width - 20;
    borders[3] = height - 20;

    Iterator i = players.iterator();
    Player p;
    while (i.hasNext()) {
      p = (Player) i.next();
      if (p.isActive()
          && !countdownF
          && (p.getX() < borders[0]
              || p.getY() < borders[1]
              || p.getX() > borders[2]
              || p.getY() > borders[3])
          && (!p.getImmunity())) {
        p.decLives(width / 2, height / 2);
        p.setImmunity(true);
      }
      if (p.getLives() <= 0) {
        if (p instanceof MouseControlledPlayer) {
          MouseControlledPlayer m = (MouseControlledPlayer) p;
          removeMouseListener(m);
          removeMouseMotionListener(m);
        } else if (p instanceof KeyboardControlledPlayer) {
          KeyboardFocusManager.getCurrentKeyboardFocusManager()
              .removeKeyEventDispatcher((KeyboardControlledPlayer) p);
        }
        i.remove();
        for (int h = 0; h < 4; h++) {
          if (deathLocation[h] == -1) {
            deathLocation[h] = p.getX();
            deathLocation[h + 1] = p.getY();
            break;
          }
        }
      }
    }

    if ((deathLocation[0] != -1) && (deathLocation[1] != -1)) {
      g.drawLine(
          deathLocation[0] - 15,
          deathLocation[1] - 15,
          deathLocation[0] + 15,
          deathLocation[1] + 15);
      g.drawLine(
          deathLocation[0] + 15,
          deathLocation[1] - 15,
          deathLocation[0] - 15,
          deathLocation[1] + 15);
    }
    if ((deathLocation[2] != -1) && (deathLocation[3] != -1)) {
      g.drawLine(
          deathLocation[2] - 15,
          deathLocation[3] - 15,
          deathLocation[2] + 15,
          deathLocation[3] + 15);
      g.drawLine(
          deathLocation[2] + 15,
          deathLocation[3] - 15,
          deathLocation[2] - 15,
          deathLocation[3] + 15);
    }

    if (!onePlayerAlive) {
      Font old = g.getFont();
      g.setFont(new Font("monospaced", Font.BOLD, 20));
      g.setColor(Color.red.darker());
      g.drawString("GAME OVER", width / 2 - 40, height / 2);
      g.setFont(old);
      g.setColor(Color.WHITE);
      g.drawString("Score", width - 60, 25);
      g.drawString(String.valueOf(score), width - 60, 40);
    }
  }
  /**
   * This starts the main game loop. This is the guts of the game. When the loop starts it checks to
   * see if the player has any lives left, if not, it prints "Game Over" and resets the game. If
   * there are lives left, it decides what the ghosts next move will be, detects any collisions, and
   * acts accordingly, and also gets input from the player
   */
  public void start() {
    Globals.state = ENEMY_HUNTER_STATE;
    Globals.game.setPlayer(new Player());
    player = Globals.game.getPlayer();
    player.setCenter(board.getPlayerStartPoint());
    newGame();
    paused = false;
    waiting = true;
    Point regenDoorPoint = board.getRegenDoor();
    Point regenPoint = board.getRegenPoint();
    Point playerPoint = player.getCenter();
    while ((this.isVisible()) && (player.getLives() >= 0)) {
      if (paused) {
        paintPauseScreen();
      }
      while (paused || stopped || waiting) {
        Thread.yield();
      }

      if (Globals.blipsLeft <= 0) {
        Globals.speed *= SPEED_MOD;
        resetCanvas();
        board.reset();
        Globals.blipsLeft = board.getBlipCount();
      }
      // now we need to see if the current state is enemy hunted
      if (Globals.state == ENEMY_HUNTED_STATE) {
        // now we need to see if we should go out of it
        if (Globals.timeUntilLeaveingHuntedState <= 0) {
          Globals.state = ENEMY_HUNTER_STATE;
          Globals.timeUntilLeaveingHuntedState = TIME_IN_HUNTED_STATE;
        } else {
          // we shouldn't, so we decrement the counter
          Globals.timeUntilLeaveingHuntedState -= Globals.speed;
        }
      }
      // move the player
      player.move(board.getMoveOptions(player), playerChoice);
      if (Globals.aiMode == FSA_AI_MODE) {
        // now updated the enemy movements
        for (int i = 0; i < enemies.length; i++) {
          // these are put here to reduce program jumping and cache misses as well
          Enemy enemy = enemies[i];
          int moveOptions = board.getMoveOptions(enemy);
          // first, we need to take apporpriate actions if we're on a regen tile
          if (board.getCurrentTile(enemy).isRegen()) {
            // first check to see if the enemy is alive
            if (!enemy.isAlive()) {
              // it's not, so we make it alive
              enemy.setAlive(true);
            }
            // now we tell the enemy to move towards the the door
            enemy.move(moveOptions, regenDoorPoint, true);
          } else {
            // We're not in the regen pen, so now we check if the enemy is alive
            if (!enemy.isAlive()) {
              // it's not, so we want to go towards the regen point
              enemy.move(moveOptions, regenPoint, false);
            } else {
              // the enemy is alive, so we tell it towards/away from the player
              enemy.move(moveOptions, playerPoint, false);
            }
          }
        }
      } else if (Globals.aiMode == ANN_AI_MODE) {
        // Globals.game.getNet().process(ANNTools.getBoardCondition(board, enemies, player));
        player.setInputValues(board);
        for (Enemy enemy : enemies) {
          enemy.setInputValues(board);
        }
        Globals.game.getNet().generateActivations();
        for (int i = 0; i < enemies.length; i++) {
          // Last 2 values don't matter here
          enemies[i].move(
              board.getMoveOptions(enemies[i]),
              board.getRegenDoor(),
              board.getCurrentTile(enemies[i]).isRegen());
        }
      }

      // no we check for collisions and act appropriately
      checkCollision();
      // check to see if we should notify the game that we have stepped
      if (Globals.trainingMode != TRAINING_MODE_OFF) {
        // we should, so we do.
        Globals.game.step();
      }
      // And now we sleep. The sleep time is the current speed
      try {
        Thread.sleep(Globals.speed);
      } catch (InterruptedException e) {
        break;
      }
      // no update the screen
      update();
    }
    if (player.getLives() <= 0) {
      paintGameOver();
      try {
        Thread.sleep(2500);
      } catch (InterruptedException e) {
        // DO nothing
      }
      newGame();
      start();
    }
  }