If any position information does not belong to the accepted_positions, it means the falling piece’s position is invalid. If you are not familiar with the syntax of list comprehension, you might refer to some online material.įrom line 170 to 174, the program checks each position stored in self.tile_pos. At line 168, the program changes the 2-dimensional list into a 1-dimensional list by using list comprehension to flatten it. At line 166, the program gets all the positions which are not occupied by the existing pieces. “is_valid_pos” method is renamed from the orginal function “valid_place”. The above method “get_piece_tile_pos” is called by the method “is_valid_pos” at line 165. Therefore, in order to “translate” the shape list information into position, the program needs to offset row and column value by using (self.row – 4) and (l – 2) to get proper position values. Please note that current piece’s row and column value is indicated by the plus sign. The following diagram illustrates how to get current piece’s position based on current piece’s row and column value. This change is important since it does not need data format conversion. It is different from the original version. In order to keep consistent with the structure of the grid, “positions” list will also follow the structure of using (row index, column index). What it does is to update falling piece’s position information based on piece’s shape, direction, row and col values. This method is converted from the original function “convert_shape_format”. In the constructor method, its value is None.įrom line 144 to 161, the program defines method “get_piece_tile_pos”. The attribute self.tile_pos is added to store falling piece’s tile positions. Please note that I changes attribute self.y and self.x into l and self.row respectively. In the _init_ constructor, the program defines more attributes than that in the original version. In Python 3.x, all the defined class inherits from base class “object” by default. Actually, if you are using Python 3.x, there is no difference if Piece class inherits from object or not. You might notice that the class inherits from base class “object”. At line 130, it defines “shape_color” list which contains seven RGB color tuples, representing different colors of each shape.įrom line 134, the program starts defining a class called “Piece”. Shape “T”, “J” and “L” have four formats, while shape “O” has only one format because no matter which direction it rotates to, it always shows the same shape.Īt line 128, the program defines list called “shape_list” which again nests all the above defined seven shapes. For example, shape “I”, “S” and “Z” have two formats. It is becuase when shapes rotate, their position information gets changed. Some lists contain four nested lists, some contain two nested lists, and others contain one. Those nested lists define the tile position of each shape at different directions. I also added two variables “COL_COUNT” and “ROW_COUNT” because several functions use them.įrom line 25 to line 125, all the code keeps unchanged from the orignal version. I just changed those global variables into capital letters to indicate that they are constants. The beginning of the program does not change a lot. In this post, I will introduced an updated design which makes it easier and clearer to understand the whole process. It is a good idea to redesign the code structure to utilize OOP method. Except this, all the other functionalities are realized by functions. The program defines a class but it only contains one constructor method. Due to the inconsistancy of data storage structure, the program needs to convert the data format in order to check if tile position is valid (in valid_space function) or if one row needs to be cleared (in clear_row method). However, the “locked_positions” dictionary’s key (it stores row and column index of the tile) is arranged in the tuple structure of (column index, row index).Ĭ. This definition is reasonal, because when the program tries to clear one row, such a structure makes it convenient to delete one row’s data and then insert a new row. In the nested list “grid” (it is created in main() function), its first dimension represents row information, and the second dimension represents column information. For each iteration of the main loop, the program needs to load data from locked_positions dictionary and convert data format to fill grid.ī. Since the existing pieces and current piece information is mixed together, the program uses a dictionary called “locked_positions” to store the positions of existing pieces. The code uses grid to store all the position’s color information, including the current falling tetris piece. However, the original tetris code in the above video is a bit complicated, so Python starters might feel confused.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |