Learn Scratch 3.0 – Two Ways to Create Multiple Bullet Clones

When we began to learn Scratch 3.0, we usually created sprite clones at random position. However, when we improve programming skills of Scratch 3.0, we may encouter the scenarios when a sprite clone is related to another sprite’s clone.

For example, in the airplane shooting game, the planes could shoot out bullets while flying. In this case, there are multiple airplane clones, and the bullets are able to be fired from any of the airplane clones. Meanwhile, the airplanes are moving randomly on the stage. This is a typical case of bullet sprite clone relating to airplane sprite clone.

In this post, I will introduce two methods to implement the bullet clones. The demo game is a simplified version of Plant and Zombie. Pea shooters could be planted in any places on the stage. The pea could be shot out from pea shooters at a random interval. The effect is shown in below.

Method 1

In this method, Peashooter sprite and Pea sprite are connected by two lists, which represent the coordinates of the Peashooter clones.

The project code could be accessed at this location: https://scratch.mit.edu/projects/438047154/

Peashooter Sprite

There are two code segments for this sprite. In “when Green Flag clicked” segment, two lists “Peashooterlist_x” and “Peashooterlist_y” are emptied. The program will wait for the mouse being clicked down and create a Peashooter clone at mouse’s position.

In “when I start as a clone” code segment, the program adds Peashooter clone’s x and y position to “Peashooterlist_x” and “Peashooterlist_y” lists, respectively. The following part of the segment is responsible for monitoring the touch of Peashooter clones with zombie. If Peashooter clone touches the zombie, it gets deleted. Its position value should also be deleted from the two lists.

Pea Sprite

The Pea sprite will continuely check the lists “Peashooterlist_x” and “Peashooterlist_y”. If the list length is 0, it means there is not any Peashooter on the stage, then the program just waits for 0.1 seconds and checks again. when there are one or more Peashooter clones, the list length will be longer than zero. The program will traverse the two lists and then create Pea clones by refering to the item values in the lists. In this way, the Pea clone will be shot out right from the position of the Peashooter clones.

In “when I start as a clone” code segment, the created Pea clone will be responsible for moving right with each step of 5, and check if it touches zombie or edge in this process.

For this method, although Peashooter clones are where Pea clones should be shot out, the two clones are not connected directly. They “communicate” via “Peashooterlist_x” and “Peashooterlist_y”. When Preshooter clone is created or deleted, the item in the two lists should be updated in time.

Method 2

In this method, both Peashooter and Pea are combined into one single sprite called Peashooter.

The project code could be accessed at this location: https://scratch.mit.edu/projects/438059559/

In “when Green Flag clicked” code segment, the code looks almost the same as that in Method 1, except that there is no need to handle the two lists “Peashooterlist_x” and “Peashooterlist_y”.

In “when I start as a clone” code segment, it divides the code into two channels. One channel is to handle actions when costume name is Peashooter, and the other channel is when costume name is Pea. Pea Shooter and Pea clones will have different behavior when touching zombie or edges.

The key of implementing this method lies in the second “when I start as a clone” code segment. In this parallel segment, when costume name is Peashooter, it will call a block called “CreateBulletClone” at a random interval. This self-defined block gets its input parameters from the x and y position of the Peashooter clone. Within definition of this block, the code switches costume to Pea, and then creates a Pea clone at the same position of Peashooter.

Once the Pea clone is created, it will run the first parallel segment of “when I start as a clone” and enter the channel “when costume name = Pea”. The Pea clone will move to right with a step of 5 and delete itself when touching zombie or edge.

This method has been used in the airplane shooting game which I have analyzed before. Its Enimies sprite and Hosny sprite use this method to create their bullets.

Summary

In the first method, separating Peashooter and Pea makes the code structure clearer, but you need to maintain the list and keep them updated. When appling to moving Peashooter, the method could also work, but needs to alter the code to keep the lists updated. That means, the list items should always store the current positions of Peashooter clones.

In the second method, putting Peashooter and Pea in the same sprite looks confusing, but keeps the code compact. Furthermore, you do not need to maintain the lists. This method could work properly no matter the Peashooter moves or not, because Peashooter will always pass its current postion to CreateBulletClone block and Pea is created just at that moment from the same position.

Which method do you prefer? Anyway, 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 3.0 – Two Ways to Create Multiple Bullet Clones

  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 )

Google photo

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