Skip to content

Please update your browser

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

4. Colpisci l’atleta


This video will show you how to make the ball sprite bounce off the athlete sprite in your project. For the ball sprite to bounce off the athlete, it has to know *if* it is touching the athlete. From the “control” menu, place an “if/then” block inside the “forever” loop in the ball sprite's code.

The “if/then” block is an example of the computer science concept you’re using in this activity: conditionals. As you saw in the first video, conditionals make blocks run only when certain conditions are true. In this case, the condition is that the ball sprite touches the athlete. From the “sensing” menu, place the “touching color” block inside the “if/then” block. Click the color square, and click on the athlete's racquet or hands, depending on the sport you chose. This example clicks the orange color on the racquet. Next, tell the program what to do if the ball sprite touches the color you selected. It should make the ball bounce. You will follow two steps to build this action. First, make the ball point in the opposite direction.

From the “motion” menu, place a “point in direction” block inside the “if” block. This block will change the ball sprite’s direction. Click on the code to test it, and wait until the ball sprite hits the athlete.

It starts moving in another direction! Next, you will make the ball look like it’s bouncing.

For that to happen, the ball has to move toward the athlete, then away from it in the opposite direction. If the sprite turned all the way around, it would turn a full circle, or three hundred sixty degrees. So, it has to turn halfway, or one hundred eighty degrees, to point in the opposite direction and look like it’s bouncing.

To make that happen, subtract the sprite’s current direction from one hundred eighty.

To do this, from the “operators” menu, place the “subtraction” block in the blank in the “point in direction” block. Type “one hundred eighty” in the first blank of the subtraction block. From the “motion” menu, place the “direction” block in the second blank of the subtraction block. Click the code to test it, then wait until the ball sprite hits the athlete sprite. Does it bounce back up in the opposite direction?

If so, great, you did it! If not, look at your code again. If the ball bounces in the wrong direction, check the subtraction. If it’s not bouncing at all, check that the conditional is inside the loop and has the right condition. If your code doesn’t work right the first time, keep trying. Computer scientists often have to rework their code many times to get it to work. Use courage and persistence to keep trying until you find a solution that works. If you need help, ask a neighbor. If you can’t figure it out together, ask a host or guru for help. That’s all you need! Test it out, and have fun playing. Then, move on to the add-on page to add some cool features to your project.

Go to the project page, and give the project a creative title and add instructions so others know how to use your creation. Here’s the game plan: Add an “if” block to the “forever” loop.

Place the “touching color” block as the condition.

Use “point in direction” to change the way the ball sprite moves.

Use subtraction to figure out the new angle and get the ball sprite moving in the right direction.

arrow_backward Indietro
Avanti arrow_forward
  1. Aggiungi un blocco "Se" dentro il loop "Per sempre".
  2. Inserisci nella casella della condizione il blocco "Sta toccando il colore".
  3. Usa "Punta in direzione di" per cambiare la direzione dello sprite Palla.
  4. Usa "sottrazione" per stabilire il nuovo angolo e far muovere lo sprite Palla nella direzione giusta.
  5. Aggiungi un blocco "Quando si clicca sulla bandiera".