Time for action - adding difficulty levels

  1. Add the following declarations to the Game1 class:
    int currentLevel = 0;
    int linesCompletedThisLevel = 0;
    const float floodAccelerationPerLevel = 0.5f;
    Vector2 levelTextPosition = new Vector2(512, 215);
    
  2. Add the StartNewLevel() method to the Game1 class:
    private void StartNewLevel()
    {
    currentLevel++;
    floodCount = 0.0f;
    linesCompletedThisLevel = 0;
    floodIncreaseAmount += floodAccelerationPerLevel;
    gameBoard.ClearBoard();
    gameBoard.GenerateNewPieces(false);
    }
    
  3. Modify the Update() method of the Game1 class by updating the case section for GameState.TitleScreen to include the following right before the game state is set to GameState.Playing:
    currentLevel = 0;
    floodIncreaseAmount = 0.0f;
    StartNewLevel();
    
  4. Modify the CheckScoringChain() method to increment the linesCompletedThisLevel variable right after playerScore += DetermineScore(WaterChain.Count);
    linesCompletedThisLevel++;
    
  5. Still in the CheckScoringChain() method, add the following to call the StartNewLevel() method if necessary. Place this code directly after the foreach loop that fades out tiles on the board:
    if (linesCompletedThisLevel >= 10)
    {
    StartNewLevel();
    }
    
  6. Update the Draw() method to display the current level in the appropriate location on the screen. Place this code right after the spriteBatch.DrawString() call that displays the player's score:
    spriteBatch.DrawString(pericles36Font,
    currentLevel.ToString(),
    levelTextPosition,
    Color.Black);
    
  7. Play! Flood Control is now completed, so try it out!

What just happened?

The current game level and the number of lines the player has completed in the current level are tracked as integers (currentLevel and linesCompletedThisLevel). The two constants, baseFloodAmount and floodAccelerationPerLevel, determine how much water is added to the facility every time the flood is updated. Finally, the levelTextPosition vector points to the location on the screen where the level number will be displayed.

The StartNewLevel() method increases the currentLevel, and clears the floodCount and lineCompletedThisLevel variables. It increases the floodIncreaseAmount by the value of floodAccelerationPerLevel and then clears the game board. Finally, new pieces are generated for each square on the board.

When beginning a new game (the updates in Step 3) we can simply set currentLevel and floodIncreaseAmount to zero, and then call the StartNewLevel() method. Since both of these variables are increased by StartNewLevel() the first level of a new game will begin with the appropriate values.

Step 4 increases the counter that tracks the number of lines the player has completed on the current level every time a scoring chain results in points. Step 5 checks to see if the player has completed 10 or more lines. If they have, a new level is started.

Finally, drawing the level number is a call to the simple form of SpriteBatch.DrawString() just as we did for displaying the player's score.

Have a go hero -

There are a number of different things you could do to spruce up Flood Control. Here are a few suggestions to try using the knowledge you have gained over these two chapters:

  • Basic add a "Paused" game state that displays an indication that the game is paused and how to resume play. To prevent cheating, the game board should either not be visible or be obscured in some way while the game is paused.
  • Intermediate the Game Over screen is not very exciting. Create a new bitmap image indicating the aftermath of the flooded facility and display that image instead of the simple Game Over! text. You will need to load the image via the LoadContent() method and display it when appropriate.
  • Advanced create an additional "suffix" for pieces that are locked down and cannot be turned. You'll need to expand the Tile_Sheet.png file by adding an additional (fourth) column and then copying the first two columns to columns three and four. Draw bolts in the four corners of each of the twelve new piece images and modify the draw code to add an additional 40 pixels to the X value of the source Rectangle if the piece contains the locked suffix. Grant extra points for using locked pieces in a scoring chain.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.2.184