Skip to content

Please update your browser

Your browser version is not supported by this site. CS First may not work properly. Learn more

2. Throw the Baseball


In this video, you will program the baseball to pitch towards home plate. The bat is already programmed 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 the screen, click the control menu, and drag out the “repeat until” block. Place the “move 10 steps” block inside it. The baseball should stop moving when it touches the edge of 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. The baseball 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 go back and start from the pitcher’s mound by using the “go to x y” block from the motion 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” block above the “point in direction” block. Cool.

For this game, the pitcher will pitch 5 times. Place a “repeat” block around the pitching code, 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 number of 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 make the 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 entering values into the “pick random” block between 160 and 200. Tinker with these values, and see where the ball goes. Finally, place a “when flag clicked” block at 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 flag is clicked. The next video will program the bat to hit the ball out of the park.

arrow_backward Retour
Suivant arrow_forward
  1. Fais se déplacer la balle vers les buts à l’aide des blocs "Répéter jusqu’à", "avancer de" et "Bord touché".
  2. Oriente la balle dans une direction aléatoire à l’aide des blocs "S’orienter à" et "Nombre aléatoire".
  3. Démarre la balle dans la même direction avant chaque lancer à l’aide d’un bloc "Aller à x y".
  4. Lance la balle 5 fois à l’aide de "Répéter" et "Attendre".
  5. Fais partir la balle lorsque le drapeau est cliqué.