繁体   English   中英

Monogame如何调整图像大小

[英]Monogame how to resize an image

我是使用 monogame 创建游戏的新手,我一直在尝试弄清楚如何调整我刚添加的图像的大小。 这是我到目前为止所拥有的。

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

        StickFigure = Content.Load<Texture2D>("StickFigure");
        
       
    }

    protected override void Update(GameTime gameTime)
    {
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            Exit();

        if (Keyboard.GetState().IsKeyDown(Keys.W))
        {
            stickPos.Y -= 10;
        }
        if (Keyboard.GetState().IsKeyDown(Keys.S))
        {
            stickPos.Y += 10;
        }
        if (Keyboard.GetState().IsKeyDown(Keys.A))
        {
            stickPos.X -= 10;
        }
        if (Keyboard.GetState().IsKeyDown(Keys.D))
        {
            stickPos.X += 10;
        }


        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        _spriteBatch.Begin();

        _spriteBatch.Draw(StickFigure, stickPos, Color.White);

        _spriteBatch.End();

        base.Draw(gameTime);
    }

有谁知道如何更改此图像的大小以便我可以按比例缩放大小。 谢谢!

SpriteBatch class 的 MonoGame 文档(链接如下)列出了几种包含缩放参数的绘制方法。 或者,我有一个模糊的 memory 简单地从特定的源矩形绘制到特定的目标矩形可能会自动调用缩放,但我可能在那里错了。

雪碧批次

根据需求,可以采用三种方式:

  1. 如果移动速度始终为 integer(如您的示例代码速度 = 10,速度非常快,达到 60 fps), stickPos更改为 Rectangle。 移动方向受integer速度限制,最小速度1只能向8个方向移动(45度),2为16个方向(22.5),...最快。 碰撞由 Rectangle.Intersect 提供。

  2. 在矩形中镜像 Vector2。 所有方向和速度都是可能的。 如#1 中所述,与 Vector2 速度和碰撞一起使用。

  3. 使用 Draw 方法的比例参数。 仅将其用于装饰品,因为碰撞缩放必须单独计算。

其他注意事项:

将速度存储在变量或常量中。 一个变化而不是四个。

每一步存储一次 KeyboardState: var ks = Keyboard.GetState();


以下代码给出了每种技术的示例,假设源纹理大小为 64x64,正在以 32x32 绘制。

1.

    // Class Level
    Rectangle stickPos = new Rectangle(0,0,32,32);
    int speed = 10; // const int speed = 10; if it will never change at runtime.

    protected override void Update(GameTime gameTime)
    {
        var ks = Keyboard.GetState();
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || ks.IsKeyDown(Keys.Escape))
            Exit();

        if (ks.IsKeyDown(Keys.W))
        {
            stickPos.Y -= speed;
        }
        if (ks.IsKeyDown(Keys.S))
        {
            stickPos.Y += speed;
        }
        if (ks.IsKeyDown(Keys.A))
        {
            stickPos.X -= speed;
        }
        if (ks.IsKeyDown(Keys.D))
        {
            stickPos.X += speed;
        }
        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        _spriteBatch.Begin();

        _spriteBatch.Draw(StickFigure, stickPos, Color.White);

        _spriteBatch.End();

        base.Draw(gameTime);

}
    // Class Level
    Vector2 stickPos = new Vector2();
    Rectangle DrawRectangle = new Rectangle(0,0,32,32);
    float speed = 10; //any float value

    protected override void Update(GameTime gameTime)
    {
        var ks = Keyboard.GetState();
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || ks.IsKeyDown(Keys.Escape))
            Exit();

        if (ks.IsKeyDown(Keys.W))
        {
            stickPos.Y -= speed;
        }
        if (ks.IsKeyDown(Keys.S))
        {
            stickPos.Y += speed;
        }
        if (ks.IsKeyDown(Keys.A))
        {
            stickPos.X -= speed;
        }
        if (ks.IsKeyDown(Keys.D))
        {
            stickPos.X += speed;
        }
        // repeat the following code anytime sickPos changes.
        DrawRectangle.X = (int)stickPos.X;
        DrawRectangle.Y = (int)stickPos.Y;
        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        _spriteBatch.Begin();

        _spriteBatch.Draw(StickFigure, DrawRectangle, Color.White);

        _spriteBatch.End();

        base.Draw(gameTime);
}
    // Class Level
    Vector2 stickPos = new Vector2(0,0);
    float speed = 10; //any float value
    Vector2 scale = new Vector2(.5f,.5f); // 64 to 32 == .5f

    // use Update changes in #2

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        _spriteBatch.Begin();
/* This is the full overloaded Draw call used from SpriteBatch.cs line 179 in the following line.
public void Draw (Texture2D texture,
                Vector2 position,
                Rectangle? sourceRectangle,
                Color color,
                float rotation,
                Vector2 origin,
                Vector2 scale,
                SpriteEffects effects,
                float layerDepth)
*/

        _spriteBatch.Draw(StickFigure, stickPos, null, Color.White, 0, Vector2.Zero, scale, SpriteEffects.None,0);

        _spriteBatch.End();

        base.Draw(gameTime);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM