在屏幕外产生物体

时间:2016-11-30 15:30:39

标签: c# positioning

所以我制作的游戏非常类似于小行星,我不知道如何让小行星在屏幕外生成。任何帮助将非常感激。以下是小行星的当前代码:

    class asteroid
{
    Texture2D m_txr;
    public Vector2 velocity, position;
    public BoundingSphere circle;
    public Rectangle rect;

    public asteroid(Texture2D txr, Vector2 screen_size, Random RNG)
    {
        m_txr = txr;
        position = Vector2.Zero;
        position.X = RNG.Next(0, (int)screen_size.X);
        position.Y = RNG.Next(0, (int)screen_size.Y);

        circle = new BoundingSphere(new Vector3(position.X + m_txr.Width / 2, position.Y + m_txr.Height / 2, 0), m_txr.Width / 2);
        velocity = new Vector2(0, 0);

        while (velocity.X == 0 || velocity.Y == 0)
            velocity = new Vector2((float)RNG.NextDouble() * RNG.Next(-2, 2), (float)RNG.NextDouble() * RNG.Next(-2, 2));

        rect = new Rectangle((int)position.X, (int)position.Y, m_txr.Width, m_txr.Height);
    }

    public void update(Vector2 screen_size)
    {
        circle.Center.X = position.X + m_txr.Width / 2;
        circle.Center.Y = position.Y + m_txr.Height / 2;

        rect.X = (int)position.X;
        rect.Y = (int)position.Y;


        position += velocity;

        if (position.X > screen_size.X + m_txr.Width)
            position.X = 0;
        if (position.X < 0 - m_txr.Width)
            position.X = screen_size.X;
        if (position.Y > screen_size.Y + m_txr.Height)
            position.Y = 0;
        if (position.Y < 0 - m_txr.Height)
            position = screen_size;
    }

    public void drawme(SpriteBatch sb)
    {
        sb.Draw(m_txr, position, Color.White);
    }
}

由于

2 个答案:

答案 0 :(得分:1)

对于困惑和久违的抱歉,我完全忘记了这个问题。

如果有人需要,我会提出解决方案;

            // possition
        int XorY = RNG.Next(0, 2);
        if (XorY == 0) // top or bottom
        {
            int TorB = RNG.Next(0, 2);
            if (TorB == 0) // top
            {
                position.X = RNG.Next(0, (int)screen_size.X);
                position.Y = 0 - m_txr.Width;
            }
            else if (TorB == 1) // bottom
            {
                position.X = RNG.Next(0, (int)screen_size.X);
                position.Y = (int)screen_size.Y;
            }
        }
        else if (XorY == 1) // sides
        {
            int LorR = RNG.Next(0, 2);
            if (LorR == 0) // left side
            {
                position.X = 0 - m_txr.Width;
                position.Y = RNG.Next(0, (int)screen_size.Y);
            }
            else if (LorR == 1) // right side
            {
                position.X = (int)screen_size.X;
                position.Y = RNG.Next(0, (int)screen_size.Y);
            }
        }

答案 1 :(得分:0)

你在asteriod生成方法中限制自己的产卵,因为你根据屏幕的大小随机定位,这应该解决它:

public asteroid(Texture2D txr, Vector2 screen_size, Random RNG)
{
    m_txr = txr;
    position = Vector2.Zero;
    position.X = RNG.Next(0, (int)screen_size.X + 10);
    position.Y = RNG.Next(0, (int)screen_size.Y + 10);

    circle = new BoundingSphere(new Vector3(position.X + m_txr.Width / 2, position.Y + m_txr.Height / 2, 0), m_txr.Width / 2);
    velocity = new Vector2(0, 0);

    while (velocity.X == 0 || velocity.Y == 0)
        velocity = new Vector2((float)RNG.NextDouble() * RNG.Next(-2, 2), (float)RNG.NextDouble() * RNG.Next(-2, 2));

    rect = new Rectangle((int)position.X, (int)position.Y, m_txr.Width, m_txr.Height);
}