试图射击子弹

本文关键字:子弹 射击 | 更新日期: 2023-09-27 18:19:01

所以,我正在制作自己的小行星游戏,我有点卡住了。当我按下z键时,我有一颗子弹从船上发射出来,但子弹总是朝同一个方向发射,我不知道如何改变这一点?我要它朝船的朝向发射。此外,如果我快速按下Z键(发射许多子弹),一颗子弹似乎一直在重置。如何解决这些问题?下面是我的代码:

class SpaceShip
{
    private Vector2 pos; //Our position in 2D.
    private Texture2D tex; //Our texture
    private float speed = 8; //The speed to move.
    private float angleOfRotation = 0;
    Sprite tempSprite;

    public SpaceShip(Vector2 pos, Texture2D tex, Texture2D bulletsToAdd)
    {
        this.pos = pos; //Our starting position
        this.tex = tex; //Our texture
        tempSprite = new Sprite(bulletsToAdd); 
    }

    private void WorldWrap(int worldWidth, int worldHeight)
    {
        if (this.pos.X < 0)
        {
            //If we've past the left side of the window, set on other side of the world.
            this.pos.X = worldWidth;
        }
        if (this.pos.X > worldWidth)
        {
            //If we've past the right side of the window, set on other side of the world.
            this.pos.X = this.tex.Width;
        }
        if (this.pos.Y < 0)
        {
            //If we've passed the top side of the window, set on other side of the world.
            this.pos.Y = worldHeight;
        }
        if (this.pos.Y > worldHeight)
        {
            //If we've passed the bottom side of the window, set on other side of the world.
            this.pos.Y = this.tex.Height;
        }
    }
    public void CreateBullets(Texture2D bulletsToAdd)
    {
        tempSprite.Position = new Vector2((pos.X),(pos.Y));
        tempSprite.Velocity = new Vector2((float)7, 7);
        //tempSprite.Rotation = angleOfRotation;
    }

    public void Update(int WorldWidth, int WorldHeight, Texture2D bulletsToAdd)
    {
        KeyboardState keys = Keyboard.GetState();
        if (keys.IsKeyDown(Keys.A))
        {
            //We want to turn the ship LEFT
            this.angleOfRotation -= 2f;
        }
        if (keys.IsKeyDown(Keys.D))
        {
            //We want to turn the ship RIGHT
            this.angleOfRotation += 2f;
        }
        if (keys.IsKeyDown(Keys.W))
        {
            //We're pressing the up key (W) so go Forwards!
            this.pos.X += (float)(Math.Cos(this.angleOfRotation * Math.PI / 180) * this.speed);
            this.pos.Y += (float)(Math.Sin(this.angleOfRotation * Math.PI / 180) * this.speed);
        }
        if (keys.IsKeyDown(Keys.S))
        {
            //We're pressing the down key (S) so go Backwards!
            this.pos.X -= (float)(Math.Cos(this.angleOfRotation * Math.PI / 180) * this.speed);
            this.pos.Y -= (float)(Math.Sin(this.angleOfRotation * Math.PI / 180) * this.speed);
        }
        if (keys.IsKeyDown(Keys.Right))
        {
            //We're pressing the up key (W) so go Forwards!
            this.pos.X += (float)(Math.Cos(this.angleOfRotation * Math.PI / 180) * this.speed);
            this.pos.Y += (float)(Math.Sin(this.angleOfRotation * Math.PI / 180) * this.speed);
        }
        if (keys.IsKeyDown(Keys.Left))
        {
            //We're pressing the down key (S) so go Backwards!
            this.pos.X -= (float)(Math.Cos(this.angleOfRotation * Math.PI / 180) * this.speed);
            this.pos.Y -= (float)(Math.Sin(this.angleOfRotation * Math.PI / 180) * this.speed);
        }
        if (keys.IsKeyDown(Keys.Up))
        {
            //We want to turn the ship LEFT. Rotate it counter-clockwise
            this.angleOfRotation -= 2f;
        }
        if (keys.IsKeyDown(Keys.Down))
        {
            //We want to turn the ship RIGHT. Rotate it clockwise
            this.angleOfRotation += 2f;
        }
        if (keys.IsKeyDown(Keys.Z))
        {
            CreateBullets(bulletsToAdd); 
        }

        tempSprite.Position += tempSprite.Velocity;
        //check if we need to move the ship.
        WorldWrap(WorldWidth, WorldHeight);
    }
    public void Draw(SpriteBatch batch)
    {
        batch.Draw(this.tex, //Texture to use.
        this.pos, //Position to draw at
        new Rectangle(0, 0, this.tex.Width, this.tex.Height),Color.White, (float)((this.angleOfRotation + 90) * Math.PI / 180), new Vector2(this.tex.Width / 2, this.tex.Height / 2), 1.0f, SpriteEffects.None, 0.0f);
        batch.Draw(tempSprite.Texture, tempSprite.Position, null, Color.White, tempSprite.Rotation, tempSprite.Center, tempSprite.Scale, SpriteEffects.None, 1.0f);
    } //End Draw function

试图射击子弹

首先,您只有一个项目- tempSprite。你可能想要在某个地方创建一个单独的Bullet类和一个List<Bullet>类,你可以在其中添加子弹。

你所需要的只是一点三角知识。现在你的速度矢量是一个常数。你要做的就是让这个矢量指向飞船指向的方向。所以假设你的角度是弧度而不是度(如果它们是度,你会想把它们变成弧度),我们可以使用Math.Sin()Math.Cos()来获得速度矢量的X和Y分量,像这样:

bulletSpeed = 7;
tempSprite.Velocity = new Vector2((float)Math.Cos(angleOfRotation) * bulletSpeed, (float)Math.Sin(angleOfRotation) * bulletSpeed);