且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 XNA 在游戏窗口中显示矩形

更新时间:2023-02-17 20:33:44

首先,为矩形制作一个 1x1 像素的白色纹理:

First, make a 1x1 pixel texture of white for the rectangle:

var t = new Texture2D(GraphicsDevice, 1, 1);
t.SetData(new[] { Color.White });

现在,您需要渲染矩形 - 假设矩形被称为 rectangle.对于渲染填充块,它非常简单 - 确保将 tint Color 设置为您想要的颜色.只需使用此代码:

Now, you need to render the rectangle - assume the Rectangle is called rectangle. For a rendering a filled block, it is very simple - make sure to set the tint Color to be the colour you want. Just use this code:

spriteBatch.Draw(t, rectangle, Color.Black);

对于边框,是否更复杂.你必须画出构成轮廓的 4 条线(这里的矩形是 r):

For a border, is it more complex. You have to draw the 4 lines that make up the outline (the rectangle here is r):

int bw = 2; // Border width

spriteBatch.Draw(t, new Rectangle(r.Left, r.Top, bw, r.Height), Color.Black); // Left
spriteBatch.Draw(t, new Rectangle(r.Right, r.Top, bw, r.Height), Color.Black); // Right
spriteBatch.Draw(t, new Rectangle(r.Left, r.Top, r.Width , bw), Color.Black); // Top
spriteBatch.Draw(t, new Rectangle(r.Left, r.Bottom, r.Width, bw), Color.Black); // Bottom

希望对你有帮助!