0

好的,首先我只是在 delphi 中乱搞,而且对它还是很陌生,但我注意到每当我尝试制作某种游戏时,W、A、S 和 D 是移动按钮object (TImage) ,它开始随机闪烁,我注意到如果速度很快,或者当它移动并且后面有另一个图像(背景)时会发生这种情况......

我的大部分“移动”代码如下所示:

if key = 's' then begin
for I := 1 to 5 do
    sleep(1);
    x:=x-2;
    Image1.Top := x;
 end;

也许这会导致它,但它仍然很烦人。如果您能对此提供帮助,我将非常高兴。

4

1 回答 1

4

像这样的事情最好使用TPaintBox代替。

让击键根据需要设置变量,然后TPaintBox.Invalidate()在操作系统准备好时调用以触发重绘。

然后,事件处理程序可以根据需要在当前变量值指定的适当坐标处TPaintBox.OnPaint绘制 a 。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;
于 2015-03-26T18:34:52.863 回答