简体   繁体   English

pictureBox1_MouseClick事件:阵列/正方形(生命游戏)

[英]pictureBox1_MouseClick event: Arrays/Squares (Game of life)

I am experimenting and learning C# by coding the game of LIFE. 我正在通过编写LIFE游戏来试验和学习C#。 Currently I have a pictureBox and a function drawGrid that creates a grid overlay. 目前我有一个pictureBox和一个创建网格覆盖的函数drawGrid In order to click in each cell inside the pictureBox I have implemented the property pictureBox1_MouseClick in which there is an if decision logic to know if a cell is selected or not. 为了点击pictureBox中的每个单元格,我已经实现了属性pictureBox1_MouseClick ,其中有一个if决策逻辑来知道是否选择了一个单元格。 The issues I am running into comes when i click the squares quickly i get an Error: System.IndexOutOfRangeException which points to fill_in[x, y] = !fill_in[x, y]; 我遇到的问题是当我快速点击方块时出现错误: System.IndexOutOfRangeException指向fill_in[x, y] = !fill_in[x, y]; .

How can I increase the clicking accuracy of the pictureBox1_MouseClick event so I dont get that error? 如何提高pictureBox1_MouseClick事件的点击准确性,以便我不会收到该错误?

Specific error: 具体错误:

`An unhandled exception of type 'System.IndexOutOfRangeException' occurred in life.exe `life.exe中发生了'System.IndexOutOfRangeException'类型的未处理异常

Additional information: Index was outside the bounds of the array.` 附加信息:索引超出了数组的范围

Code

namespace life
{
    public partial class Form1 : Form
    {

        Graphics paper;
        bool[,] fill_in = new bool[450, 450];
    int cellSize = 10;


 private void drawGrid()
        {
            int numOfCells = 100;        
            Pen p = new Pen(Color.Blue);
            paper.Clear(Color.White);

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


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

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

            if (fill_in[x, y])
            {
                // Fill grid square with the filled color
                paper.FillRectangle(Brushes.Red, x, y, 10, 10);
            }
            else
            {
                // Fill grid square with unfilled color 
                paper.FillRectangle(Brushes.White, x, y, 10, 10);          
            }

        }

     }
}

Please make sure your calculations return desired values: 请确保您的计算返回所需的值:

int x = cellSize * (e.X / cellSize);
int y = cellSize * (e.Y / cellSize);

In this scenario x and y can be outside the boundaries of your array. 在这种情况下,x和y可以在数组的边界之外。 Example: cellsize * (450 / cellsize) equals 450 and this causes the error since allowed indices range from 0 to 449. 示例:cellsize *(450 / cellsize)等于450,这会导致错误,因为允许的索引范围为0到449。

EDIT: 编辑:

To fix this problem (and this is a really dirty and temporary fix ), please change your code as follows: 要解决此问题(这是一个非常脏的临时修复 ),请更改您的代码,如下所示:

int x = cellSize * (e.X / cellSize);
int y = cellSize * (e.Y / cellSize);

x = x >= 450 ? 450 - 1 : x;
y = y >= 450 ? 450 - 1 : y;

More desirable and actually a lot smarter solution would be simply to adjust your table's size to be exactly the same as the range of eX and eY 更理想,实际上更聪明的解决方案就是将表的大小调整为与eX和eY的范围完全相同

Hope this helps, Piotr 希望这有帮助,彼得

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM