Displaying rectangles in game window with XNA(使用 XNA 在游戏窗口中显示矩形)
问题描述
我想将我的游戏网格划分为一个矩形数组.每个矩形为 40x40,每列有 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 });
现在,您需要渲染矩形 - 假设矩形被称为 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
希望对你有帮助!
这篇关于使用 XNA 在游戏窗口中显示矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 XNA 在游戏窗口中显示矩形
基础教程推荐
- 在 VB6 或经典 ASP 中使用 .NET 2022-01-01
- C# 9 新特性——record的相关总结 2023-04-03
- 重新排序 WPF TabControl 中的选项卡 2022-01-01
- 从 C# 控制相机设备 2022-01-01
- 获取C#保存对话框的文件路径 2022-01-01
- 更新 Visual Studio 中的 DataSet 结构以匹配新的 SQL 数据库结构 2022-01-01
- Mono https webrequest 失败并显示“身份验证或解密失败" 2022-01-01
- SonarQube C# 分析失败“不是指针的有效行偏移" 2022-01-01
- 如果条件可以为空 2022-01-01
- 将数据集转换为列表 2022-01-01
