In this video, you will program the receiver sprites to move around, then stop once it “catches” the ball sprite. Click on the receiver sprite. Start by making the receiver sprite move around the whole stage. From the “motion” menu, drag out a “move” block. To make the receiver sprite keep moving for the entire game, put that block inside a forever loop. Go ahead and click the code. The receiver keeps moving, but quickly runs off the stage. To keep that from happening, go to the “motion” menu, find the “if on edge, bounce” block, and place it inside the forever loop. Click the stack again. Much better! Right now, the receiver sprite just moves left and right. To make the receiver sprite *choose* a direction in which to move, go to the “motion” menu, and find the “point in direction” block. Add it to the top of the block stack. To make it choose a random direction, go to the operators menu, find the “pick random” block, and place it in the value blank of the “point in direction” block. The numbers you enter in this block will determine the receiver’s direction.
To make it go in any possible direction, put 0 and 360 in the “pick random” block.
Click the code, and watch where the sprite moves. Click the stop sign. Then, click the code again, and watch it move in a new direction. The receiver can start moving in any direction now! Awesome! The receiver needs to catch the ball. To do that, it needs to sense *if* it is touching the ball sprite and react as soon as it is.
From the "control" menu, drag out an “if” block, and place it inside the forever loop.
From the sensing menu, place a “touching” block inside the if. Click the dropdown, and select “ball sprite.” The code will keep checking over and over if the receiver is touching the ball sprite. Next, tell the receiver what to do when it catches the ball. It will change costumes to the one that has received the ball. From the “looks” menu, place a “switch costume to” block inside the “if” block. From the dropdown, select the “received” costume. It doesn’t need to keep running around once it has caught the ball, so make it wait a short bit then get out of the way. To make it wait, from the “control” menu, place a “wait” block under the “switch costume to” block. To make it disappear, from the “looks” menu, place the “hide” block under the “wait” block. Tinker with the “wait” block’s value until the sprites disappear at a time that makes sense to you. If the receiver hides, it also needs to show.
Place a “show” block at the top of the block stack, so that the receiver is always visible when it’s running around, then it hides when it gets the ball or the puck.
Finally, make this code start when the flag is clicked by placing a “when flag clicked” block on top of the code stack. Test the code by clicking the flag.
The receiver sprite should point in a random direction, move, and bounce when it touches the edge of the screen. Aim the ball sprite with the arrows, and press the spacebar when you’re ready to pass to the receiver. If the ball sprite hits the receiver sprite, the receiver sprite should stop moving. Keep trying until you see this code work.
Wait a second. The receiver sprite never returns to its first costume after it changes. To make that happen, from the “looks” menu, place a “switch costume to” block just under the “when flag clicked” block. Select the “moving” costume. Click the flag again.
The sprite starts by looking like it’s running, then looks like it catches the ball sprite.
Here’s the game plan: Program the receiver sprite to run around the stage using the “move,” “if on edge, bounce,” and “forever” blocks.
Program the receiver to change costume if it catches the ball using “wait,” “show,” “hide,” and “switch costume to” blocks. Make it start moving in a random direction using the “point in direction” and “pick random” blocks.
Program the receiver to change costumes and stop when it catches the ball using the blocks shown.
- Programme le lutin Receveur pour courir sur la scène à l’aide des blocs "Avancer de", "Rebondir si le bord est atteint" et "Répéter indéfiniment".
- Programme le lutin Receveur pour qu’il commence à se déplacer dans une direction aléatoire à l’aide des blocs "S’orienter à" et "Nombre aléatoire".
- En utilisant les blocs "Si", "Touché", "Basculer sur costume" et "Stop ce script", programme le lutin Receveur pour qu'il change de costume et s'arrête lorsqu'il attrape la balle.