Build a Flappy Bird Clone in Rust and Bevy 0.14Part 7 - Scoring
In the previous part, we implemented the pipe movements and collision detection. In this part, we'll implement the score counting logic and update the score state when the bird passes the pipes. We'll also render the score on the top-right corner of the screen.
Updating the score
In order to update the score, we need a way to know that if a pipe is in the left side of the bird or in the right side, we also need to know that if the pipe has already been counted so that we don't count it again and only increase the score by 1
for each pipe.
For that, we'll need to store a piece of data for each pipe to know if it has been passed by the bird or not. To do that, we first need to add the new field to the pipes, which is going to be a boolean
.
Updating components
We only need to update one of the pipes, either the upper pipes or the lower pipes, for this tutorial, we're going to only update the UpperPipe
struct.
Updating setup
Since we added a new field, we also need to change the code that spawns the pipes to include the new field, the default value of the new field is going to be false
.
There's also a few more modifications we need to do to the code:
- Reset
passed
of all pipes tofalse
when the game restarts, we want to reset the pipes to their initial position and also reset thepassed
field tofalse
so that the score can be counted again. - Reset
passed
of the pipes tofalse
when they reset their position to the right side of the screen. - Add a score counting system that will increment the score by
1
whenever the bird passes a pipe.
Resetting pipes when the game restarts
When the game restarts, we need to reset the pipes to their initial position and also reset the passed
field to false
.
We can do that in the start_game
system that we previously created.
We changed the upper_pipe_query
to also query for the UpperPipe
component and get a mutable reference of it, this is because we're going to reset the passed
field of all the pipes to false
when the game restarts.
Resetting passed pipes
When the pipes reset their position to the right side of the screen, we also need to reset the passed
field to false
, we can update the pipes
system that we created before.
Now whenever the pipes position resets, the passed
field also resets to false
.
Score counting system
Now that we have made the necessary changes to the pipes, we can now implement the score counting system. The logic is simple, whenever we detect a pipe that is in the left side of the bird and the passed
field is false
, we increment the score by 1
and mark the pipe as passed by setting the passed
field to true
.
Updating the Bevy App
:
This prints the score to the console, let's try it out and see if it works as expected.
Score counting
Great! The score is now being counted whenever the bird passes the pipes. Now it's time to render the score on the screen.
Rendering the score
In the spawning game entities part, we learned how to use the texture atlas layout to render the numbers on the screen, but we didn't add the logic for it to render the numbers based on the score of the player.
Render Score System
Since we have access of the score in the Game
resource, we can create another system and use that score to render the numbers on the screen using the texture atlas layout.
The code above formats the score to ensure it has at least 3 digits, then it converts each digit to a number and updates the index
field of the TextureAtlas
component, since every index of the texture atlas layout corresponds to the actual number, this will render the score on the screen.
Let's try it out!
Rendering the score
That's it! We are successfully finished with building the flappy bird clone in Rust, we hope you enjoyed this tutorial series and learned a lot from it. If you have any questions or feedback, feel free to reach out to us, we'll be happy to help you out. Thank you for reading!