Einzelnen Beitrag anzeigen

Benutzerbild von Bummi
Bummi

Registriert seit: 15. Jun 2010
Ort: Augsburg Bayern Süddeutschland
3.470 Beiträge
 
Delphi XE3 Enterprise
 
#4

AW: Bewegung von Shapes

  Alt 25. Apr 2013, 16:21
Delphi-Quellcode:
unit Unit6;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls;

type
  TForm6 = class(TForm)
    WanderShape: TShape;
    Timer1: TTimer;
    DestPointShape: TShape;
    ManShape: TShape;
    procedure Timer1Timer(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  private
    { Private-Deklarationen }
    FXdest, FYdest: Integer;
    FCnt: Integer;
  public
    { Public-Deklarationen }
  end;

var
  Form6: TForm6;

implementation

{$R *.dfm}

procedure TForm6.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
const
  C_Speed = 5;
begin
    case Key of
      VK_Left: ManShape.Left := ManShape.Left - C_Speed ;
      VK_Right: ManShape.Left := ManShape.Left + C_Speed ;
      VK_Up: ManShape.Top := ManShape.Top - C_Speed ;
      VK_Down: ManShape.Top := ManShape.Top + C_Speed ;

    end;
end;

procedure TForm6.Timer1Timer(Sender: TObject);
type
  TVektor = Record
    X: Double;
    Y: Double;
  End;
const
  C_Speed = 5;
var
  Vektor: TVektor;
  DX, DY, HP: Double;
begin
  FCnt := Random(15);
  if FCnt = 7 then
  begin
    FXdest := Random(ClientWidth - WanderShape.Width) + WanderShape.Width div 2;
    FYdest := Random(ClientHeight - WanderShape.Height) + WanderShape.Height div 2;
    DestPointShape.Top := FYdest;
    DestPointShape.Left := FXdest;
  end;
  DX := FXdest - (WanderShape.Left + (WanderShape.Width DIV 2));
  DY := FYdest - (WanderShape.Top + (WanderShape.Height DIV 2));
  HP := SQRT((DX * DX) + (DY * DY));
  if (HP > 0) then
  begin
    if Abs(DX) < C_Speed then
      Vektor.X := 0
    else
      Vektor.X := DX / HP;
    if Abs(DY) < C_Speed then
      Vektor.Y := 0
    else
    Vektor.Y := DY / HP;
  end
  else
  begin
    Vektor.X := 0;
    Vektor.Y := 0;
  end;
  WanderShape.Left := Round(WanderShape.Left + Vektor.X * C_Speed);
  WanderShape.Top := Round(WanderShape.Top + Vektor.Y * C_Speed);
end;

end.
Thomas Wassermann H₂♂
Das Problem steckt meistens zwischen den Ohren
DRY DRY KISS
H₂ (wenn bei meinen Snipplets nichts anderes angegeben ist Lizenz: WTFPL)
  Mit Zitat antworten Zitat