4

I am developing a tetris like game in winforms(C# and .netframework 2.0). The winform has an background image and a picture-box which move down(new Location is assigned) at an interval of 500ms.

The problem is when picture-box moves down the background image of form flickers at the point where the picture-box was earlier located. If i don't use any background image, then there is no flickering.

Is there any graphics accelerator or any kind of solution using which the flickering problem can be solved.

Bart
  • 19,692
  • 7
  • 68
  • 77
Ravi Jain
  • 1,452
  • 2
  • 17
  • 41

3 Answers3

3

Set DoubleBuffered = true on your controls. This should help prevent flickering.

For documentation on the DoubleBuffered property, see here:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.doublebuffered.aspx

Botz3000
  • 39,020
  • 8
  • 103
  • 127
3

The term used for this is Double Buffering. The idea is simple - start with 2 panes but only display one. Draw to a hidden pane, and quickly swap the hidden and visible panes. Rinse and repeat for every transition (animation).

Luckily, you dont have to deal with the nuts and bolts of this in .NET, controls do it for you. This SO question will help you: How to double buffer .NET controls on a form?

Community
  • 1
  • 1
Jamiec
  • 133,658
  • 13
  • 134
  • 193
3

If you are developing a game in Windows Forms you really ought to be overriding OnPaint and implementing painting of the sprites on every frame, as opposed to moving heavyweight PictureBox controls.

You will find you may get the flicker regardless of DoubleBuffer if you use the approach you mentioned. However, with all drawing done in OnPaint, then DoubleBuffer begins to work.

I did a quick search and found this interesting article on creating a game-loop in Windows Forms using OnPaint override. In particular take a look at GameStateDrawer which does the rendering directly to graphics context.

Dr. Andrew Burnett-Thompson
  • 20,980
  • 8
  • 88
  • 178
  • your suggestion is correct , i am still getting flickers after using double buffering.Thanks. – Ravi Jain May 24 '12 at 12:33
  • Yep :) I worked for around 5 years on WinForms graphical controls before switching to WPF. Use the OnPaint method with double-buffering. You will need to create a crude game engine to draw the sprites at specific locations. It will work! – Dr. Andrew Burnett-Thompson May 24 '12 at 13:05