Homework Assignment Due November 15
Create the next step in building a version of Tetris: Falling T pieces that you can shift and rotate as they fall. When they land, they form piles.
You may use either tetris_base_1.py or tetris_base_3.py as your starting point. tetris_base_3.py is recommended, since it is already refactored to "think" in terms of rows and columns rather than pixels.
The initial version of the program shows falling T pieces that you can shift and rotate. When a piece reaches the bottom, it just dissappears to be replaced by a new piece at the top.
Your version of this program must have the piece stop when it reaches the bottom or when it lands on another piece. When the piece lands, it should be recorded as a collection of four squares. The easiest way to do this is to creade a list of lists of zeroes.
You detect if a piece has landed by looking at each block in the piece and checking the location on the map that is one row below that block. If any such location is either occupied or off the map, then the piece has landed. In that case it is time to put a 1 in the map for each location that is occupied by a block in the piece that just landed.
The initial version of the program shows falling T pieces that you can shift and rotate. When a piece reaches the bottom, it just dissappears to be replaced by a new piece at the top.
Your version of this program must have the piece stop when it reaches the bottom or when it lands on another piece. When the piece lands, it should be recorded as a collection of four squares. The easiest way to do this is to creade a list of lists of zeroes.
width = 10 # columns
height = 20 # rows
map = []
for row in range(height):
row_list = []
for col in range(width):
row_list.append(0)
map.append(row_list)
A terse approach to getting the same result:
width = 10 # columns height = 20 # rows map = [ [0] * width ] * heightEither way, if you paste the code into the python interpreter, you should be able to print out a map that looks like this:
>>> from pprint import pprint as pp >>> pp(map) [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]You mark a location in the map as occupied as follows:
map[row][col] = 1You will need to do that four times whenever a piece lands. (Because each piece consists of four blocks.)
You detect if a piece has landed by looking at each block in the piece and checking the location on the map that is one row below that block. If any such location is either occupied or off the map, then the piece has landed. In that case it is time to put a 1 in the map for each location that is occupied by a block in the piece that just landed.