The game is pretty cool with one obstacle, but copying – or cloning – the obstacle will make it more challenging. In this video, you will add obstacles to your game.
When the user starts your game by clicking the flag, the obstacle sprite should start cloning, or making copies of itself.
From the “control” menu, drag a “create clone of myself” block into the scripts area. Click on it. Nothing seems to happen, but when you move the sprite, you’ll see an exact copy underneath it.
Create the clone at the beginning of the game by adding a “when flag clicked” block from the “events” menu on top of the “create clone of myself” block.
Next, tell the clone what to do. It should move across the screen the same way you programmed the original obstacle to move in the previous video. Use a new event to keep the clone code separate from the original sprite's code. From the “control” menu, place a “when I start as a clone” block on top of the code you wrote in the previous video.
Test your code by clicking the flag. Great! Every time you click the flag, the obstacle sprite clones itself and moves across the stage. However, one obstacle is hardly enough to make an interesting game. It needs to keep cloning itself throughout the entire game.
Add a “forever” block around the “create clone of myself” block.
Test your code again.
Whoa! That’s a lot of obstacles. There’s no way the racer can avoid those! Fix this by adding a “wait” block to the forever loop, then test your code.
Great, now there are fewer obstacles, but the game is too predictable since obstacles appear at one-second intervals. Spice up the game by adding a “pick random” block to the “wait” block, so the obstacles appear at random times. This example will use 0.5 and 2 seconds in the blanks, but tinker with these values until you like the game.
Test your code. Notice how the original obstacle sprite is always displayed on the stage. Modify your code so that the original obstacle sprite is invisible.
To do this, add a “hide” block from the “looks” menu right after the “when flag clicked” block. Then, add a “show” block immediately after the “when I start as a clone” block. Test the code again. You don’t see the original sprite anymore! Nice.
Now to solve another problem: The obstacles move to the left side, but they never disappear.
Add a “delete this clone” block from the “control” menu to the end of that block stack to make the clones disappear when they reach the left side of the stage.
Test your code, again. Great! The obstacles appear and disappear as expected.
Lastly, change the obstacles' costumes to make the game more interesting. From the “looks” menu, add a “switch costume to” block to the beginning of the “when I start as a clone” stack. Add a “pick random” block to that block. Find out how many costumes the obstacle sprite has by clicking on the “costumes” tab. This sprite has 3 costumes, so enter the numbers 1 to 3 into the "pick random" block.
Test your code once more. Now different obstacles randomly move across the stage! That’s pretty sweet. Here’s the game plan: Randomly clone the obstacle sprite using “forever,” “wait,” “pick random,” and the clone blocks. Hide the original obstacle sprite, show the clones when they appear, and delete them when they reach the left edge.
Make random obstacles appear using the “switch costume to” and “pick random” blocks.
You can play your game now, but nothing happens when the racer hits an obstacle. In the next video, you’ll program the losing condition.
- Clona lo sprite Ostacolo per un numero di volte a caso, usando i blocchi "Per sempre", "Attendi", "Numero a caso tra" e i blocchi clone.
- Nascondi lo sprite Ostacolo originario, mostra i cloni quando compaiono e cancellali quando toccano il bordo sinistro.
- Fai comparire ostacoli a caso usando i blocchi "Passa al costume" e "Numero a caso tra".