如何在 Monogame 屏幕中正确拉伸图像

How to properly stretch image in Monogame screen

我是 Monogame 和 C# 的初学者。我正在制作我的第一款游戏。 我已经更改了 Game1 构造函数中的屏幕尺寸。我正在绘制一个精灵,我已将矩形宽度和高度设置为屏幕的宽度和高度以适合屏幕。我正在创建一个滚动背景,我必须为此更改精灵的位置。我将位置设置为 Vector。零使精灵向右拉伸。

Game1
//member variables
        Vector2 stage;
        Texture2D fbTexture;
        private List<ScrollingBackground> sb;
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            //change the screen size
            graphics.PreferredBackBufferHeight = 900;
            graphics.PreferredBackBufferWidth = 600;
            Window.Title = "Flappy Bird Returns";
            graphics.ApplyChanges();
        }

        protected override void LoadContent()
        {
            stage = new Vector2(graphics.PreferredBackBufferWidth, 
                    graphics.PreferredBackBufferHeight);

            //Day Background
            Texture2D dayTex = Content.Load<Texture2D>("Images/background-day");
            Vector2 daySpeed = new Vector2(1, 0);
            sb = new List<ScrollingBackground>()
            {
                //set the width and height to be the same as the screen 
                //dimensions then this will 
                //stretch the image past its original dimensions to fit.
                new ScrollingBackground(this, 
                spriteBatch, 
                dayTex,
                new Vector2(0,0), 
                new Rectangle(0,0, (int)stage.X, (int)stage.Y),
                daySpeed)

            };
            foreach (ScrollingBackground scrollbackg in sb)
                this.Components.Add(scrollbackg);
          }

//ScrollingBackground.cs
private SpriteBatch spritebatch;
        private Texture2D tex;
        private Vector2 position1;
        private Vector2 speed;
        private Rectangle srcRect;

        public ScrollingBackground(Game game, SpriteBatch spriteBatch, Texture2D tex, Vector2 pos, Rectangle srcRect, Vector2 speed) : base(game)
        {
            this.spritebatch = spriteBatch;
            this.tex = tex;
            this.position1 = pos;
            this.speed = speed;
            this.srcRect = srcRect;
        }

        public override void Draw(GameTime gameTime)
        {
            spritebatch.Begin();
            spritebatch.Draw(tex, position1, srcRect, Color.White);
            spritebatch.End();
            base.Draw(gameTime);
        }

//Without position1
spritebatch.Draw(tex, srcRect, Color.White);

您需要为 float scale 参数提供参数。这意味着您需要为此方法使用更具体的签名:

SpriteBatch.Draw(Texture2D texture, Vector2 position, Rectangle? sourceRectangle, Color color, float rotation, Vector2 origin, **float scale**, SpriteEffects effects, float layerDepth)

同时,您需要为所有其他参数指定默认值。例如,下面是使用上述方法签名编写相同的 Draw() 方法的方法:

public override void Draw(GameTime gameTime)
{
    spritebatch.Begin();
    spritebatch.Draw(tex, position1, srcRect, Color.White, 0f, Vector2.Zero, 2f, SpriteEffects.None, 0f);
    spritebatch.End();
    base.Draw(gameTime);
}

在这里,我将您的纹理设置为原始大小的 2 倍。将 2f 更改为您想要的大小。请记住,最终大小将是 srcRect 的 width/height 值乘以该数字。