Please update your browser
Your browser version is not supported by this site. CS First may not work properly. Learn more.
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 racket or hands,
depending on the sport you chose.
This example clicks the orange color on the racket.
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
towards the athlete, then away from it
in the opposite direction.
If the sprite turned all the way around,
it would be in a full circle, or 360 degrees.
So it has to only turn halfway, or 180 degrees,
to point in the opposite direction
and look like it's bouncing.
To make this happen, subtract the sprite's
current direction from 180.
To do this, from the Operators menu,
place the subtraction block in the blank
in the point in direction block.
Type 180 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.
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 Projects page,
and give your 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.
And finally, add a when flag clicked block to the top.
Sign in to track your progress and earn badges