Now that the baseball
moves towards the batter, it's time to program the ball
to sense and react when the batter hits it.
When the batter clicks the mouse, the bat turns red.
You'll program the ball to detect
that it hits a swinging bat if it touches the red bat.
From the Control menu, drag out an if-then block.
From the Sensing menu, drag out a touching color block
and place it in if portion of the if-then block.
Set the touching color block to red.
First change the bat costume to the red bat.
Then select the baseball sprite again.
Click the color square and click the red bat
to change the condition to red.
Alright, back to programming the ball.
Place the if-then statement inside the repeat until loop
under the move 10 steps block.
Finally, tell the ball what to do
if it is touching the red bat.
When a baseball hits a bat,
it goes in the opposite direction.
The repeat loop makes a ball keep moving
in the same direction until it hits the edge of the screen.
To make the baseball move away
from home plate, change its direction.
Place a point in direction block inside the if-then block.
Then click the dropdown and select Up or zero.
Click the flag to test the game.
Try missing the baseball on purpose.
The baseball goes past the bat into the edge of the screen
before returning to the pitcher's mitt.
Next, hit the baseball.
If the batter swings a bat at the right time,
the baseball moves away from home plate.
Alright, here's the game plan.
Program the ball to sense if it is touching the bat
after the batter swings using if-then,
touching color, and point in direction blocks.
In the next video, you'll use a variable
to keep track of the number of pitches the batter hits.
Sign in to track your progress and earn badges