程序代写代做代考 Final Project: Tetris

Final Project: Tetris

Starter Code

You do not have to use my starter code for this assignment. Here’s an overview of
what you get if you do:

Tetris: The application, this class sets up the animation and key events and
creates the game and board. Animation and key event messages are sent to the
game object to be handled.

TetrisGame: Controls the game logic. Should control Tetris Pieces and the Board.
Implement moveDown(), moveLeft(), moveRight(), etc to implement the game
behavior.

TetrisBoard: “owns” squares when they have dropped and are on the board.
There are several constants here that you can change if desired:

• SQUARE_SIZE: the length of a side of a tetris square
• Y_DIM_SQUARES: the y dimension measured in squares
• X_DIM_SQUARES: the x dimension measured in squares

The screen is set up to have dimensions (SQUARE_SIZE*X_DIM_SQUARES,
SQUARE_SIZE*Y_DIM_SQUARES)

TetrisSquare: A single square, with a fixed height and width. Each Piece should be
composed of 4 Squares. Squares have been implemented to place themselves at
board coordinate locations, so that a square at board location (2, 3) would actually
draw at location (SQUARE_SIZE*2, SQUARE_SIZE*3), with height and width
SQUARE_SIZE. Two sample squares are drawn on the screen if you run Tetris as
given to you in the assignment.

Checkpoint 1: Tetris Pieces

I recommend dividing this checkpoint into two parts and finishing one part early,
then coming back to the second part later (in the first and second weekends
available for this checkpoint, for example). If you leave the entire checkpoint for the
last weekend you will most likely get behind.

In this part of the assignment, you will implement the basic Tetris Pieces, so that
they move left and right and rotate. In the next assignment, you will implement the
interactions between the pieces and the Tetris Board.

In this part of the assignment, you will:
• code each of the seven tetris pieces
• generate a random piece located at board location (X_DIM_SQUARES/2, 3)

(on the screen near the top) each time your program starts
• code the move left and move right actions
• code the rotate left and rotate right actions
• have special behavior for some tetris pieces: the square piece should

not rotate
• implement checking for the edges of the board

Part 1: Tetris Pieces

Tetris Pieces are made up of 4 Squares, arranged in one of the following seven
configurations. For this checkpoint, your code should generate one of these seven
pieces randomly, and place its “center” square at (X_DIM_SQUARES/2, 3) on the
board (this means it will be visible before it starts falling).

Part 2: move left and right

Each of the 4 squares within a tetris piece has its own location on the screen. In the
diagram below, all of the 7 pieces are shown, with each square labeled with its location,
assuming that the center square is at 5,7 in each case:

4, 6 5, 6

4, 7 5,7

4, 6 5, 6

5,7

5, 8

5, 6 6, 6

5,7

5, 8

4, 6 5, 6

5,7 6,7 4, 7 5,7

5, 6 6, 6

5, 6

5,7

5, 8

5,9

4,7 5,7

5,8

6,7

When writing the rotate code, it will be helpful if your Pieces also calculate a different type
of coordinates:

• the relative locations of each block to the center block

Here is what that looks like in the pieces that can rotate (not the square piece!). The center
square is shaded grey, and each square is labeled with its relative location:

Part 3: rotate left and right

If you look at the actual locations of the squares during a rotate, its hard to see a pattern.
Here is rotate for the z piece:

4, 7 5,7

5, 6 6, 6

3, 7 4,7

4, 6 5, 6

Move left

4, 7 5,7

5, 6 6, 6

5, 7 6,7

6, 6 7, 6

Move right

6, 65, 6

4, 7 5,7

Original location

6, 65, 6

4, 7 5,7

Original location

4, 7 5,7

5, 6 6, 64,6

5,7 6,7

6,8

Rotate right

4, 7 5,7

5, 6 6, 6

5,8

5,74,7

4,6

Rotate left

-1,-1 0,-1

0,0

0,1

0,-1 1,-1

0,0

0,1

-1,-1 0,-1

0,0 1,0 -1,0 0,0

0,-1 1,-1

0,-1

0,0

0,1

0,2

-1,0 0,0

0,1

1,0

To move the piece, we can just change the location of each of the squares in the piece.
Let’s take the Z shaped piece as an example. Move left and move right change the location
of the squares within the piece, but they all change in the same way (+1 or -1 to the x
location):

Once you have relative locations, rotate is easier to understand and implement. Here is
rotate for the z piece in pictures, though the formulas below work for all the pieces that
rotate:

-1,0 5,7

5, 6 1,-10,-1

0,0 1,0

1,1

1,-10,-1

-1,0 0,0

Original relative locations Rotate right

4, 7 5,7

0,-1 1,-1

0,1

0,0-1,0

-1,-1

Rotate left

For rotate right in all rotatable pieces, the relative locations change as follows:
• newRelativeX = -oldRelativeY
• newRelativeY = oldRelativeX

For rotate left in all rotatable pieces, the relative locations change as follows:
• newRelativeX = oldRelativeY
• newRelativeY = -oldRelativeX

You’ll need to work out the formula for calculating the locations of the square relative to the
center of the block, and the formula for going back to actual locations after calculating the
new relative locations.

Part 4: checking for the left and right borders

For now, you should implement the checks to be sure that the Piece doesn’t move
beyond the edge of the board. There is one trick to doing this: your code should
check whether all 4 squares can move before moving any squares.

So, your code (wherever you put this method) should look something like this:
1. check all 4 squares to see if they can move
2. move all 4 squares

This may require you to change the code you’ve written so far a bit, so you may want to
have this step in the back of your mind while designing the earlier parts.
To check for the edges of the board, for each square check:
– newx < 0 - newx > TetrisBoard.X_DIM_SQUARES
– newy < 0 - newy > TetrisBoard.Y_DIM_SQUARES

Posted in Uncategorized

Leave a Reply

Your email address will not be published. Required fields are marked *