Time for action - falling pieces

  1. Add a new class to the Flood Control project called "FallingPiece".
  2. Add using Microsoft.Xna.Framework; to the using area at the top of the class.
  3. Update the declaration of the class to read class FallingPiece : GamePiece
  4. Add the following declarations to the FallingPiece class:
    public int VerticalOffset;
    public static int fallRate = 5;
    
  5. Add a constructor for the FallingPiece class:
    public FallingPiece(string pieceType, int verticalOffset)
    : base(pieceType)
    {
    VerticalOffset = verticalOffset;
    }
    
  6. Add a method to update the piece:
    public void UpdatePiece()
    {
    VerticalOffset = (int)MathHelper.Max(
    0,
    VerticalOffset - fallRate);
    }
    

What just happened?

Simpler than a RotatingPiece, a FallingPiece is also a child of the GamePiece class. A falling piece has an offset (how high above its final destination it is currently located) and a falling speed (the number of pixels it will move per update).

As with a RotatingPiece, the constructor passes the pieceType parameter to its base class constructor and uses the verticalOffset parameter to set the VerticalOffset member. Note that the capitalization on these two items differs. Since VerticalOffset is declared as public and therefore capitalized by common C# convention, there is no need to use the "this" notation, since the two variables technically have different names.

Lastly, the UpdatePiece() method subtracts fallRate from VerticalOffset, again using the MathHelper.Max() method to ensure the offset does not fall below zero.

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

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