Skip to content

Please update your browser

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

arrow_back High Score

Transcript

In this add-on, you will keep track of the highest batting average.

In the core project, you only used the batting average once. For this project, you will use it many times. Rather than recalculating the batting average each time you use it, keep track of it in a variable. Computer scientists like you use variables to keep track of data so they can use it later in their programs. From the “data” menu, click “make a variable.” Give it a name like “batting average,” then click “OK.”

Click the coach. From the “data” menu, drag out a “set batting average to” block.

Place it under the “when I receive” block that starts the calculation.

Drag the “hits divided by 5” block out of the “say” block, and place it inside the white box next to the “to.” From the “data” menu, drag the “batting average” block and place it inside the second say block. Test the code. Click the flag, and play the game. After five pitches, the coach will say the batting average.

Next, program the coach to check if the batting average is better than the previously recorded batting averages. To do this, create another variable. Computer scientists use variables to keep track of multiple changing data points, like a high score. From the data menu, click “make a variable.” Give this a name that makes sense, like “high score.” Next, calculate whether the current batting average is the best. If it is higher than the previous batting average, it should be the new highest. From the “control” menu, drag out an “if” block and place it under the block that says the batting average. From the “operators” menu, place a “greater than” block inside the “if” block. From the "data" menu, place the “batting average” variable on the left side of the “greater than” operator. Place “high score” on the other side. Now, the “if” statement reads: “if batting average is greater than high score.” Next, fill in what will happen if the batting average is greater than the high score. If the batting average is higher than the high score, the variable should record it as the new high score.

Place a “set variable to” block inside the “if” statement. Click the dropdown to make sure you are setting the “high score” variable.

Set the high score to the batting average. From the “data” menu, drag the “batting average” block into the white square in the “set high score to” block.

Finally, make the coach say the high score just like it says the average. From the “looks” menu, place two “say” blocks under the “if/then” block. In the first, type “The high score is.” Place a “high score” block from the data menu in the second “say” block. Test the code by clicking the flag. After 5 pitches, the coach calculates the batting average and saves it in the variable named “batting average.” Then, the coach says “your batting average is…” followed by the batting average. If the batting average beats the previous high score, it becomes the new high score. Finally, the coach says the high score. Using variables helps you keep track of valuable information. Here's the game plan: Add a variable to store the batting average. Add a variable that keeps track of the high score. If the current batting average is the highest, set the high score using “if,” “set to,” and “greater than,” blocks and the “batting average” and “high score” variables.

Make the coach say the high score.

Choose an Add-On
Celebration
Program a celebration for when the batter achieves a perfect batting average.
Over the Plate
Make the baseball look more realistic as it approaches or moves away from the plate.
High Score
Tell the batter if it has beaten the best batting average so far.
Out of this Ballpark
Program the sound of a cheering crowd.
In the Big Leagues
Make the pitcher tell a story before the game starts.
Going through the Motions
Animate the pitcher so it looks like he or she is moving.
arrow_backward Back
Next arrow_forward
Instructions
  1. Choose an add-on and click "Watch" to learn how to customize your project.
  2. When you finish, come back to the add-ons screen, and try another add-on!