Time for action - displaying the flood

  1. Add the following declarations to the Game1 class:
    const int MaxWaterHeight = 244;
    const int WaterWidth = 297;
    Vector2 waterOverlayStart = new Vector2(85, 245);
    Vector2 waterPosition = new Vector2(478, 338);
    
  2. Modify the Draw() method of the Game1 class by adding the following right after the SpriteBatch.DrawString() call that displays the player's score:
    int waterHeight = (int)(MaxWaterHeight * (floodCount / 100));
    spriteBatch.Draw(backgroundScreen,
    new Rectangle(
    (int)waterPosition.X,
    (int)waterPosition.Y + (MaxWaterHeight - waterHeight),
    WaterWidth,
    waterHeight),
    new Rectangle(
    (int)waterOverlayStart.X,
    (int)waterOverlayStart.Y + (MaxWaterHeight - waterHeight),
    WaterWidth,
    waterHeight),
    new Color(255, 255, 255, 180));
    
  3. Try it out! You should now be able to watch the flood slowly increase in the flood tank. When it reaches the top the game should switch to the GameOver state and, after an 8 second delay, back to the title screen.

What just happened?

The two int values, MaxWaterHeight, and WaterWidth refer to the size of the water image hidden inside the game board. It is 297 pixels wide, and the full water image is 244 pixels high.

Two vectors are used to store the location of the filled water image (85, 245) and the location that it will be drawn to on the screen (478, 338).

In order to draw the water in the water tank, the waterHeight variable, the MaxWaterHeight is multiplied by the percentage of water currently in the tank. This results in the number of pixels of water that need to be drawn into the tank.

When determining the source and destination rectangles, the X coordinates are dependant only on the location of the overlay and the drawing position, since they will not change.

The Y coordinates must be modified to pull pixels from the bottom of the image and expand upwards. In order to accomplish this, the current waterHeight is subtracted from the MaxWaterHeight, and this value is added to the Y coordinate of both vectors.

Difficulty levels

Now that the game can end, we need some way to make the game more difficult the longer the player plays.

After the player has completed 10 scoring chains, the water tank will be emptied, a new set of game pieces will be generated, and the flood will increase faster.

..................Content has been hidden....................

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