Skip to content

Please update your browser

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

4. Quanti gol hai fatto?


In this video, you will use a variable to keep track of the number of hits the batter gets. Variables in computer science are places to store changing values. In this case, the variable will store the number of hits, which will change each time the batter successfully hits the ball. First, create the variable. Click the “data” menu, and click “make a variable.” Name the variable something that makes sense, like “hits.” More than one sprite will need to use the contents of hits, so keep “for all sprites” selected. Click “OK.” In the top left corner of the stage, you should see an orange box labeled “hits.” It shows the current value stored in the variable “hits.”

At the moment, it’s “0.” A few new blocks for interacting with the variable have also appeared. The value of “hits” should change each time the baseball touches the red swung bat. From the “data” menu, drag the "change hits by 1" block into the “if/then” block. Click the flag to test the game, then hit the ball. Here comes the pitch – it’s outta here! Now you can see that the value of “hits” has increased. But wait… the batter only hit the ball once, and “hits” increased by more than one. What’s going on?

The code keeps checking if the baseball hits the red bat each time the loop runs. If the baseball touches the red bat for too long, the number of hits keeps increasing because the loop runs faster than the bat can change back.

To fix this, place a “wait” block from the control menu inside the “if/then” block. To figure out how long to wait, click on the bat sprite to check its code. Notice that the bat is red for 0.2 seconds, so enter 0.2 seconds in the “wait” block.

Test the game again. When you click the bat, it may switch the scripts over to the bat sprite's code. If this happens to you, just click the baseball again to keep programming it. The number of hits now goes up by only 1!

But, it didn’t start counting hits from 0. To fix this, go to the “data” menu, drag out a “set hits to zero” block, and place it at the beginning of the program just under the “when flag clicked” block. Click the flag to test the game again.

This time, hits start at 0 and increase by one when the bat hits the baseball. Much better!

Did you notice that there were lots of minor problems to tweak in your program? These are called bugs, and as you fix them you are “debugging” your program. This is an important step in programming! Computer scientists make mistakes all the time, so stick with it as you tweak your code. Here's the game plan: Add a variable named “hits” to your game. Then, keep track of the batter's hits using “change by.” Make the program wait 0.2 seconds before running the loop again. Finally, reset the number of hits to 0 at the beginning of each game. In the next video, you'll add analytics to your project.

arrow_backward Indietro
Avanti arrow_forward
  1. Aggiungi al gioco una variabile chiamata "gol".
  2. Registra i gol del giocatore usando "Cambia di".
  3. Ordina al programma di attendere 0,2 secondi prima di eseguire di nuovo il loop.
  4. Porta a 0 il numero delle battute all'inizio di ogni partita.