MIT6 189IAP11 Final Proj
MIT6 189IAP11 Final Proj
Game Play
The goal of the project is to implement the basic game play described below.
The game starts with an empty board drawn. The board is typically 10x20 squares. The top left
corner square of the board has coordinates (0, 0) and the bottom right corner square has coordinates
(9, 19) (the y-axis is still ipped). A randomly chosen Tetris piece from the seven possible shapes
is drawn at the top of the board. The piece starts falling at regular intervals - one square at a time.
Basic Rules
1. The piece cannot fall into a square occupied by another piece or beyond the edge of the board.
2. When a piece hits another piece or the bottom of the board, it stops moving and a new piece
appears at the top of the board.
3. As the pieces ll up the board lines form. If a complete line forms, it disappears and all the
blocks above it fall down one line.
4. If a new piece can no longer be placed at the top of the board, the game ends and a Game
Over! message is displayed.
User Interaction
The user can use the arrow keys to move and rotate the pieces - Left, Right, Down arrow keys
move the piece left, right and down by 1 square respectively. The Up arrow key will rotate the
piece. The user can also drop a piece by pressing the spacebar. Dropping a piece means that the
piece will fall down until it can no longer move and the user can longer rotate or move it in any
other direction. When the piece is moved or rotated, it cannot move into another piece or over the
edge of the board.
Project Design
We already had a bit of a head start. Last week, we created objects for the all the tetrominoes
that had the functionality to be drawn on the screen and Project 2 used the same game board
framework as Tetris. As discussed in lecture, we have prepared a starter le that has all the class
and method denitions, but you will have to implement the methods to make your game work. We
will do this step by step - starting small and extending the game features as we go along. Youll
work with your partner and your LA to try your best to implement the methods; well periodically
email out code to the whole class to help you along with the trickier bits.
Most of the methods that you need to implement have just one statement, pass, that tells Python
that the method doesnt do anything currently. All the places where you will need to add code
have a comment YOUR CODE HERE. At the end of the project, you should have code in all
the places where you nd this comment.
READ ALL THE INSTRUCTIONS IN A GIVEN SECTION BEFORE YOU START
WRITING ANY CODE. MAKE SURE THAT YOUR CODE WORKS BEFORE
MOVING ON TO THE NEXT SECTION.
Run your code and make sure the empty board appears on the screen.
Run your code and make sure you see the shape on the screen before you continue.
3. Keyboard Events
Before we can move the shapes around, we need to learn how to get keyboard events, e.g. when a
key is pressed.
If you look at the Tetris. init method. It calls the bind all method on the Window object to
create a key binding that tells the Window object to automatically call the Tetris.key pressed
method when the user presses a key.
Run the code. Click on the Tetris window to make sure that the window is in focus and then
press the arrow keys and the space bar. Notice the output in IDLE. The variable key in the
Tetris.key pressed method has a type string and it contains the value of the key pressed. If you
press the letter a, key will have value a. But, since the arrow keys and the space bar are special
keys, they have the following values:
Up, Down, Right, Left, space
4. Moving Shapes
Modify the Tetris.key pressed and Tetris.do move methods to make the shapes move when the
Left, Right and Down arrow keys are pressed. Take a look at the DIRECTION attribute of the
Tetris class. It is a dictionary with a key that has type string and species the direction to move
the shape, and a value (dx, dy) corresponding to how many units to move along the x and y axis
respectively. Look also at the Shape.move method.
Dont try to implement all the functionality in the Tetris.do move method yet! - well keep adding
to this function in later sections. For now, just add code to move the shape in the appropriate
direction as specied by the parameter.
Run your code and make sure your pieces dont fall o the board when hit all three of the
edges - left, right and bottom.
Run your code and make sure that when you can drop a piece and when it reaches the
bottom, it will be added to the board and a new random piece will appear at the top.
7. Attention! Intruders!
What happens if a shape tries to move to a square that is already occupied? How would you change
your code to make sure that a shape doesnt move to a square that is already taken?
Modify the Board.can move method to check if there is already a piece at the current position
and return True only if there isnt and False otherwise. Hint: Use the in operator on the grid
dictionary to check if there is a value (eg, a block) at the key (x, y).
Run your code and make sure the pieces dont trample each other.
8. Rotating a piece
Now moving a shape is easy, but how do we rotate one? What we need to know is how to rotate a
square around another square 90 degrees. Here is a formula that can help:
x = center.x - dir*center.y + dir*block.y
y = center.y + dir*center.x - dir*block.x
This formula gives the new coordinates of the Block object, block, if it is rotated around the
Block object, center. The variable dir species the direction of rotation (you can nd the current
rotation direction using the Shape.get rotation dir method). If dir = 1, the block is rotated
clockwise (cw), and if dir = -1, the block is rotated counterclockwise (ccw). In the gure below,
the black square is the center block and the other blocks rotate around it, i.e. the black square is
the center of rotation.
The dierent pieces, however, behave dierently. J, L, and T always rotate clockwise. I, S, and
Z rotate back and forth. Z and S rotate clockwise, then counterclockwise, while I goes the other
way. O does not rotate. To implement rotation for each of the pieces you need to know what is the
center of rotation. The black square in the gure below shows the center of rotation for each of the
pieces and this is the block at index 1 in the blocks attribute of the Shape object.
1. Implement the Shape.can rotate and Shape.rotate methods. The shapes are not allowed
to rotate o the board or into another piece. The Shape.can rotate should return False, if
any of the blocks in the shape cannot move to its new position on the board (either because
the position is beyond the boundaries or because the square is already occupied).
2. Now implement the Tetris.do rotate method to rotate the current shape, if possible.
3. Finally, modify your Tetris.key pressed method to rotate a piece when the Up arrow key
is pressed.
Run your code and make sure the pieces rotate when you use the Up arrow key.
Run your code and make sure the piece falls down on its own.
The Board class has several methods to help with implementing this feature - delete row (deletes a
row), is row complete (returns True if all squares in the given row are occupied), move down rows
(moves all rows above the given row inclusive down one square), and remove complete rows (checks
if there are any complete rows and removes them, and then moves all rows above down one).
1. Implement all the four methods described above. Recall the del command for dictionaries,
listed in the dictionary cheat sheet of Exercise 3.3.
2. Then, modify the Tetris.do move method so that every time a shape can no longer move
and is added to the board, it checks if any rows have been completed and removes them.
Run your code and make sure that your game removes completed rows correctly!
Run your code and make sure that your game over message appears on the screen when
you cannot add any more pieces to the board.
CONGRATULATIONS! You now have your very own Tetris game.
2. Piece Preview
To aid your game play, create a preview for the upcoming piece. Create a PiecePreview class
that will also create a CanvasFrame where the next piece will be drawn. You need to gure out
what methods will be necessary to communicate between the Tetris object and the PiecePreview
object in order to both display the next piece and update the current piece when it is necessary.
3. Pause Game
Modify your game so that when the user presses p or P, the game will pause until the user presses
p or P again. By pause, we mean
the piece will stop falling automatically
the user will not be able to move or rotate the piece by pressing the arrow keys while the
game is paused
there will be a message displayed on the board that says the game was paused and how to
resume play.
MIT OpenCourseWare
http://ocw.mit.edu
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.