This video will show you how to make the ball sprite bounce off the athlete sprite in yourproject. 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 inthis activity: conditionals. As you saw in the first video, conditionals make blocksrun only when certain conditions are true. In this case, the condition is that the ballsprite touches the athlete. From the “sensing” menu, place the “touching color” blockinside the “if/then” block. Click the color square, and click on the athlete's racquetor hands, depending on the sport you chose. This example clicks the orange color on theracquet. Next, tell the program what to do if the ballsprite touches the color you selected. It should make the ball bounce. You will followtwo 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’sdirection. 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 oppositedirection. If the sprite turned all the way around, itwould 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’sbouncing.
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 blankin the “point in direction” block. Type “one hundred eighty” in the first blankof the subtraction block. From the “motion” menu, place the “direction” block in thesecond blank of the subtraction block. Click the code to test it, then wait untilthe 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 thewrong direction, check the subtraction. If it’s not bouncing at all, check that theconditional is inside the loop and has the right condition. If your code doesn’t workright the first time, keep trying. Computer scientists often have to rework their codemany times to get it to work. Use courage and persistence to keep trying until you finda 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 havefun 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 othersknow 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.