My First Game – Part 8: Pong – Score Text

We pretty much have an MVP already by now — Players can bounce the ball back against each other, and the round restarts when the ball goes behind either of the paddles.

Now let’s keep track of the score to see who’s winning. First we need a variable that doesn’t get reset whenever we reload the MenuState  screen.

Note that whenever you create a HaxeFlixel template project, there is a Reg.hx  file in your source folder. You can use this file to store any game data during the game’s lifetime. This is where we’ll store our Pong score data too. We don’t need to modify anything in Reg.hx  because there’s already a scores  variable there.

Remember, we have two players, so we need to keep track of two scores. Where do we handle the scoring? Let’s do it in MenuState.hx , because that’s where most of our code logic is anyway:

There are three parts to note in the code above.

In the first part, by default, uninitialised values are null . In Haxe, there are various different default null values, depending on which target platform you want to build on, as noted in this page. As such, to play safe, I did a quick check on the initial values of the Reg.scores  array. In case the value is not larger than zero, we initialise it to zero.

In the second part, we just load the score values accordingly.

In the third part, I set the position of the scores at an arbitrary position — The top-middle part of the screen. We then create two FlxText  objects (based on the parameters for  new() ) and add them to the stage.

If you test the game now, you will notice that the score is always zero, no matter who wins. That’s because we haven’t added any code to manipulate the score. Let’s do that now. In Ball.hx , modify the code for update() :

Note that we have split the switchState  code into two parts — If the ball exits the screen on the left, it means Player 2 (on the right) gets a point. If the ball exits on the right, Player 1 (on the left) gets the point.

Now if you test the game, the score will update correctly:

ss7

Before we can pass this off as a somewhat completed game, we will add one finishing touch to it — sound effects. We’ll cover that in the next post.

Leave a Reply

Your email address will not be published. Required fields are marked *