Skip to content
Transcript

Before moving onto this video, you should have added a backdrop and two sprites to your project.

If you haven't done that yet, please pause this video and do so now.

In this video you will learn how to program the chaser sprite to start moving in a random direction and bounce off the walls forever.

When computer scientists have large or difficult problems to solve, they use something called problem decomposition to make solving the problem easier.

Problem decomposition means breaking a bigger problem up into smaller steps.

To make this sprite point in a random direction and bounce off the walls forever, you'll break this problem into smaller steps.

One, move the sprite.

Two, move the sprite forever.

Three, make the sprite bounce off, if it's on an edge.

And four, point the sprite in a random direction when it starts.

First, move the chaser sprite.

Click the chaser sprite and drag on a move block.

Next, program the sprite to keep moving forward.

Click control and place a forever block around the move block.

Click the block stack 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.

Good, the sprite is bouncing back and forth on the stage.

The wouldn't be a very fun game if the sprite only bounced back and forth like this.

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 move a 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 down, the sprite will turn 15 degrees before starting to move forward.

From the operator's menu, select a pick random block and place it inside the turn block.

Set the values in the pick random block to whatever you want.

Each time this block is read, the computer will choose a random number between the values you selected and turn that sprite that amount.

Test your code.

Sweet.

Now tell the computer when to run this code.

Add a when flag clicked event.

Now it's your turn.

Program your chaser sprite to point in a random direction, move forward forever, and bounce of the edges of stage.

You might break down the problem into these steps.

Move forward, move forward forever, bounce, if on edge, and turn a random direction at the beginning of the game.

Finally, use a when flag clicked event to start the game.

Instructions

  1. Move the chaser sprite.
  2. Make the chaser sprite move forever.
  3. Get the chaser sprite to bounce if it is on an edge.
  4. Point the chaser sprite in a random direction when it starts.
  5. Make the chaser sprite start moving when the green flag is clicked.