在XNA 4.0中绘制具有不同属性的多个Sprite

时间:2016-07-06 23:26:58

标签: c# visual-studio xna

我正在编写一些代码,通过创建一个精灵类,我将同一个精灵的一百个副本动画到屏幕上。我希望精灵,一个四环互锁环相互旋转的动画,在一定高度绘制,在屏幕上下拉,像球一样弹跳,每次弹跳逐渐减少,直到它们完全停止。我设法完成这部分;但是,我似乎无法找到一种方法来随机化每个不同精灵的加速度和动画速度。有人可以为我的代码提供一些建议吗?

游戏1.cs

namespace lab_6
  {

   public class Game1 : Microsoft.Xna.Framework.Game
   {
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;

    Sprite rings;


    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
        graphics.PreferredBackBufferHeight = 600;
        graphics.PreferredBackBufferWidth = 800;
    }


    protected override void Initialize()
    {
        // TODO: Add your initialization logic here
        base.Initialize();
    }


    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);


        Texture2D rings_texture = Content.Load<Texture2D>("Images/threerings");

        //animation
        Point frameSize = new Point(75, 75);
        Point currentFrame = new Point(0, 0);
        Point sheetSize = new Point(6, 8);
        int millisecondsPerFrame = 50;
    rings = new Sprite(rings_texture, ringsPos,
            frameSize, 0, currentFrame, sheetSize, ringsSpeed, millisecondsPerFrame);
    }



    protected override void UnloadContent()
    {
        // TODO: Unload any non ContentManager content here
    }


    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Update(GameTime gameTime)
    {
        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();
        if (Keyboard.GetState().IsKeyDown(Keys.Escape))
            this.Exit();
        // TODO: Add your update logic here
        rings.Update(gameTime, Window.ClientBounds);

        base.Update(gameTime);
    }


    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        // TODO: Add your drawing code here
        spriteBatch.Begin();

        rings.Draw(gameTime, spriteBatch);
        spriteBatch.End();
    }
   }
}

Sprite.cs

namespace lab_6
{
    public class Sprite
    {
    //basics
    protected Texture2D rings;
    protected Vector2 ringsPos = new Vector2(0,0);
    protected Color tint = Color.White;
    protected Vector2 ringsSpeed = new Vector2(0,0);
    protected Vector2 ringsAccel = new Vector2(0, 1);

    //animation
    protected Point frameSize = new Point(75,75);
    protected Point currentFrame = new Point(0, 0);
    protected Point sheetSize = new Point(6,8);

    //animation timing
    protected int timeSinceLastFrame = 0;
    protected int millisecondsPerFrame = 50;
    const int defaultMillisecondsPerFrame = 16;

    //bounding box offset
    protected int collisionOffset;

    Random r = new Random(DateTime.Now.Millisecond);

    public Sprite(Texture2D rings, Vector2 ringsPos, Point frameSize,
                    int collisionOffset, Point currentFrame, Point sheetSize, Vector2 ringsSpeed,
                    int millisecondsPerFrame)
    {
        this.rings = rings;
        this.ringsPos = ringsPos;
        this.frameSize = frameSize;
        this.collisionOffset = collisionOffset;
        this.currentFrame = currentFrame;
        this.sheetSize = sheetSize;
        this.ringsSpeed = ringsSpeed;
        this.millisecondsPerFrame = millisecondsPerFrame;
    }




    public virtual void Update(GameTime gameTime, Rectangle clientBounds)
    {


        int maxY = (600 - frameSize.Y );
        ringsAccel.Y += (byte)r.Next((1 / 10), 1);
        ringsSpeed.Y += ringsAccel.Y;
        ringsPos.Y += ringsSpeed.Y;


        if (ringsPos.Y > maxY)
        {
            ringsSpeed *= -0.8f;
            ringsPos.Y = maxY;
        }

        //Update animation frame
        millisecondsPerFrame = 50;
        millisecondsPerFrame *= ((byte)r.Next(1, 10));

        timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
        if (timeSinceLastFrame > millisecondsPerFrame)
        {
            timeSinceLastFrame = 0;
            ++currentFrame.X;
            if (currentFrame.X >= sheetSize.X)
            {
                currentFrame.X = 0;
                ++currentFrame.Y;
                if (currentFrame.Y >= sheetSize.Y)
                    currentFrame.Y = 0;
            }
        }

    }



    public virtual void Draw(GameTime gameTime, SpriteBatch spriteBatch)
    {
        for(int i = 0; i < 100; )
        {

           Vector2 newPos = ringsPos + new Vector2((10 * i), (1 * (byte)r.Next((1/10), 1)));
            spriteBatch.Draw(rings, newPos,
                new Rectangle(currentFrame.X * frameSize.X, 
                currentFrame.Y * frameSize.Y,
                frameSize.X, frameSize.Y),
                tint, 0, Vector2.Zero, 1f, SpriteEffects.None, 0);
            i++;
            r = new Random(DateTime.Now.Second);
        }
    }
   }
}

1 个答案:

答案 0 :(得分:0)

将ringSpeed和ringAccel的构造函数移动到构造函数并将其随机化。