Click here to Skip to main content
15,883,901 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My timer interval is set to 50 milliseconds; the snake moves quickly and smoothly, but it tends to flicker. I am not sure what would help you most, so here are my drawing methods and one MoveSnake() meethod:

<pre lang="cs">public void DrawSnake(Graphics g)
{
    foreach (Rectangle rec in mySnake)
    {
        g.FillRectangle(myBrush, rec);
    }
}

public void DrawSnake()
{
    for (int i = mySnake.Length - 1; i > 0; i--)
    {
        mySnake[i] = mySnake[i - 1];
    }
}

public void MoveDown()
{
    DrawSnake();
    mySnake[0].Y += 16;
}

Posted

That's going to depend on a number of things. If your screen is small, then you may be able just to set DoubleBuffered on your Form properties, and get away with it - if you don't get away with it, then the movement will all slow down.

If that happens, then it gets a little more complex:
Override OnPaintBackground, and don't do it, if this is a timed paint. (if it is the result of a normal move, re-size or whatever, you must erase the background.
protected override void OnPaintBackground(PaintEventArgs e)
    {
    if (!timedPainting)
        {
        // Only do the background if not drawing our screen
        base.OnPaintBackground(e);
        }
    timedPainting = false;
    }
Then you must erase the tail block yourself.

Sounds simple? It is, sort of. It's just a lot of work to get right.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 25-Jul-11 17:07pm    
Correct is this is a Form (my 5). Chances are, this is some other control, so my recipe is needed.
Please see my more general solution.
--SA
I don't know what control do you use to render your animated graphics. If this is Form, you can use the property System.Windows.Forms.Form.DoubleBuffered and do it as Griff suggested. Very often this is some custom control of just panel.

To do animation, you need to access System.Windows.Forms.Control.SetStyle. You need to include the following styles: System.Windows.Forms.ControlStyles.OptimizedDoubleBuffer | System.Windows.Forms.ControlStyles.AllPaintingInWmPaint. As this method is protected, you can only do by sub-classing a control, which is not a problem. As you have to create your own control anyway, it's the best to derive it from System.Windows.Forms.Control and provide the background you need.

See:
http://msdn.microsoft.com/en-us/library/system.windows.forms.controlstyles.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.aspx[^].

—SA
 
Share this answer
 
Try this:
C#
try {
    this.BeginUpdate();
    //Do your drawings here
}
finally {
    this.EndUpdate();
}
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 25-Jul-11 16:55pm    
Sorry, you made a mistake; there is not such method; only some special control types have it, and it has nothing to do with the problem; the only solution is double buggering.
--SA
[no name] 26-Jul-11 0:11am    
Well, the UserControl has one, and so do many of the stock controls that come with .NET Framework. As a person who has done practically many projects of this nature, I know it works. I was about to suggest Double Buffering if the OP didn't find this helpful.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900