试图射击子弹

时间:2011-12-30 08:22:37

标签: c# .net math xna

所以,我正在制作我自己的小行星游戏,而且我有点卡住了。当我按下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

1 个答案:

答案 0 :(得分:3)

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

你需要的只是一点三角学。现在你的速度矢量是常数&lt; 7,7&gt;。你想要做的是让那个向量朝向宇宙飞船指向的方向。因此,假设您的角度是弧度而不是度数(如果它们以度为单位,您需要将它们更改为弧度),我们可以使用Math.Sin()Math.Cos()来获取单个X和Y分量像这样的速度矢量:

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