Updating Scores and Resetting Play | Making Your First Game in Godot

At this point we have a mostly functioning game where there are two player controlled paddles, the ball bounces, and you can score…. except that scoring doesn’t actually do anything. Keep reading to learn how we can update a scoreboard and reset the ball.

Connecting the Goal Zone Areas

Just like the timer node, Area2D Nodes have built in signals that we can leverage in our scripts. For our use case, we are looking for the body_entered signal that is in both the Right and Left Goal’s Area2D. We will connect those to the main game script and they will appear under the previous timer timeout() function. Inside each of the functions add the same BallTimer.start() line from before. This will then ultimately call the ball_spawn() function to reset the ball and resume play whenever the ball goes past the goal zones on either end of the field.

Area2D Body Entered

Adding and Updating the Scoreboard

Before we can update the scores, we need to add the scoreboard to our main game scene. This is accomplished by using an HBoxContainer (which horizontally aligns and spaces content) to hold Labels that will represent the score. We set the width of the HBoxContainer to span our enter viewport and set the alignment to center so the labels will stay centered on screen.

If we update the label to show the player scores 0-0 we’ll notice its kind of hard to see. Inside of each label, the theme can be overridden to increase the font size and change the font color. I went with pure white to match the aesthetic and made the font 130.

Editor with Score Labels

We then want to create two variables in the main game script to hold the Player 1 and Player 2 scores. Inside each of the relevant Area2D Entered functions, we can also +=1 for each time the ball goes into the goal zone. So not only will the game reset the ball to the original position to restart play but the scores are being calculated too! The final thing to do is to update the scoreboard. Labels have a text property that can be accessed and assigned. For us, we can assign the label for each player’s score’s text to the player score variables. The most important piece is to set it to str(player_XXX_score) so it converts the integer to a string before setting the label text.

Updated Body Entered Function

And just like that we have a completed game! Two players are able to battle to the end to see who can rack up the highest score. Now this is a very basic version of pong that doesn’t have sounds, a menu, AI, or even a score to win but with this foundation all the other changes can be easily added. If you want to request additional tutorials on adding to this Pong Clone, get in contact with us!

Be sure to check out our other posts and subscribe on YouTube to find more helpful content like this!