How Best To Handle 2D Graphics

• Question

• I recently finished making a basic Tetris clone and it works fine. Only problem is there is an annoying flicker every time the screen is updated and it's given me cause to revise my paint() method which draws to the screen.
Is it perhaps purely because my paint method is too inefficient? How would others choose to output 2D graphics for simple arcade games such as Tetris or Breakout?

Currently I have a picturebox which I've set up a graphics object within to which I draw upon. I chose to do this purely 'cause this is the only form of specific graphic output I've come across in the short year that I've been learning C#. Was this wise or are there other, better methods of drawing to the screen?

Also I've been using nothing but filled rectangles to represent the squares of the game, would things maybe work more smoothly if I used small bitmaps or something instead?

Here's my current paint() method:

public void paint()
{
lock (this)
{

SolidBrush squarepen = null;

grphPicBoxGameArea.Clear(Color.White);
grphPicBoxNextBlock.Clear(Color.White);

//reads in the array of the pile and fills the corresponding squares of the grid
for (int i = 0; i < Pile.pile.GetLength(0); i++)
{
for (int j = 0; j < Pile.pile.GetLength(1); j++)
{
if (Pile.pile[i, j] != null)
{
squarepen = new SolidBrush(Pile.pile[i, j].getColour());
grphPicBoxGameArea.FillRectangle(squarepen, i * 30, (j * 30) - 120, 30, 30);
}
}
}
//reads in the array of the current block and fills the corresponding squares of the grid
Square[] currentblock = blockinuse.getBlockSquares();
for (int i = 0; i < currentblock.GetLength(0); i++)
{
int currentxpos = currentblock[i].getXpos(), currentypos = currentblock[i].getYpos();
squarepen = new SolidBrush(currentblock[i].getColour());

grphPicBoxGameArea.FillRectangle(squarepen, currentxpos * 30, (currentypos * 30) - 120, 30, 30);
}
}
}

Sunday, August 9, 2009 4:05 PM

• You should take advantage of the double buffering supported by controls.  To do so, you have to implement the Paint() event of those controls to do your drawing, use e.Graphics.  Force a repaint with the Invalidate() method.  It is best to not use picture boxes, just the paint event of the form.  Set the form's DoubleBuffered property to True.

Hans Passant.
Sunday, August 9, 2009 4:31 PM

All replies

• You should take advantage of the double buffering supported by controls.  To do so, you have to implement the Paint() event of those controls to do your drawing, use e.Graphics.  Force a repaint with the Invalidate() method.  It is best to not use picture boxes, just the paint event of the form.  Set the form's DoubleBuffered property to True.

Hans Passant.
Sunday, August 9, 2009 4:31 PM
• Works just dandy with no flicker now, thanks a bunch. =]
Sunday, August 9, 2009 7:02 PM