Learn Scratch by analyzing Others’ Project – Remake Popular Game 2048 – Part 2


The Main Sprite – Backdrop (Continue.)

In the previous post, I explained the main code segments of sprite backdrop, now I will continue explaining it before I switch to other sprites.

“Checkifgameover” Block

In the main segment that I mentioned in the previous post, after Replace and Change operation, the program will add a new number at a random position and then check if game is over. The first sign of game over is that all the grids have been occupied with number, but it is possible at this condition that adjacent grids have equal numbers, so still need to further check if it is really game over or not. This verification is done by “check if game over” block.

“check if game over” Block

In this block, the program will scan all the grids to check if there are adjacent equal numbers. If there is any, just set “goon” variable to 1 and then exit the block. The program will go on running.

An example of checking if game is over

This example demonstrates the scenario where the play board is fully occupied, but is actually not game over. There are two adjacent grids having equal number of “2” as I circle them in red.

Stop the game

If the above “check if game over” block verifies game over, the variable “game over” changes to 1, the backdrop sprite will broadcast “game over” message, so all of other sprites could do the proper operation and finally the whole program will stop.

Here I list two code segments. The left one is the original one which uses a forever loop. However, it is better to use “wait until” block which is more readable and concise. I have put the updated version on the right for your reference.

Adjust the tune of the game

The program provides two tones – a brighter one and a darker one. When the variable “dark” toggles value, the tone should change accordingly.

Other Sprites

Play board

the play board sprite is generally simple. For one thing, it should change its brightness when tone changes; for the other thing, when the current score exceeds the recorded score, it sets the new score record to the variable “HiFF” and set current user name to the variable “ByFF”. The display of score and username will be done by another sprites “counter1”, “counter2” and “counter4”. I will not explain them in this post.

Left Sprite – Show Number

When I explain the main sprite backdrop, I specifically introduce how the Replace and Change blocks will update the item values of Data list, but who is responsible for showing those values in the Data list? It is Left sprite. I would say that the author of the game has not named his sprites very well. This sprite is better renamed to something like “Show number” or “number costume”.

This sprite firstly creates 16 clones of itself and assign each clone a ID called my.id. In the event of “when I start as a clone”, depending on the value of my.id, the clones will go to the proper position which is corresponding to the grid’s position on the play board.

In the forever loop, each clone will update its costume and size continuously.

“Costumecheck” block

In the “costumecheck” block, the program relates the number to the costume of Left sprite. As shown in the below diagram, u represents the number in a grid, if it is equal to 64 = 2^6, through the calculation of the block, o = 6, which is the corresponding costume number 6 of Left sprite, displaying the text of 64. Done!

Bubbles sprite

The bubble sprite is used to create bubbling effect to decorate the game. It continuously creates clones and then makes clones move in a random direction. The “move” block defines the movement direction and steps. When the clone moves outside of screen, it is just deleted.

Sprite 1 – Highlight the number 2048

This sprite is used to highlight the number of 2048 by flickering three times around the 2048 number, just for the decoration purpose.

That is all for the analysis of game 2048. The key is to understand its main sprite – backdrop, and especially Replace and Change blocks. The implementation of Left sprite also deserves attention. Their implementation reflects the design mechanism of separated data and display. Hope you enjoy our analysis for the game 2048, and stay tuned for our further articles.

Most of all, 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 analyzing Others’ Project – Remake Popular Game 2048 – Part 2

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.