Adding Bullets and Shooting in SFML / C#

In the following post, we are going to create a new class called Bullet. The spacecraft needs bullets because it has to survive against the enemies. 

I created a class named Bullet. We need to define the speed of the bullet. This speed should be bigger than the speed of the player's craft. Also, we need to know the position of the bullets, because bullets will be spawned according to the positions of the player. We declared property for just get method to able to reach in the Player class and also for the position of bullet.

Let's examine the constructor of Bullet. We pass the position as a parameter. This position will be defined at the start of the position of the new bullet where it will be drawn. In addition, we determined the color of the bullet and its size. 

Another method is the update method. This method is about the movement of bullets. If a bullet is created then it should move in axis-y as negative. BTW, It's not the coordinate system, don't confuse your mind with it. 
using SFML.Graphics;
using SFML.System;

namespace shmup {
    class Bullet
    {
        private RectangleShape rectangle;
        public const float BULLET_SPEED = 20f;
        Vector2f position;

        Vector2f size = new Vector2f(5, 10);
        
        public Vector2f Position { get { return position; } }
        public RectangleShape RectangleBullet { get { return this.rectangle; } }

        public Bullet (Vector2f position) 
        {
            this.rectangle = new RectangleShape(size);
            this.rectangle.FillColor = Color.White;
            this.rectangle.Position = position;
            this.position = position;
        }

        public void update() {
            this.position.Y -= BULLET_SPEED;
            this.rectangle.Position = this.position;                
        }
    }
}
So let's use it player class. Firstly, I'm defining new field named delay. This variable is just to prevent continuous shooting. Also I added list generic to store bullet in it.
    class Player 
    {
        private int delay = 0;
        private Sprite sprite;
        public const float PLAYER_SPEED = 4f;
        Vector2f position;

        public List<Bullet> bullets = new List<Bullet>();
I created a new method called fire. In this method, we creates two bullet object to the list of bullets.
        private void fire()
        {
            this.delay++;
            if ( this.delay >= 15 )
            {
                this.bullets.Add(new Bullet(this.position));
                var positionOfSecondBullet = new Vector2f(this.position.X + 25, this.position.Y);
                this.bullets.Add(new Bullet(positionOfSecondBullet));
                this.delay = 0;
            }
        }
This method will be executed when the player pressed space key:
        public void keyHandler()
        {
            ...

            bool isFire = Keyboard.IsKeyPressed(Keyboard.Key.Space);
            if(isFire) this.fire();   
        }
We need to update every bullets in the list of bullets. Otherwise, the don't moved and removed when crossed the line.
        public void update() {
            this.keyHandler();
            this.sprite.Position = position;

            for (int i = 0; i < this.bullets.Count; i++) {
                this.bullets[i].update();
                if (this.bullets[i].Position.Y < 0) 
                {
                    this.bullets.Remove(this.bullets[i]);
                }
            }
        }
And finally, we will draw these bullets on the screen and we can see how it works:
        public void draw(RenderTarget window) 
        {
            window.Draw(this.sprite);
            foreach (var bullet in this.bullets)
            {
                window.Draw(bullet.RectangleBullet);    
            }
        }
Shooting with Spacescraft

No comments:

Post a Comment