3. In the Strikezone
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 the “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.” Finally, click the bat sprite again, and the code will automatically change the costume back.
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 the 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 0. Click the flag to test the game. Try missing the baseball on purpose. The baseball goes past the bat and to the edge of the screen before returning to the pitcher’s mitt. Next, hit the baseball. If the batter swings the bat at the right time, the baseball moves away from home plate. Nice work!
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.