By Clarence Crawford,2014-07-08 11:10
7 views 0

Tetris, Part 3

    TetrisPiece Class

    As of Part 2, your TetrisPiece class should have the following:

    ; Private variables (free code).

    ; Public constants (1-7) (free code).

    ; A default constructor that will create a random TetrisPiece and initialize it. ; A constructor that will create a TetrisPiece that is a copy of another TetrisPiece (free


    ; A CopyArray method that returns a 2D array that is a copy of the array in the

    TetrisPiece that was passed in (free code).

    ; A method to initialize the color of the TetrisPiece (mostly free code). ; A method to initialize the array of the TetrisPiece (mostly free code). ; Property procedures (get, set) for the TetrisPiece's Type, Color, Array, and Location. ; A method to rotate the piece (its array) right 90 degrees (free code). ; A method to find the leftmost column of a given row in the array that has a tile in it

    (free code).

    ; A method to find the rightmost column of a given row in the array that has a tile in it

    (free code).

    This is everything! When you have implemented all of the above, your Tetrispiece class is complete.

    TetrisBoard Class

    Your TetrisBoard class should have the following:

    ; Private variable (the array, free code).

    ; Private constants (free code).

    ; A constructor that will initialize the 20x10 array to zeros.

    ; A public LeftMoveIsOK method that will determine if the piece that is passed to it can

    move left (free code).

    ; A public RightMoveIsOK method that will determine if the piece that is passed to it

    can move right (free code).

    ; A public RotationIsOK method that will determine if the piece that is passed to it can

    rotate (free code).

    ; A public int method GetBoardCell that will retrieve a value (int) from a given cell on

    the board. The row and column need to be passed in to this method. You will need

    this to determine which color to draw in this cell when drawing the board on the


    ; A public boolean method MoveIsBetweenMargins that will determine if a piece that is

    passed to it will be within the margins of the board. I passed in two arguments to

    this function: the TetrisPiece and a Point representing the location I am considering

    moving the piece to. I need the TetrisPiece so I know how wide it is. Note that you

    could do the same thing by just passing the width of the piece's array instead of

    passing the entire piece. There's more than one way to do just about everything.

    ; A public void method CopyPieceToBoard that will copy a piece that is passed to it into

    the corresponding cells on the board. This only needs to be done when the piece has

    reached its final position (either the bottom of the board or the top of another piece). ; A public boolean function PieceIsInFinalLocation that will look at a piece that is

    passed to it and determine if it is in its final location. Initially, we will define "final

    location" as the bottom row. Later on, we will define "final location" as meaning that

    it can drop no more, either because it is on the bottom row, or because it is resting

    on another piece.

    Still to do

    ; A private void method DeleteRow that will delete a single (full) row from the board.

    This simply involves copying all of the rows above the given row down one position.

    For example, if the row to be deleted is row 10, move row 9 down to row 10. Move

    row 8 down to row 9. Move row 7 down to row 8, etc. This is a simple for loop.

    ; A public int method DeleteFullRows that will delete all full rows from the board. This

    method should locate a row that is full and call This method returns an int because it

    returns the number of points that were earned by the player, and this is only

    determined by counting the number of rows that were deleted (1 row = 100, 2 rows

    = 300, 3 rows = 600, and 4 rows = 1000). Start at the top of the board (row 0) and

    work your way down. A row is full if there are no zeros in the row.

    Main Program (Game1.cs)

    ; Initialize method: Initialize screen dimensions, score, any other variables. ; LoadContent method: Load board, square, fonts, sound effects. Create the first piece.

    Initialize any rectangles and vectors that you will have to use for displaying text and

    textures on the board.

    ; GetInput method: Determine the state and then determine what, if any, keystroke

    (InputType) needs to be returned. Most of the time the InputType None will be


    ; DrawPiece method: Pass in a TetrisPiece that is to be drawn. Draw all of the squares

    that make up a piece. You will need to step through the piece’s array one cell at a

    time. If there is a 0 in the cell, do nothing. If there is a non-zero value in the cell,

    you must draw the correct colored square in the correct location on the board. This is

    a void method.

    ; DrawCell method: Draw a single cell at a specified location and a specified color.

    Pass in the row on the board, the column on the board, and the color to be drawn at

    that row and that column. This method will be called four times whenever you draw

    the current piece because every TetrisPiece has four squares. This is a void method. ; UpdatePiece method: This method will take the current piece and the current input

    from the user and determine the next state of the current piece. This method does

    NOT draw anything! It just updates the current piece’s location (and possibly its

    array if the input command was to rotate). Before updating the piece’s location, you

    must check to make sure that the move is legal. If the move is to move left, you

    cannot move left if you are currently in column 0. You can also not move left if one

    of the current piece’s cells is immediately to the right of a filled cell in the board. You

    must also consider the same type of things for a right move, down move, and

    rotation. This method must also update our countdown timer to see if 60 ticks have passed. If our countdown timer is down to 0, reset it, and drop the piece one row (add 1 to its Y location value). This method must also check to see if the piece has reached its final location. If so, then get a new current piece.

Report this document

For any questions or suggestions please email