【发布时间】:2014-03-20 14:17:20
【问题描述】:
我已经完成了一个生命游戏的实现,但是在应用游戏规则后渲染网格时遇到了问题。我有一个看起来像这样的游戏循环:
while (gameIsRunning)
{
//Needed for accessing UIControls from the background
//thread.
if (InvokeRequired)
{
//Process the array.
MainBoard.Cells = engine.ApplyGameRules(MainBoard.Cells, MainBoard.Size.Height, MainBoard.Size.Width, BOARD_DIMENSIONS);
//Check if there is a state such as
//all states being dead, or all states being
//alive.
//Update the grid with the updated cells.
this.Invoke(new MethodInvoker(delegate
{
timeCounter++;
lblTimeState.Text = timeCounter.ToString();
pictureBox1.Invalidate();
pictureBox1.Update();
Thread.Sleep(100);
}));
}
}
还有一个如下所示的绘图函数:
for (int x = 0; x < MainBoard.Size.Height; x++)
{
for (int y = 0; y < MainBoard.Size.Width; y++)
{
Cell individualCell = MainBoard.Cells[y, x];
if (individualCell.IsAlive() == false)
{
e.Graphics.FillRectangle(Brushes.Red, MainBoard.Cells[y, x].Bounds);
}
//White indicates that cells are alive
else if (individualCell.IsAlive() == true)
{
e.Graphics.FillRectangle(Brushes.White, MainBoard.Cells[y, x].Bounds);
}
else if (individualCell.IsInfected() == true)
{
e.Graphics.FillRectangle(Brushes.Green, MainBoard.Cells[y, x].Bounds);
}
//Draws the grid background itself.
e.Graphics.DrawRectangle(Pens.Black, MainBoard.Cells[y, x].Bounds);
}
}
我遇到的问题是我将所有游戏规则应用于网格中的每个单元格,然后绘制该网格,然后再次应用所有规则,所以我永远不会得到我想要的生命形式 blob应该看到。游戏规则是否应该逐个单元格地应用,以使其类似于以下内容:将游戏规则应用于单元格,绘制网格,将游戏规则应用于另一个单元格,绘制网格......?
【问题讨论】:
-
据我了解,您将规则应用于整个网格,然后重绘。所以你需要将网格的状态保持原样,然后根据original网格中的情况更新一个copy。
-
好的,我会尝试类似的方法,我记得在某处读到,在显示网格时必须使用副本,并且我使用原件来进行规则应用和渲染。