image Computational Thinking - Three Aliens Coming Down at a Time

So far we have been able to move a set of 10 rabbits together, with two different approaches in two different tutorials (one with Boolean attribute and another with variable attribute).

In this tutorial, we want to move 3 aliens down at a time. Suppose there are 30 aliens placed in the top half of the canvas. How could we somehow order three of them to come down at a time? Recall that each instance of alien (or any character) has an associated index. Perhaps we can make use of this variable to our advantage!

Before we can do that, we need to determine the timing of moving the aliens. Recall that in an earlier lesson, we had used a moving timer. Here, we can use the same trick! Let us use the diamond to serve as the moving time. And we will use the variable x1 for the diamond to compare with the index of the aliens.

There are 36 aliens and a diamond.
The diamond is invisible.

The speed of the diamond is 8.
The diamond starts by moving to the right.
When the diamond touches a border, it reverses.
When the diamond touches a border, the x1 of the diamond increases by 3.

Here in the above text for the moving timer, we see that each time the diamond touches a border, the variable attribute x1 for the diamond will increase by 3. Why 3? Well, that is because we want to move 3 aliens down at a time. If you had wanted to move 4 aliens, you'd change this number from 3 to 4!

Now the magic happens.

When the index of the alien is less than the x1 of the diamond, the alien becomes happy.
When an alien is happy, it moves down.

Try it out and see if the aliens are moving down 3 at a time! You can also add the following sentence so that you see the index of the aliens to determine which ones will be the next ones coming down!

The index of the alien is displayed.

Now suppose we want to move the aliens down in the reverse order. We simply need to modify a few lines. The final complete game plan for this example is shown below:

There are 36 aliens aligned 3 by 12 at the top and a diamond.
The x1 of the diamond is initially 35.
The diamond is invisible.

The speed of the diamond is 8.
The diamond starts by moving to the right.
When the diamond touches a border, it reverses.
When the diamond touches a border, the x1 of the diamond decreases by 3.

When the index of the alien is greater than the x1 of the diamond, the alien becomes happy.
When an alien is happy, it moves down.


Try it yourself

Can you make 2 aliens move to the right at a time?


Next page

Tutorials Home