使用带类方法的延迟计时器的C#表单,,,

时间:2016-02-10 00:31:35

标签: c# winforms

我是网站的新手,我是C#的业余编码员我正在以C#形式尝试Cromwell生命游戏程序,我遇到了计时器延迟的问题,它会延迟程序,但它不会浏览代码以显示新网格。任何帮助将不胜感激。

    private void button1_Click(object sender, EventArgs e)
    {
        timer1.Interval = Convert.ToInt32(1000 / hScrollBar1.Value);
        timer1.Start();

    }
    public void timer1_Tick(object sender, EventArgs e)
    {
        MyGrid MyGrid = new MyGrid(panel2, gridTag, uiGenTextBox);
        MyGrid.Update();
        FirstState();
        if (MyGrid.Survived() == 0)
        {
            MessageBox.Show("No survivers!");
            timer1.Stop();
        }

    }

班级:抱歉,我知道有很多代码

      class MyGrid
      {
       // veriables
       int[,] gen;
       int[,] lastGen;
       int genNumber;
       int gridHeight;
       int gridWidth;
       Panel myPanel = default(Panel);
       private Panel mPanel;
       private TextBox genBox;

    public int GenNumber
    {
        get { return genNumber; }
    }


    public MyGrid(Panel panel2, int[,] gridTag, TextBox uiGenTextBox)
    {
        gen = (int[,])gridTag.Clone();
        genNumber = 0;
        gridWidth = gen.GetLength(1);
        gridHeight = gen.GetLength(0);
        lastGen = new int[gridHeight, gridWidth];
        mPanel = panel2;
        genBox = uiGenTextBox;
    }
    private int GridRule(int x, int y)
    {
        int count = 0;

        // Check for x - 1, y - 1
        if (x > 0 && y > 0)
        {
            if (gen[y - 1, x - 1] == 1)
                count++;
        }

        // Check for x, y - 1
        if (y > 0)
        {
            if (gen[y - 1, x] == 1)
                count++;
        }

        // Check for x + 1, y - 1
        if (x < gridWidth - 1 && y > 0)
        {
            if (gen[y - 1, x + 1] == 1)
                count++;
        }

        // Check for x - 1, y
        if (x > 0)
        {
            if (gen[y, x - 1] == 1)
                count++;
        }

        // Check for x + 1, y
        if (x < gridWidth - 1)
        {
            if (gen[y, x + 1] == 1)
                count++;
        }

        // Check for x - 1, y + 1
        if (x > 0 && y < gridHeight - 1)
        {
            if (gen[y + 1, x - 1] == 1)
                count++;
        }

        // Check for x, y + 1
        if (y < gridHeight - 1)
        {
            if (gen[y + 1, x] == 1)
                count++;
        }

        // Check for x + 1, y + 1
        if (x < gridWidth - 1 && y < gridHeight - 1)
        {
            if (gen[y + 1, x + 1] == 1)
                count++;
        }

        return count;
    }
    public void GenSort()
    {
        int[,] nextGen = new int[gridHeight, gridWidth];
        lastGen = (int[,])gen.Clone();
        genNumber++;

        for (int heightLoop = 0; heightLoop < gridHeight; heightLoop++)
        {
            for (int widthLoop = 0; widthLoop < gridWidth; widthLoop++)
            {
                if (GridRule(widthLoop, heightLoop) < 2)
                    nextGen[heightLoop, widthLoop] = 0;
                else if (gen[heightLoop, widthLoop] == 0 &&                  GridRule(widthLoop, heightLoop) == 3)
                    nextGen[heightLoop, widthLoop] = 1;
                else if (gen[heightLoop, widthLoop] == 1 && (GridRule(widthLoop, heightLoop) == 2 || GridRule(widthLoop, heightLoop) == 3))
                    nextGen[heightLoop, widthLoop] = 1;
                else
                    nextGen[heightLoop, widthLoop] = 0;
            }
        }
        gen = (int[,])nextGen.Clone();
    }

    public void Panel_Click2(System.Object sender, System.EventArgs e)
    {
            Panel myPanel = ((Panel)sender);
            int locX = myPanel.Location.X / myPanel.Width;
            int locY = myPanel.Location.Y / myPanel.Height;
            if (myPanel.BackColor == Color.Blue)
            {
                myPanel.BackColor = Color.White;
                gen[locX, locY] = 1;
            }
            else if (myPanel.BackColor == Color.White)
            {
                myPanel.BackColor = Color.Blue;
                myPanel.Tag = 1;
                gen[locX, locY] = 1;
            }
    }

    public void DrawGen()
    {
        int tempValue;
        int tempValue2;
        foreach (Control myPanel in Panel2.Controls)
        {
            int locX = myPanel.Location.X / myPanel.Width;
            int locY = myPanel.Location.Y / myPanel.Height;
            tempValue = gen[locY, locX];
            tempValue2 = lastGen[locY, locX];

            if (tempValue == 0)
            {
                myPanel.BackColor = Color.Blue;
            }
            else
            {   
                myPanel.BackColor = Color.White;  
            }
            if (tempValue2 == 1 && tempValue == 0)
            {
               myPanel.BackColor = Color.Crimson;

            }
        }
    }

    public void Update()
    {
        GenSort();
        DrawGen();
        Write();
    }
    public Panel Panel
    {
        get { return (this.myPanel); }
    }
    public Panel Panel2
    {
        get { return (this.mPanel); }
    }
    public TextBox uiGenTextBox
    {
        get { return (this.genBox); }
    }
    public int Survived()
    {
        int count = 0;

        for (int heightLoop = 0; heightLoop < gridHeight; heightLoop++)
            for (int widthLoop = 0; widthLoop < gridWidth; widthLoop++)
                if (gen[heightLoop, widthLoop] == 1)
                    count++;

        return count;
    }
}

}

0 个答案:

没有答案