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


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.

Click Make a variable.

Give it a name like batting average.

Then click okay.

Click the coach.

Drag out a set batting average to block and place it under the when I receive block that starts the calculation.

Drag the hits divided by five block out of the say block and place it inside the white box next to the to.

Drag out 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 recording batting averages.

To do this, create another variable.

Computer scientists use variables to keep track of multiple changing data points.

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.

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 drop down to make sure you are setting the high score variable.

Set the high score to the batting average.

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 in the second say block.

Test the code by clicking the flag.

After five 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.

Alright, 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
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.
Going through the Motions
Animate the pitcher so it looks like he or she is moving.
arrow_backward Back
Next arrow_forward
  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!