Example #1
0
  public void paintComponent(Graphics g) {
    super.paintComponent(g);

    height = this.getHeight();
    width = this.getWidth();

    drawPlayers(g);
    g.setColor(Color.WHITE);
    g.drawString("Score", width - 60, 25);
    g.drawString(String.valueOf(score), width - 60, 40);

    if (countdownF) {
      g.drawString(String.valueOf(counterN), width / 2 - 10, height / 2);
    } else {
      if ((spawnCircleB) && (spawnIncrease)) {
        spawnCircles();
      }
      if (spawnMonsterB) {
        spawnMonsters();
      }
      if (spawnRandomersB) {
        spawnRandomers();
      }
      if (spawnRainB) {
        spawnRain();
      }
      if (spawnBombB()) {
        spawnBomb();
      }

      try {
        Iterator i = enemies.iterator();
        while (i.hasNext()) {
          Enemy e = (Enemy) i.next();
          Iterator j = players.iterator();
          while (j.hasNext()) {
            Player p = (Player) j.next();
            e.move(players, programSpeedAdjust /*/players.size()*/);
            if ((e.collidesWith(p.getX(), p.getY())) && (!p.getImmunity())) {
              p.decLives(p.getX(), p.getY());
              p.setImmunity(true);
            }
          }
          e.paint(g);
        }
      } catch (Exception e) {
      }
    }
    drawLayout(g);
  }
  /**
   * 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();
    }
  }