I have some troubles with rendering my canvas. Actually I’m trying to write game like space invaders, without using any OpenGL or DirectX. So at the background I have moving sky, and jet moving over it. But the jet is blinding, and sky moves not uniformly. Here’s my code
sky := TBitmap.Create;
sky.LoadFromFile('sky.bmp');
jet := TBitmap.Create;
jet.LoadFromFile('jet.bmp');
jet.Transparent := True;
while True do
begin
for k := 0 to sky.Height do
begin
for i := -1 to (pbMain.Height div sky.Height) do
begin
for j := 0 to (pbMain.Width div sky.Width) do
begin
pbMain.Canvas.Draw(nx, ny, jet);
pbMain.Canvas.Draw(j*sky.Width, k + i*sky.Height, sky);
end;
Application.ProcessMessages;
end;
Sleep(1);
end;
end;
Thank you.
You can’t write a standard Windows app like that. You have to do your painting in response to
WM_PAINTmessages. In Delphi terms this equates to overriding thePaintmethod of aTWinControldescendent, or perhaps using aTPaintBoxand providing anOnPaintevent handler. I’m going to assume that you use aTPaintBox.If you need to avoid flicker it is common practice to draw to an off-screen bitmap and then show this when you are asked to paint.
Your application should probably use a timer control to provide a regular pulse. Then, on each pulse, update your off-screen bitmap. Then call
Invalidateon your paint box to force a paint cycle.The code might look like this: