Learn Scratch by Reading and Analyzing Others’ Project – Airplane Shooting Game Part 2

In the last post, I analyzed three cores sprites of the game – Player sprite, Bullet sprite and Enemies sprite. In this post, I will analyze another core sprite – Hosny, which represents the ultimate evil power. After that, I will go to analyze several auxiliary sprites.

Hosny Sprite

Hosny sprite represents the ultimate evil monster. In “when I receive Game Over” and “when Green Flag clicked”, the code is very simple, just to hide the sprite or delete its clone at the beginning and end of the game.

The message “Dragon Hut” is broadcast from Bullet sprite when the latter shoots into Hosny. After receiving the message, the Hosny sprite will play a sound from three random choices.

“when I receive Boss” Code Segments

There are 5 parallel code segments of “when I receive Boss”. Each of them implements different functions. This message is broadcast by Enemies sprite when two waves of enemy airplane attacks finish.

The first code segment is mainly responsible for judging the health status of the Hosny sprite. Horey is always pointing toward Player sprite. Its x position always keeps at 200, close to the right edge of the stage. When “Hosny Health” variable is less than 1, which means that Hosny is going to be destroyed. The program will generate trembling effect for the Hosny by adding random noise value to its x and y positions. I would say the effect is amazing.

#1 parallel code segment

I mentioned above that Hosny’s x position is kept at 200, so how about Hosny’s y position? As you could see in the second parallel code segment, Hosny sprite will glide to a y position randomly within 2 to 5 seconds and then repeat this process.

In the third parallel segment, the code will be responsible for changing the costume of Hosny. There are 7 constumes, and each of them just has minor difference in the gesture. The author puts the costume change in a separate segment, so it will not interfere with other code’s execution.

#2 and #3 parallel code segments

The fourth segment is responsible for changing brightness of Hosny when it is hit by Bullet sprite clones.

The last parallel segment is responsible for creating clones of Hosny sprite. You might wonder why the program needs to create clones of Hosny sprite. After all, there is always one Hosny sprite showing on the stage.

Here the idea is the same as that in Enemies sprite. Enemies sprite contains 3 costumes. Two of them are showing Regular and Purple aircraft images, while the third one is a bullet image. Hosny sprite has 9 costumes. Among them, 7 costumes belong to Hosny image the monster, while the left two costumes (costume 8 and 9) are showing the fireball images. When the program creates clone in #5 segment, it is creating fireballs.

#4 and #5 parallel code segments

You might wonder whether it is necessary to have five parallel code segments of “when I receive Boss”. I would say it is not overused in this scenario. For example, the #2 parallel segment will always make the Hosny glide. Since Glide block will keep the program wait till it finishes, if putting Glide block with others within one segment, the program could not change costume or respond to the shooting of bullet, etc. The same reason, the #3 parallel segment will switch the costume of Hosry at the frquency of 0.04 seconds. If merging this code segment with others, Hosry sprite could not do anything else but just switch its costumes.

“when I start as a clone” Code Segments

This code segment is responsible for defing the behavior of Hosny fireball clones. At the beginning of the code segment, it switches costume to “fireball2”, so that what you see on the stage is the fireball, not another Hosny monster image. The fireball will have the same x position, y position and direction as Hosny sprite. It bursts off straight and moves forward with vast speed until touching edge. Depending on if “invisible” variable is equal to true, it has different behavior. Generally, the whole idea is the same as Enemies bullet clones, so I will not detail them here.

The second parallel code segment of “when I start as a clone” will switch between two costumes of fileball clones to make it look more vivid.

Pen Sprite

If you are wondering how to show the health value of Player sprite and Hosny sprite dynamically, here is the answer. The function is implemented by Pen sprite.

In the code segment “when I receive Game”, it will draw two lines. The white line and green line are drawn from the same starting position. starting x is equal to “Player X – 30”, and starting y is equal to “y position of Player + 50”. Therefore, the two lines will always move together with the Player sprite and hover over it. The white line has a fixed length, while green line’s length is proportional to the health value of Player sprite. The final effect is that you see a health bar which indicates Player sprite’s health status dynamically.

The display of health value for Hosny is similar. Only the position and size of the health bar have been changed.

“Draw Line” block used in the above code segments is easy to be understood. It just wraps the starting and ending coordinates and size value of a line. Please note that white line is usually a bit wider than red and green lines, so that you could feel like the green or red “blood” filled in the white tube.

The other three code segments do some cleanup stuff at the beginning and end of the game. I will not explain them further.

Clouds Sprite

The Clouds sprite will generate a moving backdrop. The real backdrop is just a blue static image. It is the existance of clouds that gives the airplanes a rapid moving context. In “when Green Flag clicked” code segment, the program creates multiple cloud clones with a random interval. In “when I start as a clone” segment, the code sets multiple parameters for those cloud clones. Each clone’s costume, direction, size, ghost effect, moving speed, and layer is set to a random value, bringing more variance to the background setting. If you observe the game carefully, you will notice that sometimes the airplanes wave their way through those cloud clones.

That is all for the introduction of Hosny, Clouds and Pen sprites. We still get two more sprites to be introduced before wrapping up the whole game. Stay tuned to our blog and do not 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.

One thought on “Learn Scratch by Reading and Analyzing Others’ Project – Airplane Shooting Game Part 2

  1. Pingback: Learn Scratch by Reading and Analyzing Others’ Project – Tower Defense – The Coding Fun

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.