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

Transcript

In this video, you will program the baseball to pitch towards home plate.

Right now, 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 edges of the screen, click the control menu and drag out a repeat until block.

Place the move ten 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 to the repeat until loop.

Then, select edge from the drop down.

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 and direction block and place it above the loop.

Click the drop down 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 pitchers mound by using the go to xy block from the motion menu.

But before you drag out the block, drag the baseball to it's starting spot.

The values in the go to xy block will automatically change to the ball's location.

Once you've placed the ball in it's starting position, place the go to xy block above the point and direction block.

Cool.

For this game, the pitcher will pitch five times.

Place a repeat block around the pitching code, then change the 10 in the value space to a five.

Click the code.

The pitcher pitches five times, but the pitches are a little too close together.

Separate them by placing a wait block between the go to and the point and 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 same spot.

So make the ball point in a random direction that's still towards the hitter.

From the operator's menu, place a pick random block inside the point and direction block.

Try entering values into the pick random block between 160 and say 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.

Alright, here's the game plan.

Make the baseball move towards home plate using the repeat until, move, and touching edges block.

Point the baseball in a random direction using point indirection, and random blocks.

Start the baseball in the same position before each pitch, using the go to xy block.

Pitch five times using repeat and wait.

Make the baseball start moving when the flag is clicked.

The next video we'll program the bat to hit the ball outta the park.

arrow_backward Back
Next arrow_forward
Instructions
  1. Make the baseball move towards home plate using the "repeat until," "move," and "touching edge" blocks.
  2. Point the baseball in a random direction using "point in direction" and "random" blocks.
  3. Start the baseball in the same position before each pitch using a "go to x y" block.
  4. Pitch 5 times using "repeat" and "wait."
  5. Make the baseball start moving when the green flag is clicked.