In this video, you will program the baseball to pitch towards home plate. The bat is alreadyprogrammed to follow your mouse pointer, and it will swing when you click the mouse.
Click on the baseball. To make it *repeat* moving *until* it touches the edge of thescreen, click the control menu, and drag out the “repeat until” block. Place the “move10 steps” block inside it. The baseball should stop moving when it touches the edgeof the screen. From the “sensing” menu, place the “touching” condition into the“repeat until” loop, then select "edge" from the dropdown. Click on the code. Thebaseball moves, but it moves to the right instead of down, towards home plate.
To make the baseball point towards home plate before the pitch, from the “motion” menu,choose the “point in direction” block, and place it above the loop. Click the dropdown,and select “down” or 180. Click on the code to test it.
Nothing seems to happen. The ball is already at the edge of the screen. Make the ball goback and start from the pitcher’s mound by using the “go to x y” block from themotion menu. But, before you drag out the block, drag the baseball to its starting spot.
The values in the “go to x y” block will automatically change to the ball’s location.
Once you've placed the ball at its starting position, place the “go to x y” blockabove the “point in direction” block. Cool.
For this game, the pitcher will pitch 5 times. Place a “repeat” block around the pitchingcode, then change the 10 in the value space to a 5.
Click the code. The pitcher pitches 5 times, but the pitches are a little too close together.
Separate them by placing a “wait” block between the “go to” and “point in direction”blocks. Test by clicking the code. Much better! Tinker with the values to change the numberof pitches or the length between them for your game.
The game is too easy if the pitcher always throws the ball to the same spot, so makethe ball point in a random direction that’s still towards the hitter. From the “operators”menu, place a “pick random” block inside the “point in direction” block. Try enteringvalues into the “pick random” block between 160 and 200. Tinker with these values, andsee where the ball goes. Finally, place a “when flag clicked” blockat the top of the pitching code stack. Here's the game plan:Make the baseball move towards home plate using the “repeat until,” “move” and“touching edge” blocks. Point the baseball in a random direction using“point in direction” and “random” blocks.
Start the baseball in the same position before each pitch using a “go to x y” block.
Pitch 5 times using “repeat" and “wait.” Make the baseball start moving when the flagis clicked. The next video will program the bat to hitthe ball out of the park.