绘制矩形多维数组 [英] Drawing a rectangle multidimensional array
问题描述
我目前正在使用库存系统,但是在确定应如何绘制时遇到了问题。
I'm currently working on an inventory system however I'm having problem figuring out how I should draw it.
我有一个矩形数组,看起来像这样:
I have an array of rectangles looking like this:
Rectangle[] Inventoryslots = new Rectangle[24]; // 24 slots
现在我要绘制6 * 4列的插槽,其中6个插槽宽度和4个高度的插槽。
now I want to draw the slots like a 6*4 columns, 6 slots in width and 4 slots in height.
我要像这样绘制它们,直到弄清楚如何在y上绘制它们:
I'm drawing them like this until I have figured out how I should draw them on y as well:
for (int i = 0; i < Inventoryslots.Length; i++)
{
Inventoryslots[i] = new Rectangle(i * 33, 0, box.Width, box.Height);
spriteBatch.Draw(box, Inventoryslots[i], Color.White);
}
所以当时,我想以某种方式向下移动y33 [ i]
达到6并重置x位置。
So somehow I want to move y33 down when [i]
reaches 6 and also reset x position.
我敢肯定这很简单,但是我无法弄清楚帮助会为我节省很多时间。
I'm sure it's pretty simple but I just can't figure it out so any help would save me a lot of time.
推荐答案
首先从创建多维数组和常量开始
first start by creating a multidimensional array and constant
const int offset = 100;
Rectangle[,] Inventoryslots = new Rectangle[6, 4];
然后您将使用双嵌套的for循环进行初始化
then you'll initialize with a double nested for loop
for (int x = 0; x < 6; x++)
{
for (int y = 0; y < 4; y++)
{
Inventoryslots[x, y] = new Rectangle((x * Width) + offset,
(y * Height) + offset, Width, Height);
}
}
然后,您将进行两次嵌套的for循环遍历它们
then you'll do a double nested for loop to iterate through them
for (int x = 0; x < 6; x++)
{
for (int y = 0; y < 4; y++)
{
spritebatch.draw(texture, Inventoryslots[x, y], Color.White);
}
}
至少我认为这就是您要的内容,让我知道这是如何工作的。该常数可用于移动整个矩形数组(如果要单独操纵X和Y,请使用vector2)
At least I think that's what you're asking, let me know how this works. The constant can be used to move the whole array of rectangles around (use a vector2 if you want to manipulate X and Y individually)
这篇关于绘制矩形多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!