PictureBox-网格中的网格和填充(生命游戏)

时间:2012-10-04 22:45:42

标签: c#

我正在通过实施生命游戏的副本来学习C#。我已经能够使用for循环在pictureBox上成功绘制网格。名为fill_in的bool是填充正方形。我正在请求帮助使广场可点击。我已将pictureBox的propertyite属性设置为允许pictureBox_MouseClick。在mouseClick事件的旁边,我设置了坐标xy。问题是该事件中的if语句不正确,因为==无法应用于bool操作数。

如果bool fill_in为真,我怎么能用if条件语句填充黑色?

代码

  namespace life
    {
        public partial class Form1 : Form
        {

            Graphics paper;
            bool[,] fill_in = new bool[450, 450];

        public Form1()
            {
                InitializeComponent();
                paper = pictureBox1.CreateGraphics();

            }


    //makes grid in picture box
    private void drawGrid()
            {
                int numOfCells = 100;
                int cellSize = 10;
                Pen p = new Pen(Color.Blue);
                paper.Clear(Color.White);

                for (int i = 0; i < numOfCells; i++)
                {   
                    // Vertical
                    paper.DrawLine(p, i * cellSize, 0, i * cellSize, numOfCells * cellSize);
                    // Horizontal
                    paper.DrawLine(p, 0, i * cellSize, numOfCells * cellSize, i * cellSize);
                }
            }


    // populate bool fill_in with true (alive) or false (dead)
            private void clearGrid()
            {
                for (int x = 0; x < 450; x = x + 10)
                {
                    for (int y = 0; y < 450; y = y + 10)
                    {
                        fill_in[x, y] = false;
                    }
                }
            }

     private void button1_Click(object sender, EventArgs e)
            {
                drawGrid();
                clearGrid();

                for (int x = 0; x < 440; x = x + 10)
                {
                    for (int y = 0; y < 440; y = y + 10)
                    {
                        if (fill_in[x, y] == true)
                            paper.FillRectangle(Brushes.Black, x, y, 10, 10);
                    }
                }
            }

     private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
            {

                int x = e.X;
                int y = e.Y;
                int i = x / 10;
                int j = y / 10;


                fill_in[i, j] = !fill_in[i, j];

                if (fill_in[i, j])
                {

                 paper.FillRectangle(Brushes.Black, x, y, 10, 10);
                }
                else
                {

                paper.FillRectangle(Brushes.White, x, y, 10, 10);       

                }

                }

                }
           }

更改if语句后:

enter image description here

1 个答案:

答案 0 :(得分:3)

if (fill_in == true)中的pictureBox1_MouseClick缺少数组下标(fill_in是一个数组,还记得吗?)。

但没关系;您已经知道fill_in[i, j]是正确的,因为您只是这样做了,所以您可以完全删除if

如果您想在第一次点击时填写它并取消填充(将其恢复为“未填充”),您可以稍微更改一下事件处理程序:

private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
  int x = e.X;
  int y = e.Y;
  int i = x / 10;
  int j = y / 10;

  // Reverse the value of fill_in[i, j] - if it was false, change to true,
  // and if true change to false
  fill_in[i, j] = !fill_in[i, j];

  if (fill_in[i, j])
  {
    // Fill grid square with the filled color
  }
  else
  {
    // Fill grid square with unfilled color            
  }
}