Before moving on to this video, you should have added a backdrop and two sprites to yourproject. If you haven’t done that yet, please pause this video and do so now.
In this video, you’ll learn how to program the chaser sprite to start moving in a randomdirection and bounce off the walls forever. When computer scientists like you have largeor difficult problems to solve, they use something called problem decomposition to make solvingthe problem easier. Problem decomposition means breaking a bigger problem up into smallersteps. To make the sprite point in a random directionand bounce off the walls forever, you'll break this problem into smaller steps:1) Move the sprite 2) Move the sprite forever3) Make the sprite bounce off it’s on an edge4) Point the sprite in a random direction when it startsFirst, move the chaser sprite. Click the chaser sprite, and drag out a “move”block. Next, program the sprite to keep moving forward.
Click control, and place a forever block around the “move” block.
Click the blockstack to test. Awesome! The sprite keeps moving forward.
Next, program the sprite to bounce off the edges of the stage.
Click motion, and drag the “if on edge bounce block” into your forever loop.
Try it by clicking on the forever loop. Gooooood, the sprite is bouncing back and forth on thestage now. This wouldn’t be a very fun game if thesprite only bounced back and forth like this, though.
To fix this, turn the sprite a random amount of degrees at the beginning of the game.
This is a problem that you can break down into smaller pieces. To get a sprite to turna random amount, you’ll need it to turn first.
Drag out a “turn” block from the motion menu, and put it at the start of the program.
Each time the game runs now, the sprite will turn 15 degrees before starting to move forward.
From the operators menu, select a “pick random” block, and place it inside the “turn”block. Set the values in the “pick random” blockto whatever you want. Each time this block is run, the computer will choose a randomnumber between the values you selected, and turn the sprite that amount.
Test your code. Sweet! Now, tell the computer when to run this code.
Add a “when flag clicked” event.
Don’t worry if you tested a solution, and it didn’t work. That happens all the timein computer science! Keep coding, testing, and trying solutions until you find one thatworks. Now, it’s your turn. Program your chasersprite to point in a random direction, move forward forever, and bounce off the edgesof the stage. You might break down the problem into thesesteps: Move forwardMove forward forever Bounce, if on edgeTurn a random direction at the beginning of the gameFinally, use a when flag clicked event to start the game.