Skip to content

Please update your browser

Your browser version is not supported by this site. CS First may not work properly. Learn more

2. Die Verfolgungsjagd hat begonnen!


Before moving on to 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’ll learn how to program the chaser sprite to start moving in a random direction and bounce off the walls forever. When computer scientists like you 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 the sprite point in a random direction and bounce off the walls forever, you'll break this problem into smaller steps: 1) Move the sprite 2) Move the sprite forever 3) Make the sprite bounce off it’s on an edge 4) Point the sprite in a random direction when it starts First, 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 the stage now. This wouldn’t be a very fun game if the sprite 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 turn 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 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” block to whatever you want. Each time this block is run, the computer will choose a random number 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 time in computer science! Keep coding, testing, and trying solutions until you find one that works. Now, it’s your turn. Program your chaser sprite to point in a random direction, move forward forever, and bounce off the edges of the stage. You might break down the problem into these steps: Move forward Move forward forever Bounce, if on edge Turn a random direction at the beginning of the game Finally, use a when flag clicked event to start the game.

arrow_backward Zurück
Weiter arrow_forward
  1. Bewegt die Verfolger-Figur.
  2. Lasst die Verfolger-Figur sich fortlaufend bewegen.
  3. Lasst die Verfolger-Figur abprallen, wenn sie den Rand berührt.
  4. Lenkt die Verfolger-Figur in eine zufällige Richtung, wenn das Spiel beginnt.
  5. Lasst die Verfolger-Figur sich bewegen, wenn die grüne Flagge angeklickt wird.