简体   繁体   English

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

[英]Displaying rectangles in game window with XNA

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. 每个矩形为40x40,每列有14个矩形,总共25列。 This covers a game area of 560x1000. 这涵盖了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? 或者任何相对简单的方法我可以确保这个有效吗?

Thank you very much. 非常感谢你。

First, make a 1x1 pixel texture of white for the rectangle: 首先,为矩形制作1x1像素的白色纹理:

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 . 现在,您需要渲染矩形 - 假设Rectangle被称为rectangle For a rendering a filled block, it is very simple - make sure to set the tint Color to be the colour you want. 对于渲染填充块,它非常简单 - 确保将色调Color设置为所需的颜色。 Just use this code: 只需使用此代码:

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

For a border, is it more complex. 对于边界,它是否更复杂。 You have to draw the 4 lines that make up the outline (the rectangle here is r ): 你必须绘制构成轮廓的4条线(这里的矩形是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! 希望能帮助到你!

This worked perfect if you want to draw rectangles over your existing textures. 如果要在现有纹理上绘制矩形,这非常有用。 Great when you want to test/see for collisions 想要测试/查看碰撞时非常棒

http://bluelinegamestudios.com/blog/posts/drawing-a-hollow-rectangle-border-in-xna-4-0/ http://bluelinegamestudios.com/blog/posts/drawing-a-hollow-rectangle-border-in-xna-4-0/

-----From Site----- -----来自网站-----

The basic trick to drawing shapes is to make a single-pixel texture which is White, which you can then mix with other colors and display in solid shapes. 绘制形状的基本技巧是制作一个白色的单像素纹理,然后可以将其与其他颜色混合并以实心形状显示。

// At the top of your class:
Texture2D pixel;

// Somewhere in your LoadContent() method:
pixel = new Texture2D(GameBase.GraphicsDevice, 1, 1, false, SurfaceFormat.Color);
pixel.SetData(new[] { Color.White }); // so that we can draw whatever color we want on top of it

Then in your Draw() method do something like: 然后在你的Draw()方法中执行以下操作:

spriteBatch.Begin();

// Create any rectangle you want. Here we'll use the TitleSafeArea for fun.
Rectangle titleSafeRectangle = GraphicsDevice.Viewport.TitleSafeArea;

// Call our method (also defined in this blog-post)
DrawBorder(titleSafeRectangle, 5, Color.Red);

spriteBatch.End();

And the actual method that does the drawing: 以及绘图的实际方法:

private void DrawBorder(Rectangle rectangleToDraw, int thicknessOfBorder, Color borderColor)
{
    // Draw top line
    spriteBatch.Draw(pixel, new Rectangle(rectangleToDraw.X, rectangleToDraw.Y, rectangleToDraw.Width, thicknessOfBorder), borderColor);

    // Draw left line
    spriteBatch.Draw(pixel, new Rectangle(rectangleToDraw.X, rectangleToDraw.Y, thicknessOfBorder, rectangleToDraw.Height), borderColor);

    // Draw right line
    spriteBatch.Draw(pixel, new Rectangle((rectangleToDraw.X + rectangleToDraw.Width - thicknessOfBorder),
                                    rectangleToDraw.Y,
                                    thicknessOfBorder,
                                    rectangleToDraw.Height), borderColor);
    // Draw bottom line
    spriteBatch.Draw(pixel, new Rectangle(rectangleToDraw.X,
                                    rectangleToDraw.Y + rectangleToDraw.Height - thicknessOfBorder,
                                    rectangleToDraw.Width,
                                    thicknessOfBorder), borderColor);
}

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

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