I've worked out most of the code and have several game classes. T开发者_如何学Pythonhe one bit I'm stuck on at the moment, it how to draw the actual Connect 4 grid. Can anyone tell me what's wrong with this for
loop? I get no errors but the grid doesn't appear. I'm using 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();
}
The code in Drawgrid()
needs to be executed when the window is redrawing itself.
The Invalidate()
call tells the application that it needs to redraw the window contents (it triggers a redraw of your window). This code (with the exception of the Invalidate()
call) should be in your overridden OnPaint()
method, otherwise whatever gets drawn by this code will be immediately overwritten by the default drawing code in OnPaint()
(which, by default, will probably draw a white background) when you make the Invalidate()
call.
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
// (your painting code here...)
}
精彩评论