我已经解决了大部分代码并且有几个游戏类。我现在坚持一点,它是如何绘制实际的Connect 4网格。谁能告诉我这个for
循环有什么问题?我没有错误,但网格没有出现。我正在使用C#。
private void Drawgrid()
{
Brush b = Brushes.Black;
Pen p = Pens.Black;
for (int xCoor = XStart, col = 0; xCoor < XStart + ColMax * DiscSpace; xCoor += DiscSpace, col++)
{
// x coordinate beginning; while the x coordinate is smaller than the max column size, times it by
// the space between each disc and then add the x coord to the disc space in order to create a new circle.
for (int yCoor = YStart, row = RowMax - 1; yCoor < YStart + RowMax * DiscScale; yCoor += DiscScale, row--)
{
switch (Grid.State[row, col])
{
case GameGrid.Gridvalues.Red:
b = Brushes.Red;
break;
case GameGrid.Gridvalues.Yellow:
b = Brushes.Yellow;
break;
case GameGrid.Gridvalues.None:
b = Brushes.Aqua;
break;
}
}
MainDisplay.DrawEllipse(p, xCoor, yCoor, 50, 50);
MainDisplay.FillEllipse(b, xCoor, yCoor, 50, 50);
}
Invalidate();
}
答案 0 :(得分:2)
当窗口重绘时,需要执行Drawgrid()
中的代码。
Invalidate()
调用告诉应用程序它需要重绘窗口内容(它会触发重绘窗口)。此代码(Invalidate()
调用除外)应该在您重写的OnPaint()
方法中,否则此代码绘制的任何内容都将被OnPaint()
中的默认绘图代码立即覆盖(当您进行Invalidate()
调用时,默认情况下可能会绘制白色背景。
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
// (your painting code here...)
}