Yur*_*gen 1 delphi animation canvas
渲染我的画布时遇到了一些麻烦.实际上我正试图像空间入侵者一样编写游戏,而不使用任何OpenGL或DirectX.所以在背景中我有移动的天空,并且喷射在它上面移动.但喷气机是致盲的,天空不均匀.这是我的代码
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;
Run Code Online (Sandbox Code Playgroud)
谢谢.
你不能写那样的标准Windows应用程序.你必须做你的绘画以回应WM_PAINT
消息.在Delphi术语中,这等同于覆盖后代的Paint
方法TWinControl
,或者可能使用TPaintBox
并提供OnPaint
事件处理程序.我假设你使用了TPaintBox
.
如果你需要避免闪烁,通常的做法是绘制到屏幕外的位图,然后在要求绘画时显示.
您的应用程序应该使用定时器控件来提供常规脉冲.然后,在每个脉冲上,更新您的屏幕外位图.然后调用Invalidate
您的油漆盒强制进行油漆循环.
代码可能如下所示:
procedure TMainForm.RefreshTimerTimer(Sender: TObject);
begin
RedrawOffscreenBitmap;
PaintBox.Invalidate;
end;
procedure TMainForm.RedrawOffscreenBitmap;
begin
//paint to FOffscreenBitmap
end;
procedure TMainForm.PaintBoxBox(Sender: TObject);
begin
PaintBox.Canvas.Draw(0, 0, FOffscreenBitmap);
end;
Run Code Online (Sandbox Code Playgroud)