0

Ok so first of all i'm just messing around in delphi, and still really new to it, but i noticed that whenever i try to make some kind of game , where W,A,S, and D are the buttons which moves an object (TImage) , it starts flashing randomly, i noticed that it happens if the speed is fast, or when it's moving and there is another image (background) behind it...

Most of my "moving" code looks like this:

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

Maybe that causes it, but still it's really annoying. I would be really pleased if you could help with this.

Craig
  • 1,874
  • 13
  • 41
Coldus12
  • 27
  • 6
  • 5
    Fundamentally your entire approach is wrong. Image controls are not meant to be used as sprites. The call to Sleep is also not good. You need a fundamentally different style of architecture for a game. – David Heffernan Mar 26 '15 at 18:16
  • @Coldus: TImage and TPaintBox are not good for this as mentioned. Check GDI+ which would work great for your purposes. – smooty86 Mar 26 '15 at 18:38
  • Or you can check some of the existing game based graphical engines or even game engines instead. For quick prototyping you could check the unDelphiX (http://www.micrel.cz/Dx/) which is simple graphics engine written for Delphi that wraps around DirectX for achieving god performance. And if you decide to use it note this: Double-clicking on TDXTimer which is DelphiX version of Timer does not create OnTimer event method as it would with classic Delphi timer but OnActivate method instead. This is most common problem that new users of DelphiX usually face :-) – SilverWarior Mar 27 '15 at 08:04
  • Also if you are really interested in game development you could come join us at http://www.pascalgamedevelopment.com/ – SilverWarior Mar 27 '15 at 08:18

1 Answers1

4

Something like this is better handled using TPaintBox instead.

Have the keystrokes set variables as needed and then call TPaintBox.Invalidate() to trigger a repaint when the OS is ready for it.

The TPaintBox.OnPaint event handler can then draw a TGraphic at the appropriate coordinates specified by the current variable values as needed.

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;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770