将数组中的更多值设置为 1

本文关键字:设置 数组 | 更新日期: 2023-09-27 18:34:52

class spielfeld
{
    int[,] spielfeldgitter = new int[16, 16];
    public void spielfeldnullsetzen(/*PictureBox pictureBox1*/)
    {
        for (int i = 0; i < spielfeldgitter.GetLength(0); i++)
        {
            for (int j = 0; j < spielfeldgitter.GetLength(1); j++)
            {
                spielfeldgitter[i, j] = 0;
            }
        }
    }
    public void spielfeldol(PictureBox pictureBox1)
    {
        Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
        Graphics feld = Graphics.FromImage(bmp);
        for (int i = 0; i < 16; i++)
        {
            feld.DrawLine(Pens.Black, 0, (320 / 16) * i, 320, (320 / 16) * i);
        }
        for (int i = 0; i < 16; i++)
        {
            feld.DrawLine(Pens.Black, (320 / 16) * i, 0, (320 / 16) * i, 320);
        }
        pictureBox1.Image = bmp;
    }
    public void mouseclick(int eX, int eY, PictureBox pictureBox1)
    {
        int cellw = (eX / 20);
        int cellh = (eY / 20);
        if (spielfeldgitter[cellw, cellh] != 1)
        {
            spielfeldgitter[cellw, cellh] = 1;
            Graphics rectangle = Graphics.FromImage(pictureBox1.Image);
            rectangle.FillRectangle(Brushes.Green, ((320 / 16) * cellw + 1), (320 / 16) * cellh + 1, (320 / 16) - 1, (320 / 16) - 1);
        }
        else
        {
            spielfeldgitter[cellw, cellh] = 0;
            Graphics rectangle = Graphics.FromImage(pictureBox1.Image);
            rectangle.FillRectangle(Brushes.White, ((320 / 16) * cellw + 1), (320 / 16) * cellh + 1, (320 / 16) - 1, (320 / 16) - 1);
        }
    }
}

所以它是 src 的片段,其中包含我坚持的逻辑。

    private void Form1_Load(object sender, EventArgs e)
    {   
        Feld.spielfeldnullsetzen(/*pictureBox1*/);       
        Feld.spielfeldol(pictureBox1);
    }
    private void pictureBox1_Click(object sender, EventArgs e)
    {
        Feld.spielfeldol(pictureBox1);
    }
    private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
    {
        int eX = e.X;
        int eY = e.Y;
        Feld.spielfeldol(pictureBox1);
        Feld.mouseclick(eX, eY, pictureBox1);
    }

}

这就是形式.cs 我的问题是我正在创建一个 16,16 大的 2D 数组,并将每个值设置为 0。断点在我检查时说同样的事情。

然后当我在鼠标单击时做断点特定的数组值和矩形的颜色也会发生变化。但是我无法用另一种颜色制作多个字段

也许有人可以帮忙?

将数组中的更多值设置为 1

public partial class Form1 : Form
{
    spielfeld Feld = new spielfeld();
    public Form1()
    {
        InitializeComponent();
        Load += Form1_Load;
        //pictureBox1.Click += pictureBox1_Click;
        pictureBox1.MouseClick += pictureBox1_MouseClick;
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        Feld.spielfeldnullsetzen();
        Feld.spielfeldol(pictureBox1);
    }
    //private void pictureBox1_Click(object sender, EventArgs e)
    //{
    //    Feld.spielfeldol(pictureBox1);
    //}
    private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
    {
        int eX = e.X;
        int eY = e.Y;
        //Feld.spielfeldol(pictureBox1);
        Feld.mouseclick(eX, eY, pictureBox1);
    }
}

与本课程一起

class spielfeld
{
    int[,] spielfeldgitter = new int[16, 16];
    public void spielfeldnullsetzen(/*PictureBox pictureBox1*/)
    {
        for (int i = 0; i < spielfeldgitter.GetLength(0); i++)
        {
            for (int j = 0; j < spielfeldgitter.GetLength(1); j++)
            {
                spielfeldgitter[i, j] = 0;
            }
        }
    }
    public void spielfeldol(PictureBox pictureBox1)
    {
        Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
        Graphics feld = Graphics.FromImage(bmp);
        for (int i = 0; i < 16; i++)
        {
            feld.DrawLine(Pens.Black, 0, (320 / 16) * i, 320, (320 / 16) * i);
        }
        for (int i = 0; i < 16; i++)
        {
            feld.DrawLine(Pens.Black, (320 / 16) * i, 0, (320 / 16) * i, 320);
        }
        pictureBox1.Image = bmp;
    }
    public void mouseclick(int eX, int eY, PictureBox pictureBox1)
    {
        int cellw = (eX / 20);
        int cellh = (eY / 20);
        if (spielfeldgitter[cellw, cellh] != 1)
        {
            spielfeldgitter[cellw, cellh] = 1;
            Graphics rectangle = Graphics.FromImage(pictureBox1.Image);
            rectangle.FillRectangle(Brushes.Green, ((320 / 16) * cellw + 1), (320 / 16) * cellh + 1, (320 / 16) - 1, (320 / 16) - 1);
        }
        else
        {
            spielfeldgitter[cellw, cellh] = 0;
            Graphics rectangle = Graphics.FromImage(pictureBox1.Image);
            rectangle.FillRectangle(Brushes.White, ((320 / 16) * cellw + 1), (320 / 16) * cellh + 1, (320 / 16) - 1, (320 / 16) - 1);
        }
        pictureBox1.Refresh();
    }
}

应该让你开始。

对原始代码的更改:

  1. pictureBox1.Refresh()板鼠标点击法
  2. 表单中的更改放在注释中

这是因为spielfeldol清除所有图像,而mouseclick在先前清除的图像中只绘制一个矩形。因此,有几个选项可以解决此问题:

  1. 从事件处理程序中删除spielfeldol方法调用pictureBox1_MouseClick并添加某种按钮来调用spielfeldol用于清除图像的方法。
  2. spielfeldol方法重用pictureBox1.Image(不要创建新的位图 - 它将为空(,就像在"鼠标单击"方法中一样。