Learning Scratch by Analyzing Others Project – A Fighting Game – Part 2

In the previous post, I analyzed “Player” sprite, and now I will switch to the most complicated sprite in this project – “Logic” sprite. So get ready for it!

When I receive game (main code segment)

When the player clicks Green Start button, “When Start clicked” event will “broadcast game” message, and “When I receive game” code segment will begin working. It is the main code segment of the “Logic” sprite, and also the core of the whole game.

As shown in the above picture, it calls for several other key blocks. First, it calls “Initiate Projectiles” block, which is used to initiate (clear) projectile related lists. Projectile is a sprite to enhance the fighting effects by adding sparks, black lines and some other comics like elements. After that, it calls “Clone” to clear most of lists and variables used by the game. “Clone” is not an appropriate block name here, which is better to be replaced with names such as “Reset” or “ClearData”.

Next, the main code segment will begin forever loop. It “resets effect queue” first. The effect sprite is similar to projectiles sprite for enhancing the fighting effect. It then calls “Inputs” block which will check key pressing events. The game will check the status of 12 keys representing the actions of left and right movement, jumping, crouching, punches and kicks for each player (6 actions * 2 players = 12 keyboard pressing events) and record those key pressing into “Inputs” list.

The next step, the main code segment will call “Playerticks” block. This block is responsible for updating players’ gestures. Since each player’s gesture is influenced not only by the game player’s operation, but also by whether it is hit by the other player. The gesture change is more complicated than what we think. This is another key code segment. I will introduce it more in detail in this post.

After calling “Playerticks” block, the main code segment will adjust scroll X, Rumble X and Rumble Y. those variables will be used when calculating the X and Y positions of players in the next iteration.

“Playerticks” Block Definition

As I mentioned, “Playerticks” is another important code segment. The name “ticks” represents that the status update of player happens in a tick. As computer runs very fast, it is very reasonable that those code segments might have run a decade rounds in a tick.

The “Playerticks” block will repeat twice to update player status. Again, Do not need to worry that it keeps updating one player with priority to the other because computer runs very fast. It first calls “Hurtbox” block which draws three rectangles surrounding player’s silhouette.

After that, it calls “Detect hits” block to compare hurtbox position with that of hitbox. If part of hurtbox (one player) overlaps with hitbox (the other player), it means that hitting occurs, the player to be hit needs to update its status, including its State, X and Y position, speed at X or Y direction, etc.

Finally, the “Playerticks” block will update player’s gesture by calling “Ticks (p)” block, which will choose a proper costume for player based on their state and other parameters. It is a bit early to explain “Ticks(p)” too detail at this moment and I will leave it to the next post for further explanation.

To reduce information overloading, I will analyze “Hurtbox” in this post.

“Hurtbox(p)” Block Definition

“Hurtbox(p)” Block set values to a set of variables from the corresponding lists, but does not use those variables first. Those variables will be used to draw hurtbox.

After that, the code clears all the lists of “hurtbox X”, “hurtbox Y”, “hurtbox x size” and “hurtbox y size”. As shown in the below diagram, the player’s silhouette is divided into three rectangles and their position and size are recorded in the four lists. The author chooses to record three rectangles instead of one, so that the hitting can be more accurately measured.

As I mentioned in the first post, each player has 18 states, such as idle, jumping, kick, low kick, crouching, air flying, etc. “Hurtbox (p)” block adds hurtbox rectangles based on the player’s state. The result is an accurate set of rectangles attaching to each player.

“add hurtbox” Block Definition

This block is simply called by “Hurtbox (p)” block to draw rectangle.

Till now, I analyzed Player sprite and part of “Logic” sprite. “Logic” is a really big sprite which contains several cascaded blocks. In the next post, I will analyze “Detect Hits” block and “Ticks(p)” block, so that we can have an overall picture of how the game runs. Stay tuned and enjoy the coding!

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 “Learning Scratch by Analyzing Others Project – A Fighting Game – Part 2

  1. Pingback: Learning Scratch By Analyzing Others Project – A Fighting 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 )

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.