Learn Scratch by Reading and Analyzing Others’ Project – Another Way to Design Tetris, Part 2

Let’s continue the analysis of Tetris Game.

“Tetris Pieces” Sprite (Cont.)

“when Down arrow key pressed” Code Segment

When Down arrow key is pressed, the falling piece will accelerate falling down. The program uses a “repeat until” block to realize a continuous acceleration process. The speedy falling down will continue until Down arrow key is released (not key down arrow pressed?) or the falling piece has been settled (Done = 1).

In the first post, we introduced that the Fall block will move the Tetris piece down by one row and check if such a movement is valid. The speedy falling means that the falling interval is reduced dramatically to 1/10 of normal falling interval (wait Time/10 seconds).

“when Up arrow pressed” Code Segment

Up arrow key has different functionality compared to other arrow keys. Each time it is clicked, the Tetris falling piece will turn 90 degree close-wise. This logic is simple, but the program should also check different boundary conditions. For example, how about the rotated piece touches the existing pieces? How about the rotation results into the piece falling out of boundary? A large portion of this code segment is used to handle different exceptional conditions.

When the rotated piece touches the bottom or other existing pieces, it will cancel the rotation. When the rotation touches the left boundary, the falling piece will offset to the right by 20 units or 40 units, depending on how much it crosses boundary. The same rule applies to when it exceeds the right side boundary.

Since there is an automatic horizontal adjustment when touching boundary, the code also needs to check if the falling piece touches any existing pieces after horizontal offset, which is done by “Cancel if Touching” block. If it touches, the program will cancel both the horizontal adjustment and rotation.

“Cancel if Touching” Code Segment

“Line” Sprite

In the last post, when I introduced “Tetris Pieces” sprite, its “when I receive start” code segment will broadcast “check lines” message after the falling piece is settled. This message is received by “Line” sprite, whose costume is a line consisting of 10 tiles which precisely match one row in the board area.

As we could calculate, there are 17 rows in the board area in total. What this line sprite does is to iterate 17 times and check if it touches color white. It begins by checking the line at the bottom of the board. If it touches, it means the current row is not fully occupied and could not be cleared. It will then continue checking the above lines.

If the line has not touched any white color in one row, it means that row is fully occupied and could be cleared up. The program will change Line Count by 1 and then broadcast “clear line” message. The “Line Count” variable will show on the screen, indicating accumulated cleared lines. The “Line Number” variable indicates the row index which should be cleared up.

When Squares sprite receives “clear lines” message, how should it do to clear the line? Follow me to take a look below.

Squares Sprite

After receiving “clear line” message, Squares sprite will move to the position of (x: 90, y:-150), which is the position of left most tile on the bottom row. Using the value of Line Number, Squares sprite moves up to the row which would be cleared up by “change y by (Line Number) * 20”.

If one row needs to be cleared up, the program will call “Copy Line” block. “Copy Line” block will copy the whole above line into the current line through stamping Squares Sprite. After repeating (17 – Line Number) times, this program could copy all the above rows and move them down all by one row.

Since the computer runs very fast, the player would not feel the stamping process, but only feel that a row is cleared up and all the above rows drop down accordingly.

“Copy Line” Block Definition

In this block, the Squares sprite first moves up by one row and gets the color at that position. It changes its costume to the same color, moves down to current position (change y by -20) and then makes a stamp for itself. The same operation repeats 10 times to copy all the 10 tiles on this row.

Personal Opinion

In this game, Tetris Pieces sprite is responsible for generating the falling piece and responding to key pressing operations. Line sprite and Squares sprite could be regarded as the core of the game. Without them, rows could not be cleared correctly. The key to their success is that their size and position totally coincident with the stamps of Tetris Pieces sprite. Therefore, Line sprite could identify the color of each position accurately, and Squares sprite could replace the color of each position with its new stamp.

Another highlight of this game is that it uses different colors to trigger different judgement. In order to judge if the falling piece crosses the left or right boundary, the program designs four tones of gray, so that it could judge accurately which side the piece is crossing and how deep it crosses. It uses another tone of gray to judge if the falling piece touches the existing pieces. It uses black color to judge the bottom of the board.

I believe the above two highlights could be well applied to other scenarios.

That is all for this version of Tetris game. Feel much simpler and conciser? It does not use any lists to store the tiles information, but achieves the same functionality. Hope you enjoy the coding and have fun!

Note: All the analysis articles are copyright products of http://www.thecodingfun.com. 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.