Displaying The Score in SFML / C#

In the following post, we'll learn how to display text on the screen. 

I found a font to use in the game. This font's name is FreeMono. You can find it on Internet, also you can get it from the GitHub repository of this game. I added this font in bin/Assets/Fonts folder. 

Displaying text on the screen is a very simple operation in SFML. Primarily, we need a class to manage texts on the game. So, I created TextManager class. In this class, I declared the path of the font we will use. After that, I defined a font field to create an object in loadFont of TextManager. And finally, we defined the list called texts to store texts and this provides easy management in this way. 

Let's write TextManager class. 

using System;
using System.Collections.Generic;
using SFML.Graphics;
using SFML.Window;
using SFML.System;

namespace shmup
{
    class TextManager
    {
        private const string FONT_PATH = "bin/Assets/Fonts/";
        public static Font font;

        private static List<Text> texts = new List<Text>();

        public static void loadFont(string fontFamily) 
        {
            font = new Font(FONT_PATH + fontFamily + ".ttf"); 
        }

        public static void typeText(string text, int value, uint fontSize, Color fontColor, Vector2f position)
        {
            Text textContent = new Text(text + value.ToString(), font, fontSize);
            textContent.Position = position;
            textContent.FillColor = fontColor;
            texts.Add(textContent);
        }

        public static void draw(RenderTarget window) 
        {
            for (int i = 0; i < texts.Count; i++)
            {
                window.Draw(texts[i]);
                texts.Remove(texts[i]);
            }
        }

    }
}
Now, let's use this class in our game. But first I need to display the score of the player. Therefore, I declared a field called score as integer and also property to use it:
namespace shmup {
    class Player 
    {
        ...
        private int score = 0;

        ...
        public int Score { get { return score; } set { score = value; } }
Now, the score of the player will be increased by 10 when the bullets of the player collided the invader. I'm going to do in the EnemyManager class:
        public bool collisionOfBullets(Enemy enemy, Player player)
        {
            ...

            for (int i = 0; i <yer.bullets.Count; i++)
            {
                if (enemy.EnemySprite.GetGlobalBounds().Intersects(player.bullets[i].RectangleBullet.GetGlobalBounds())) 
                {
                    player.Score += 10;
                    player.bullets.Remove(player.bullets[i]);
                    return true;
                }
            } 
            return false;
        }
We should load the font of TextManager in the Game class:
        public Game()
        {
            ...

            TextureManager.LoadTexture();
            TextManager.loadFont("FreeMono");
I created a new method called updateScore for displaying text on the screen. If you notice we call the score of the player to put on the text object:
        private void updateScore()
        {
            TextManager.typeText("Score: ", player.Score, 25, Color.White, new Vector2f(10f, 10f));
        }
Let's use this method in the update method of the Game class:
        private void update() 
        {
            this.updateScore();
            this.player.update();
            this.enemies.update(this.player);
            AnimationManager.update();
        }
Finally, we can draw these texts with TextManager on the screen:
        private void draw() 
        {
            this.window.Clear(Color.Blue);
            
            this.window.Draw(this.background); 
            this.player.draw(this.window);
            this.enemies.draw(this.window);
            AnimationManager.draw(this.window);
            TextManager.draw(this.window);

            this.window.Display();
        }
Let's check out the game:
The score of the player in space invaders


No comments:

Post a Comment