[英]Two-Dimensional Array Rectangle in C#
I need to create a new rectangle using two dimensional arrays in C#. 我需要使用C#中的二维数组创建一个新的矩形。
I created an image called brick_fw and imported it into the IDE for the game I'm creating. 我创建了一个名为brick_fw的映像,并将其导入到要创建的游戏的IDE中。
Here's the code I have (which is in it's own class named Brick): 这是我的代码(在它自己的名为Brick的类中):
brickLength = 60;
brickHeight = 20;
int[,] brickLocation = { { 0, 0 }, { 62, 0 }, { 123, 0 }, { 184, 0 }, { 245, 0 }, { 306, 0 }, { 367, 0 } };
bool[] brickLive = { true, true, true, true, true, true, true };
brickImage = Breakout.Properties.Resources.brick_fw;
brickRec = new Rectangle(x, y, brickLength, brickHeight);
The problem I have here is; 我这里的问题是; brickRec
can only use the integer values of x and y and only accepts this way of representing a new rectangle (meaning if I remove the x and y from the brackets and replace it with brickLocation
then the compiler will moan). brickRec
只能使用x和y的整数值,并且只接受这种表示新矩形的方式(这意味着如果我从括号中删除x和y并用brickLocation
替换,则编译器会brickLocation
)。
As it stands, the compiler will only draw a single brick into the program because it's not taking into account the two dimensional array. 就目前而言,编译器只会在程序中绘制单个积木,因为它没有考虑二维数组。 Is there a way to represent this brickLocation
within the Rectangle function? 有没有办法在Rectangle函数中表示这个brickLocation
?
EDIT: 编辑:
public Brick()
{
brickLength = 60;
brickHeight = 20;
int[,] brickLocation = { { 0, 0 }, { 62, 0 }, { 123, 0 }, { 184, 0 }, { 245, 0 }, { 306, 0 }, { 367, 0 } };
bool[] brickLive = { true, true, true, true, true, true, true };
brickImage = Breakout.Properties.Resources.brick_fw;
for (int i = 0; i < brickLocation.GetLength(0); i++)
{
brickRec = new Rectangle(brickLocation[i, 0], brickLocation[i, 1], brickLength, brickHeight);
}
}
public void drawBrick(Graphics paper)
{
paper.DrawImage(brickImage, brickRec);
}
A Rectangle
value can only represent a single rectangle, so if you want multiple rectangles you would loop through one dimension of the array and create a rectangle for each pair of values: 一个Rectangle
值只能代表一个矩形,因此,如果要多个矩形,可以遍历数组的一个维度,并为每对值创建一个矩形:
for (int i = 0; i < brickLocation.GetLength(0); i++) {
brickRec = new Rectangle(brickLocation[i, 0], brickLocation[i, 1], brickLength, brickHeight);
// draw the rectangle
}
To do the drawing of the rectangles separate from the creation of the rectangles, you would put them in an array of rectangles: 为了将矩形的绘制与矩形的创建分开进行,您可以将它们放在矩形数组中:
private Rectangle[] brickRec;
public Brick()
{
brickLength = 60;
brickHeight = 20;
int[,] brickLocation = { { 0, 0 }, { 62, 0 }, { 123, 0 }, { 184, 0 }, { 245, 0 }, { 306, 0 }, { 367, 0 } };
bool[] brickLive = { true, true, true, true, true, true, true };
brickImage = Breakout.Properties.Resources.brick_fw;
brickRec = new Rectangle[brickLocation.GetLength(0)];
for (int i = 0; i < brickLocation.GetLength(0); i++)
{
brickRec[i] = new Rectangle(brickLocation[i, 0], brickLocation[i, 1], brickLength, brickHeight);
}
}
public void drawBrick(Graphics paper)
{
for (int i = 0; i < brickRec.Length; i++) {
paper.DrawImage(brickImage, brickRec[i]);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.