矩形显示在游戏窗口与XNA [英] Displaying rectangles in game window with XNA
问题描述
我想我的游戏网格划分成矩形阵列。每个矩形是40×40,并有14个长方形的每一列,共25列。这个占地560x1000一个游戏区
I want to divide my game grid into an array of rectangles. Each rectangle is 40x40 and there are 14 rectangles in every column, with a total of 25 columns. This covers a game area of 560x1000.
这是我已经设置了使矩形在游戏网格的第一列的代码:
This is the code I have set up to make the first column of rectangles on the game grid:
Rectangle[] gameTiles = new Rectangle[15];
for (int i = 0; i <= 15; i++)
{
gameTiles[i] = new Rectangle(0, i * 40, 40, 40);
}
我敢肯定这工作,但当然我无法证实,因为矩形没有在屏幕上呈现为我身体上看到他们。我想为调试目的,做的是呈现一个边框,或者用颜色填充矩形,所以我可以看到它在游戏本身,只是为了确保这一工作的。
I'm pretty sure this works, but of course I cannot confirm it because rectangles do not render on the screen for me to physically see them. What I would like to do for debugging purposes is to render a border, or fill the rectangle with color so I can see it on the game itself, just to make sure this works.
有没有办法做到这一点?或者有什么比较简单的方法,我可以确保此工作的?
Is there a way to make this happen? Or any relatively simple way I can just make sure that this works?
非常感谢你。
推荐答案
首先,使白色矩形1x1像素质地:
First, make a 1x1 pixel texture of white for the rectangle:
var t = new Texture2D(GraphicsDevice, 1, 1);
t.SetData(new[] { Color.White });
现在,您需要渲染矩形 - 假设矩形称为矩形
。用于渲染填充块,这是非常简单的 - 确保设置色调颜色
来是你想要的颜色。只需使用此代码:
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条线构成的轮廓(这里的矩形是研究
):
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
希望它帮助!
Hope it helps!
这篇关于矩形显示在游戏窗口与XNA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!