示例#1
0
  /**
   * regulateFramerate: This method regulates the framerate of the game. Pre: The game must have
   * been initialized. Post: The game's framerate has been regulated.
   */
  private static void regulateFramerate() {
    // CODE BLOCK:
    // Framerate regulator
    lGameLoopEndTime = System.currentTimeMillis();
    lGameLoopTimeTaken = (lGameLoopEndTime - lGameLoopStartTime);
    lLastMSPFO = lGameLoopTimeTaken;

    // Framerate regulator for stable gameplay.
    if (lCurrentFrame % GameSettings.iFPSRegulationPeriod
        == 0) { // Every "GameSettings.iFPSRegulationPeriod" amount of frames,
      if (iMSPFOGmAdj < lGameLoopTimeTaken) { // If the current framerate is insufficient,
        iMSPFOGmAdj++; // slow the game down.
      } else if (lGameLoopTimeTaken
          > GameSettings
              .iMSPFOGm) { // But if the framerate is slower than the max and the framerate is more
        // than sufficient,
        iMSPFOGmAdj--; // speed the game up.
      }
    }

    // System.out.println("ms for gameplay frame: " + lGameLoopTimeTaken);
    lTimeToSleep = iMSPFOGmAdj - lGameLoopTimeTaken;
    if (lTimeToSleep
        > 0) { // Because we don't want to sleep for negative times. Because we don't know what that
      // does.
      try {
        // Thread.yield(); Unknown effects
        Thread.sleep(iMSPFOGmAdj - lGameLoopTimeTaken);
      } catch (InterruptedException e) {
        System.err.println("Who interrupted the main thread's slumber?");
      }
    }
    // END OF CODE BLOCK
  }
示例#2
0
  /**
   * main: This method initializes and coordinates the main actions of the game. Pre: None Post: The
   * game has been completed
   */
  public static void main(String[] args) {
    JOptionPane.showMessageDialog(
        null,
        "CLEANSANITY\n"
            + "Instructions:\n"
            + "Use W, A, S, and D to move the Cleansinator towards the exit (a pulsating GREEN tile)\n"
            + "Clean the forces of dirt by pointing at them and pressing the RIGHT or LEFT MOUSE BUTTONS.\n"
            + "Use COMMA and PERIOD to zoom in and out.\n"
            + "Press SHIFT to toggle sprint.\n"
            + "Use P and O to increase/decrease the volume.",
        "CLEANSANITY",
        JOptionPane.PLAIN_MESSAGE);

    String strSeedToSetTo =
        JOptionPane.showInputDialog(
            null, "Input a game seed:", "CLEANSANITY", JOptionPane.INFORMATION_MESSAGE);
    try {
      iCurrentMapSeed = Integer.parseInt(strSeedToSetTo);
    } catch (NumberFormatException e) {
      iCurrentMapSeed = strSeedToSetTo.hashCode();
    }

    JOptionPane.showMessageDialog(
        null,
        "CLEANSANITY\n" + "NOW PLAYING: " + iCurrentMapSeed,
        "CLEANSANITY",
        JOptionPane.PLAIN_MESSAGE);

    strGamePath = new File("").getAbsolutePath();

    // Initialization begins
    iGameReadinessState = -1;
    mainGameWindow = new GameWindow();

    GameInput.initGameInput();
    GameSettings.initGameSettings();
    GameSettings.setDefaultKeyBindings();
    ColorScheme.initColorScheme();

    iMSPFOGmAdj = GameSettings.iMSPFOGm;
    lCurrentFrame = 0;
    mainGameWindow.start();

    entveCurrentEntities = new Vector<Entity>();
    addEntity(
        ContentLibrary.PLAYER_BLUEPRINT,
        0,
        0,
        0,
        new ControllerPlayer(),
        new SkeletonHumanoid(),
        ContentLibrary.PLAYER_COLORS);
    // addEntity(ContentLibrary.RAT_BLUEPRINT, 10,17,0, new ControllerAI(), new SkeletonCreature(),
    // ContentLibrary.CREATURE_COLORS);
    // addEntity(ContentLibrary.DIRTY_BUBBLE_BLUEPRINT, 15,15,0, new ControllerAI(), new
    // SkeletonBubble(), ContentLibrary.DIRTY_BUBBLE_COLORS);
    addItem(
        new Item(
            ContentLibrary.DUSTER_BLUEPRINT,
            new ControllerItem(),
            new SkeletonDuster(),
            ContentLibrary.DUSTER_COLORS,
            0,
            handleEntity(0).ensSkeleton.sklaSkeleton[5]),
        handleEntity(0));
    addItem(
        new Item(
            ContentLibrary.BROOM_BLUEPRINT,
            new ControllerItem(),
            new SkeletonBroom(),
            ContentLibrary.BROOM_COLORS,
            0,
            handleEntity(0).ensSkeleton.sklaSkeleton[6]),
        handleEntity(0));

    dngCurrentDungeon = new Dungeon(iCurrentMapSeed);

    iGameReadinessState += 1;
    // Initialization ends
    bRenderMenu = false;
    bRenderGame = true;

    mainGameWindow.show();
    // The Gameplay Loop
    while (true) {

      lGameLoopStartTime = System.currentTimeMillis();

      if (bRenderGame) {
        doGameLoop();
        ColorScheme.updateColorList();
        lCurrentFrame++;
      }
      // Do features such as zoom in, zoom out, calling out menus, etc.
      doNonGameplayInput();
      // Checks whether different music needs to be played
      GameSounds.updateGameSounds();
      regulateFramerate();
    }
  }