将图像添加到矩形数组 - C#

本文关键字:数组 图像 添加 | 更新日期: 2023-09-27 18:31:47

我有以下代码,想知道是否有办法将brickImage属性分配给代码中的数组。如果是这样,我该如何实现?我基本上想创建一个要在多行等中显示的砖块数组。

public class Brick
    {
        private int x, y, width, height;
        private Image brickImage;
        private Rectangle brickRec;
        private Rectangle[] brickRecs;
        public Rectangle BrickRec
        {
            get { return brickRec; }
        }

        public Brick()
        {
            x = 0;
            y = 0;
            width = 60;
            height = 20;

            brickImage = Breakout.Properties.Resources.brick_fw;
            brickRec = new Rectangle(x, y, width, height);
            Rectangle[] brickRecs =
            {
                new Rectangle(0, 0, 60, 20),
                new Rectangle(0, 0, 121, 20),
                new Rectangle(0, 0, 242, 20)
            };
        }

        public void drawBrick(Graphics paper)
        {
            paper.DrawImage(brickImage, brickRec);
            //paper.DrawImage(brickImage, brickRecs);
        }
    }

将图像添加到矩形数组 - C#

如果你想将图像附加到矩形,只需创建你自己的类

public class MyBrick
{
   public Image Image {get; set;}
   public Point[] Locations {get; set;}
}

然后将其添加到代码中的某个位置

MyBrick[] brickRecs  = 
{
   new MyBrick()
   {
       Locations = 
       {
          new Point(60, 20),
          new Point(10, 10),
          //....
       },
       Image = ... // ADD IMAGE REFERENCE HERE
   },
   new MyBrick()
   {
       Locations = 
       {
          new Point(90, 90),
          new Point(5, 5),
          //....
       },
       Image = ... 
   },
};