0

坐着制作一个 2D 射击游戏,我的玩家需要能够使用角度向左和向右射击。我遇到的问题是我找不到使这项工作的方法。

那么如何计算以特定角度射击子弹的角度呢?

我在播放器类中的代码

class Player
{
    //Player 
    public Rectangle playercollisionbox;
    public Texture2D Playertexture;
    public Vector2 Position = new Vector2(470, 850);



    //Bullet
    public Texture2D bulletTexture;
    //How fast you shoot
    public float bulletDelay = 1;
    public List<Bullet> bulletList;

    //Angle bullet
    public float spriterotation = 0;
    public float speed = 1;
    public Vector2 bulletposition;


    //Health
    public int Health;
    public Vector2 healthbarposition = new Vector2(20, 40);
    public Texture2D healthbartexture;
    public Rectangle healthrectangle;


    //Contructor
    public Player()
    {
        bulletList = new List<Bullet>();
        Health = 200;
    }

    public void LoadContent(ContentManager Content)
    {

        bulletTexture = Content.Load<Texture2D>(@"images/projectile2");
        healthbartexture = Content.Load<Texture2D>(@"images/HealthBar");




    }

    public void Update(GameTime gameTime)
    {


        KeyboardState keyboardState = Keyboard.GetState();

        playercollisionbox = new Rectangle((int)Position.X, (int)Position.Y, Playertexture.Width, Playertexture.Height); 
        //Player movement
        if (keyboardState.IsKeyDown(Keys.Up))
        {
            Position.Y -= 7;
        }

        if (keyboardState.IsKeyDown(Keys.Left))
        {
            Position.X -= 7;
        }
        if (keyboardState.IsKeyDown(Keys.Down))
        {
            Position.Y += 7;
        }
        if (keyboardState.IsKeyDown(Keys.Right))
        {
            Position.X += 7;
        }
        //Player movement

        healthrectangle = new Rectangle((int)healthbarposition.X, (int)healthbarposition.Y, Health, 25);

        //Off-screen block
        if (Position.X < 0)
        {
            Position.X = 0;
        }
        if (Position.Y < 0)
        {
            Position.Y = 0;
        }
        if (Position.X > 943)
        {
            Position.X = 943;
        }
        if (Position.Y > 904)
        {
            Position.Y = 904;
        }

        //Bullet
        if (keyboardState.IsKeyDown(Keys.Space))
        {
            Shoot();
        }
        UpdateBullet();

    }

    public virtual void Draw(SpriteBatch spriteBatch)
    {

        spriteBatch.Draw(Playertexture, Position, Color.White);
        foreach (Bullet b in bulletList)
        {
            b.Draw(spriteBatch);
        }
        spriteBatch.Draw(healthbartexture, healthrectangle, Color.White);
    }
    //Shooting method 
    public void Shoot()
    {
        if (bulletDelay >= 0)
        {
            bulletDelay--;
        }
        if (bulletDelay <= 0)
        {
            //First Bullet
            Bullet newBullet = new Bullet(bulletTexture);
            newBullet.position = new Vector2(Position.X + 40 - newBullet.texture.Width / 2, Position.Y - 40);
            newBullet.isVisible = true;
            //Second Bullet





            if (bulletDelay == 0)
            {
                bulletDelay = 20;
            }

            if (bulletList.Count() < 20)
            {
                bulletList.Add(newBullet); 
            }


        }
    }
    //Updating bullet after shooting
    public void UpdateBullet()
    {
        //speed on nullet
        foreach (Bullet b in bulletList)
        {

            b.position.Y = b.position.Y - b.speed;
            b.bulletcollisionbox = new Rectangle((int)b.position.X, (int)b.position.Y, b.texture.Width, b.texture.Height);

            //outside screen removes it
            if (b.position.Y <= 0)
            {
                b.isVisible = false;
            }
        }

        for (int i = 0; i < bulletList.Count; i++)
          {
              if (!bulletList[i].isVisible)
              {
                  bulletList.RemoveAt(i);
                  i--;
              }
          }

    }

 }

我的子弹课

 public class Bullet
{
    public Texture2D texture;
    public Vector2 origin;

    public Vector2 position;
    public bool isVisible;
    public float speed;
    public Rectangle bulletcollisionbox;




    public Bullet(Texture2D newTexture)
    {
        speed = 10;
        texture = newTexture;
        isVisible = false;
    }

    public void Draw(SpriteBatch spriteBatch)
    {

        spriteBatch.Draw(texture, position, Color.White);
    }
}
4

2 回答 2

1

以弧度表示的 2 个向量之间的角度

Math.Atan2(b.Y - a.Y,b.X - a.X);

子弹方向

Vector2 direction = playerPosition - enemyPosition;
direction.Normalize();

然后通过方向向量增加子弹位置

于 2013-05-17T10:03:22.500 回答
1

实际上,我有一个小的自制库,我在所有 XNA 项目中都使用它。辅助功能之一是:

/// <summary>
/// Converts a given angle into a Vector2.
/// </summary>
/// <param name="angle">The angle (in radians).</param>
/// <param name="normalize">True to normalize the resultant vector, otherwise false.</param>
/// <returns>A vector representing the specified angle.</returns>
public static Vector2 Vector2FromAngle(double angle, bool normalize = true)
{
    Vector2 vector = new Vector2((float)Math.Cos(angle), (float)Math.Sin(angle));
    if (vector != Vector2.Zero && normalize)
        vector.Normalize();
    return vector;
}

随意使用它!它返回的向量将是子弹的轨迹。

如果您不熟悉如何将度数转换为弧度radians = degrees * pi / 180并将其转换回degrees = radians * 180 / pi.

于 2013-05-16T21:18:21.660 回答