好吧,首先我只是在delphi中玩得很开心,对它来说还是个新手,但我注意到,每当我尝试制作某种游戏时,其中W、A、S和D是移动对象(TImage)的按钮,它就会开始随机闪烁,我注意到如果速度很快,或者当它移动时,后面有另一个图像(背景),就会发生这种情况…
我的大部分"移动"代码看起来是这样的:
if key = 's' then begin
for I := 1 to 5 do
sleep(1);
x:=x-2;
Image1.Top := x;
end;
也许这是原因,但仍然很烦人。如果你能帮忙,我将非常高兴。
这样的事情最好使用TPaintBox
来处理。
让按键根据需要设置变量,然后在操作系统准备就绪时调用TPaintBox.Invalidate()
来触发重新绘制。
TPaintBox.OnPaint
事件处理程序然后可以根据需要在由当前变量值指定的适当坐标处绘制TGraphic
。
var
X: Integer = 0;
Y: Integer = 0;
procedure TMyForm.KeyPress(Sender: TObject; var Key: Char);
begin
case Key of
'W': begin
Dec(Y, 2);
PaintBox.Invalidate;
end;
'A': begin
Dec(X, 2);
PaintBox.Invalidate;
end;
'S': begin
Inc(Y, 2);
PaintBox.Invalidate;
end;
'D': begin
Inc(X, 2);
PaintBox.Invalidate;
end;
end;
end;
procedure TMyForm.PaintBoxPaint(Sender: TObject);
begin
PaintBox.Canvas.Draw(X, Y, SomeGraphic);
end;