The game is pretty cool with one obstacle, but copying – or cloning – the obstaclewill 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 startcloning, or making copies of itself.
From the “control” menu, drag a “create clone of myself” block into the scriptsarea. Click on it. Nothing seems to happen, but when you move the sprite, you’ll seean exact copy underneath it.
Create the clone at the beginning of the game by adding a “when flag clicked” blockfrom 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 programmedthe original obstacle to move in the previous video. Use a new event to keep the clone codeseparate from the original sprite's code. From the “control” menu, place a “whenI 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 obstaclesprite clones itself and moves across the stage. However, one obstacle is hardly enoughto 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 thisby 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 obstaclesappear at one-second intervals. Spice up the game by adding a “pick random” block tothe “wait” block, so the obstacles appear at random times. This example will use 0.5and 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. Modifyyour code so that the original obstacle sprite is invisible.
To do this, add a “hide” block from the “looks” menu right after the “when flagclicked” block. Then, add a “show” block immediately after the “when I start as aclone” 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 blockstack 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 asa clone” stack. Add a “pick random” block to that block. Find out how many costumesthe 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 prettysweet. Here’s the game plan:Randomly clone the obstacle sprite using “forever,” “wait,” “pick random,” and the cloneblocks. Hide the original obstacle sprite, show theclones 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 nextvideo, you’ll program the losing condition.