Я закончил реализацию игры «Жизнь», но столкнулся с проблемой при рендеринге сетки после применения правил игры. У меня есть игровой цикл, который выглядит так:
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);
}
}
Проблема, с которой я сталкиваюсь, заключается в том, что я применяю все правила игры к каждой ячейке в сетке, а затем рисую эту сетку, а затем снова применяю все правила, поэтому я никогда не получаю капли форм жизни, которые я должен видеть. . Должны ли правила игры применяться к ячейке за ячейкой, чтобы это было примерно так: применить правило игры к ячейке, нарисовать сетку, применить правило игры к другой ячейке, нарисовать сетку...?