Let’s Learn Scratch through Analyzing Others’ Code – a Tetris game Part 2

In the previous post, I analyze the initialization code for the Tetris game, now let’s recall what the main code segment is doing.

After resetting grid to blank and then generating a new piece, the main code enters into a loop. In this loop, the code sets level and piece fall speed. the two factors are decided by the total number of cleared lines. Then the main activity is to render the grid, update key hold frames and update the falling pieces. Since those activities are wrapped in the loop and also the computer runs very fast, the player will see a continuous falling tilt and the existing tiles could update without any lagging.

In this post, I will analyze two blocks “render grid” and “update key hold frames”. I will leave “update falling pieces” to the next post to avoid stuffing too much information.

“render grid” Block

The “render grid” block is a big one. It uses several other blocks. It first draws the black boundary, and then renders the existing tiles of the game area by reading “grid” list and calling “render tile” block. Finally, it renders the falling tiles by calling “get piece coordinates” block and using “render tile” again.

As I introduced in post 1, the game area is divided into 22 rows * 10 columns. The information, such as which position has been occupied by tile, and which color for that tile, is stored in “grid” list. What “render grid” block does is to set up a “working process” of rendering and then extract the column (represented by x), row (represented by y), and color (represented by “letter x of (letter y of grid)”) information and passes them to “render tile” function. The rendering job is actually delivered by the function “render tile”, which I will introduce below.

“render tile” Block

Yes, “render tile” block is the one which really uses the pen to draw things. What “render tile” does is also interesting. First, based on the input type, the function decodes it into different pen color. After that, based on the input x and y values, the pen goes to a calculated coordinate (go to x: y:). Please note that the pen size at that moment is set to GRID_SIZE (value of 16). When it calls “pen down” and then “pen up”, it draws a big round point, like shown in the example, the big yellow circle. Finally, it adjusts the pen’s position and size (set pen size to 4), and draws a hollow square. The two drawings combine together to finish rendering one tile.

“Get piece coordinates” Block

The above “render file” block also calls a block called “get piece coordinate”. I have introduced this block in Part I. I re-post it as a reference. Basically, it uses the falling tiles’ reference point (x ,y ) information and rotation, type input to extract the falling tiles’ rendering position and store them in “piece tiles x” and “piece tiles y” lists, whose item values are then passed to “render tiles” block to draw.


“update key hold frames” Block

The above block “update key hold frames” is used to capture the key press actions. It identifies 4 keys: left/right/up arrows keys, and space key. When there is key press, the corresponding item in “key hold frame” list is plus 1, otherwise, minus 1.

So where is “update key hold frames” used? I will introduce it in the next post.

For this post, that is all of the content till now. See you then!

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.

6 thoughts on “Let’s Learn Scratch through Analyzing Others’ Code – a Tetris game Part 2

  1. Hi Parzian, actually till now I am not worried too much about the spam problem. I am not sure which website tools you are using. For me, I uses the Premium package of wordpress.com, so the anti-spam tool Akismet will automatically filter out some comments or feedback. For those limited pending spam comments, I could manually verify and decide their fortune. Any questions, you could contact me through the email: enquiry@thecodingfun.com. Thanks.


  2. Pingback: Let’s Learn Scratch through Analyzing Others’ Code – a Tetris game Part 3 – Coding Courses Designed for Kids

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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.