Leçon 1,
Chapitre 1
En cours
Jeu Tire sur la chauve souris – Etape 2 : Birth, Death, and the In Between: The Magic of Randomness
Here comes the fun part: the randomness. The bat must appear randomly anywhere in ‘space’, literally, and must move around randomly, till the time either you successfully aim at it or till a sufficient amount of time has passed. After that time, provided you haven’t been able to aim at it, it must disappear. Once the bat disappears, a new one must appear randomly from anywhere. Let’s write the script to bring our idea into reality.
- Begin the script with the when () key pressed block. Select the same key that you did in the previous script. Why? Because it’s we want everything to happen simultaneously.
- Now, we want the bat to appear randomly in space. For that, it must first disappear/hide! And as you can see, the bat is STILL there! So, we need to first hide it. For that, go to the Looks palette, and drag and drop the hide block below the when () key pressed block.
- Next, drag and drop the forever block. Now, we want the bat to appear randomly from anywhere. For that, drag and drop the show block inside the forever block.
- Below the show block, drag and drop the set size to () % block, and set the size to 0; we want the bat to appear, and increase gradually in size.
- Next, drag and drop the go to x: () y: () block below the set size to () % block; you’ll find this block in the Motion palette. Now, to make the bat appear randomly, the x and y coordinates must be randomly chosen. For that, there’s a block in the Operators palette: the pick random () to () block. But how do we decide the range? It’s quite easy actually, provided you paid full attention at the beginning of the course! In your very first lesson, you used the xy-grid backdrop. The range for both the X-coordinates and Y-coordinates was given in it. Go back and look carefully this time; you’ll notice that the range on the X-axis is -220 to 220, and on the Y-axis is -160 to 160. These will be our ranges. Therefore, for x, in the pick random () to () block, write -220 and 220. Then drag and drop this pick random () to () block inside the space next to x in the go to x: () y: () block. Similarly, for y, write -160 and 160 in another pick random () to () block, and drag and drop this block inside the space next to y in the go to x: () y: () block.Now, we want the bat to gradually increase in size, then move to any other random point in space, and then disappear if the player is not able to aim at it. For that we’re going to use the repeat () block, the wait () seconds block, and the change size by () block.
- Drag and drop the repeat () block below the go to x: () y: () block, and inside the space write 5. Next, inside the repeat () block, drag and drop the wait () secs block; inside the space write 0.05. Finally, drag and drop the change size by () block below the wait () secs block, and its space write 10. Why these three? When the bat appears randomly at a point for the first time, it must gradually increase in size; the wait () secs block instructs the script to wait for 0.05s, and then increase the bat’s size by 10. And this is repeated five times, till the bat reaches its full size.
- The next step to make the bat move to another random point; we’re going to make it glide to the point, instead of simply teleporting to that point; you can check this for your self by using the go to x: () y: () block; you’ll observe that the bat kind of teleports another point. To make it glide, we’ll use the glide () secs to x: () y: () block; drag and drop it below the repeat () block. Here as well, we’re going to use the pick random () to () block to select the random point and the range that we used before.
- Now comes the time to make the bat disappear; it is as easy as making it appear. The blocks to be used are the same; the only difference will be while changing the size. But first, let’s put all the blocks in their place; drag and drop the repeat () block, the wait () secs block, and the change size by () block in the same way that we did before. Next, inside the change size by () block, instead of writing 10, write -10. The minus sign signifies that the size is reducing. You might now ask: “If we’re using the minus sign here to specify that the size is reducing, why didn’t we use the plus sign to specify that the size was increasing?” Well, this is because we don’t need to write a sign to specify that the size is increasing; if there is no sign, it by default means that the change is positive, i.e. the size is increasing.
Below this repeat block, drag and drop the hide block to finally make the bat disappear. With this, our script is complete!
