Simple flicker question (newbie)

W

wandoledzep

I have some very simple code that animates a square across the screen.
Why do the squares flicker so much during runtime?? I am asking because
I am working on a "breakout" type game and have about 20 squares on
screen that flicker like a SOB. What is all this about double buffering
I hear so much about? Any help would be awesome... (Let me remind you
that I _am_ a newbie so be gentle)

Peace,
LedZep



Dim g As Graphics
Dim rect As New Rectangle(20, 20, 100, 100)
Dim rect2 As New Rectangle(140, 20, 100, 100)
Dim rect3 As New Rectangle(260, 20, 100, 100)
Dim rect4 As New Rectangle(380, 20, 100, 100)
Dim x As Integer = 1

Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
g = CreateGraphics()
g.FillRectangle(Brushes.BlueViolet, rect)
g.FillRectangle(Brushes.BlueViolet, rect2)
g.FillRectangle(Brushes.BlueViolet, rect3)
g.FillRectangle(Brushes.BlueViolet, rect4)
End Sub


Private Sub cmdTest_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdTest.Click
Timer1.Enabled = True

End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
rect.X += x
Invalidate()
End Sub
End Class
 
B

Bob Powell [MVP]

T

tommaso.gastaldi

Hi there,

A very elementary way (if you insist with gdi and do not need very high
performances) is to create a graphics from a bitmap (let's call it
"Bitmap").
Then you use the bitmap as the image of a PictureBox.

Anytime you need to change the drawing for animation
you draw normally on the graphics. What you do will not
be seen on the picture box (the picture box is not automatically
refreshed).

When done with the redrawing you simply do:
PictureBox.image = Bitmap
(no need to refresh) at this point that changes you have done will be
visible.

Since during the drawing the bitmap is not refreshed you will have
no flickering. You are actually doing double buffering yourself...

Also when moving objects, you can save the part of background they
cover
first and restore it before a new redraw. I think there are fast api's
for that
you can readily find on the web.

Let me if this works and how it goes...

tommaso
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top