C#使用带有类方法的延迟计时器形成,,,
我是该网站的新手,我是C#的业余程序员。我正在尝试C#形式的克伦威尔生活游戏程序,我遇到了计时器延迟问题,它会延迟程序,但不会通过代码来显示新的网格。任何帮助都将不胜感激C#使用带有类方法的延迟计时器形成,,,,c#,winforms,C#,Winforms,我是该网站的新手,我是C#的业余程序员。我正在尝试C#形式的克伦威尔生活游戏程序,我遇到了计时器延迟问题,它会延迟程序,但不会通过代码来显示新的网格。任何帮助都将不胜感激 private void button1_Click(object sender, EventArgs e) { timer1.Interval = Convert.ToInt32(1000 / hScrollBar1.Value); timer1.Start(); }
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;
}
}
classmygrid
{
//可验证
int[,]gen;
int[,]lastGen;
int-genNumber;
整数网格高度;
整数网格宽度;
Panel myPanel=默认值(Panel);
私人小组委员会;
专用文本框genBox;
公共整数
{
获取{返回genNumber;}
}
公共MyGrid(面板panel2,int[,]gridTag,TextBox-uiGenTextBox)
{
gen=(int[,])gridTag.Clone();
genNumber=0;
gridWidth=gen.GetLength(1);
gridHeight=gen.GetLength(0);
lastGen=新整数[gridHeight,gridWidth];
mPanel=面板2;
genBox=uiGenTextBox;
}
私有整数网格规则(整数x,整数y)
{
整数计数=0;
//检查x-1,y-1
如果(x>0&&y>0)
{
if(gen[y-1,x-1]==1)
计数++;
}
//检查x,y-1
如果(y>0)
{
if(gen[y-1,x]==1)
计数++;
}
//检查x+1,y-1
如果(x0)
{
if(gen[y-1,x+1]==1)
计数++;
}
//检查x-1,y
如果(x>0)
{
if(gen[y,x-1]==1)
计数++;
}
//检查x+1,y
if(x0&&y
}你不是有无限循环吗?您正在每个间隔上调用timer.Start()。是冷冻的吗使用后台工作程序?我需要它在一个循环中运行流程、规则和类,但我需要它在每次通过循环时更新网格,如果我使用带有线程的while循环,睡眠它会按照我需要的方式工作,但它会阻塞UI,所以我有点卡住。另外,你是说康威的生活游戏,或者这是我没听说过的变体?您不需要一直在
timer1\u勾选中调用timer1.Start()
。此外,确保在该循环中执行的任何操作都不会超过计时器间隔。最后,您确定要在每个计时器滴答声中创建并初始化一个新网格吗?我同意Chris,tim的观点