Use PyGame to Create Tetris Game – An Updated Version Part 2

In my previous post, I introduced the first part of Tetris game written in Python. Today, let’s continue the left part of the code. In the first part, we have introduced the class Piece and several functions which will be used by the main() function. Now, let’s see what the main() function is doing.

Starting from line 283, the main() function begins. It first declares two global variables “grid” and “score”. Defining global variables avoids passing the arguments among different functions, because the functions could use those global variables directly. Please note that the variable “grid” is a nested list. Its first dimension represents the 20 rows, and the nested second dimension represents 10 columns. As to why we define “grid” variable like this structure, I have explained it in the first part of the code analysis. This structure is more convenient when clearing rows of data and inserting new rows.

From line 289 to 292, this program defines several variables. “score” represents the total score user gets. “b_accelerate” variable is of a Boolean type. When user presses Down arrow key or Space key, representing the user wants the tetris piece fall down quickly, then this variable is set to True. “change_piece” indicates that the current piece has settled and the next new piece is going to drop.

At line 293 and 294, the program defines two Piece objects, representing the current and next tetris piece, respectively.

Starting from line 298, the program enters main loop. Through calling “clock.get_rawtime()”, the program gets the time passed after the last calling of “clock.tick()”. If fall_time is bigger than the expected fall_speed 0.27, the current piece’s row attribute increases by 1, meaning that the tetris piece drops by one row. However, if “b_accelerate” variale is True, no matter if fall_time is bigger than fall_speed or not, the current piece’s row attribute will increase by 1 because it is in the state of falling acceleration.

From line 308 to line 310, the program checks if the current piece drops to an invalid position. If it does, it reverses to its previous row by calling “current_piece.row – = 1”. It also indicates the current piece has found its final position. Therefore, “change_piece” variable is set to True.

From line 312 to line 341, the program catches the events to see if user presses the arrow keys or Space key. Bases on those keyboard events, current piece’s row or col attributes change accordingly.

Please note that after each change of row or col attributes, the program will call “current_piece.is_valid_pos()” to check if the new position is valid or not. If the new position is invalid, it will cancel position change and restore to previous position.

At line 343, the program calls Piece class’s get_piece_tile_pos() method to retreive the falling piece’s position. Recall this method “get_piece_tile_pos” will utilize “shape_list” to return a position list, which is corresponding to the positions of current piece on “grid” variable. This position information is assigned to current_piece.pos attribute.

The program then draws the existing tiles and falling piece. If “change_piece” is True, it adds the current piece’s position into “grid” variable, assigns next_piece to current_piece, and then creates a new Piece object as next piece.

After all the above steps, the program will check if any existing rows are fully occupied. It calls “clear_rows” function to update the “grid” variable.

From line 359 to line 365, the program draws the next Piece object to the right side of the displaying area, draws grid lines and boundary, and draws score text. The last step is to call “pygame.display.update()” to show the image to the end user.

At line 369, the program calls “check_lost” function to check if any existing tile has been piling up to the ceiling of the playing area, which indicate the status of “game over”.

At line 372, when the game is over and exits the main loop, the program shows “You Lose” text and then waits for two seconds. However, the whole program is not yet stopped, since main() function is wrapped in loop of function main_menu. When main() function is exited and user presses any key, another main() function starts, so the game could continue.

That is the complete anlaysis of Tetris game written in Python’s PyGame framework. Hope you enjoy the coding and keep tuned to our future posts. See you then.

Note: All the analysis articles are copyright products of Anyone re-posting them should credit author and original source. Anyone using them for commercial purposes or translating them into other languages should notify TheCodingFun and get confirmation first. All Rights Reserved.

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.