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

In one of previous posts, I analyzed a design of Tetris game, which uses multiple lists to store Tetris piece information, and then uses Pen functionality to render Tetris pieces. It remakes the classic Tetris game perfectly, but just one weakness – the code structure is a bit too complicated.

In this episodes of posts, I would like to introduce another method to design Tetris game. The code structure is much simpler and there are some unique design to work around the initial limitation of this method. Let’s take a look at the final effect through the video.

The original project could be opened at: https://scratch.mit.edu/projects/21976095

As usual, I will introduce auxiliary sprites first, and then main sprites.

Auxiliary Sprites

Backdrop Sprite

The backdrop code is shown below. The middle white board area is where the Tetris pieces drops, while both sides are areas showing information such as the next dropping piece, accumulated cleared lines and how to use Up, Down, Left, Right and Space keys.

When Start green flag is pressed, this sprite will erase all the previous pen strokes, and broadcast two messages – “start” and ” game over 2″. It will initiate the values of three variables and then set background music. Tetris Pieces sprite (I will introduce it later) will receive “start” message and then falls from the top of the board area. Game Over sprite will receive “game over 2” message and then hide itself.

Among the three variables, “Count” represent the total cleared lines. Time represents the time interval between two falling pieces. “Pause” represents if the game will enter into Pause status.

When space key is pressed, the Pause variable will switch its status between 0 and 1 and broadcast two different messages. The two messages “pause” and “pause 2” will be received by Pause sprite.

“Game Over” Sprite

There are two simple event procedures for this sprite. When the sprite receives “game over 2” message, it will hide itself. When it receives “game over” message, it will show and say “game over”.

The “game over 2” message is not really appropriate here. It is better rename it to “play” or something like that. This message represents that the game is running as normal.

“Pause” Sprite

Similar to “game over” sprite, “Pause” sprite also has two simple event procedures. When it receives “pause” message, show up and say “Pause”.

There is another variable “Pause” related to the pause status. Later on, you will see that when Pause = 1, the whole program just freezes without moving. When it receives “pause 2” message or when Start green flag is pressed, the sprite will hide itself.

Here, the similar problem exists. It is better change “pause 2” message to “no pause” or some names like that, which could better explain the real meaning of the code.

“Next Tetris Preview” Sprite

The sprite is circled in blue. Upon receiving “switch” message, it just switches costume randomly from costume 1 to 7. It reminds the player what the next Tetris piece looks like.

Till now, I have introduced all the auxiliary sprites. The code structure of them is much simpler than the other core sprites. In the following sections, I will introduce the core sprites.

Core Sprites

“Tetris Pieces” Sprite

“when I receive start” Code Segment

This is one of the core sprites in this game. It has multiple costumes representing different Tetris shape and colors. When this sprite receives “when I receive start” message, it will switch costume to that of “Next Piece Preview” sprite, so the Tetris shape indicated by “Next Piece Preview” will fall from the top of the board area.

The program will then broadcast “switch” message. The “Next Piece Preview” sprite receives this message and changes to a randomly selected costume from 7 costume choices.

After that, the program will verify if it touches the edge of any existing pieces. If it touches, it means that the pieces have tiled up high to the top of the board, which indicates game over. Otherwise, it sets value of Done variable to 0 and then makes the Tetris piece fall with a certain speed (after each falling step, wait “time” seconds).

At the end of this code segment, it will check how many lines have been cleared up. For every 10 lines cleared up, the Time variable will multiply 0.9, so the falling interval between two pieces is reduced and the game speeds up.

Please note that this event “when I receive start” is not only broadcast at the beginning of the game. It will be broadcast repeatedly by Line sprite. In the next post, I will introduce it.

“Fall” Block

This block is called by the above “when I receive start” code segment. It is responsible for moving the Tetris Pieces sprite down by one row’s height. Since each row occupies 20 units vertically, it will change y by -20. It then checks if the falling piece touches the bottom line (black color) or other existing pieces (dark gray color). If it touches either of them, stop falling and return back to previous position through “change y by 20”.

At this final position, the program will stamp this sprite and set Done to 1, which means that the current falling piece has been settled. It will broadcast “check lines” message to check if there are any lines fully occupied and could be cleared up. This message will be received by another sprite – Line sprite, which I will introduce in the next post.

When Left/Right arrow keys are pressed

Those code segments will move the piece to the left or right by one tile’s width (change x by 20). However, if it touches the boundary which is indicated by different tones of gray, it will return the piece back to the original position.

I will pause here for this post. There are still parts of Tetris Pieces not introduced. Another two sprites – Line sprite and Squares sprite, also play an important role in realizing the functionality and their design is creative. I will introduce them all in the next post. Stay tuned!

and don’t forget to 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.

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

  1. Pingback: Learn Scratch by Reading and Analyzing Others’ Project – Airplane Shooting Game – The Coding Fun

Comments are closed.