5. Baseball Average Calculator
Now that you have a way to count hits, you can calculate the hitter’s batting average.
In this video, you’ll learn how to reveal the batter’s average at the end of the game.
Over time, this information will show the player’s growth and development as a hitter and player.< To start, click on the coach sprite.
In baseball, a player’s batting average is the number of hits divided by the number of times up at bat. For this game, the batting average will be the number of hits divided by the number of pitches. From the "operators" menu, drag out the “division” block. From the "data" menu, place “hits” in the first blank of the division block. Type 5 – or the number of pitches you chose for your game – in the second blank of the “division” block.
Click on the division block. It shows the value of hits, in this case 2, divided by the number of pitches, which is 5. 2 divided by 5 is 0.4. Nice.
To make the coach say this value, go to the “looks” menu and drag out a “say for 2 seconds” block, then drag your division block into the say block.
Click on the block. The coach says 0.4. Place another “say” block above this one that explains the number. This example says, “Your batting average is.”
Finally, the coach needs to know when all the pitches have been thrown before saying the batting average. Click on the baseball. From the “events” menu, drag out a “broadcast” block, and place it at the end of the "repeat" loop.
To name the message something that makes sense, click on “new message,” then type in a new name. This example uses “calculate.” Click the coach. From the "events" menu, place a “when I receive” block on top of the stack that makes the coach say the batting average. Click the flag to test your game. Try it a few times. What the coach says should change depending on how many times you hit the ball.
If you score zero hits out of five, for example, the coach says “your batting average is zero.” If you hit the ball three times, the batting average is point six.
Here's the game plan: Calculate the batter’s batting average using a “division” block and the “hits” variable.
Make the coach say the batting average. And finally, let the coach know when to say the value using “broadcast” and “when I receive” blocks.
Once you complete these steps, you're done with the core project! Move onto the next page to add some sweet add-ons to the batter up project.