unit Game2A;

interface

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

type
  TGame = class
    Hgrund: TImage;
    Figur:  TImage;
    xPos, yPos, xDiff, yDiff: integer;
    xLinks, xRechts, yOben, yUnten: integer;
    constructor Create (Bild1, Bild2: TImage);
    procedure Run;
    procedure Step (Nr: integer);
    destructor Free;
  end;

  TForm1 = class(TForm)
    Image1: TImage;
    Image2: TImage;
    procedure FormCreate(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

const Pfad = '.\bilder\';
var
  Form1: TForm1;
  Bugging: TGame;

implementation

{$R *.dfm}

constructor TGame.Create (Bild1, Bild2: TImage);
begin
  // Hintergrund und Figur laden
  try
    Hgrund := Bild1;
    Figur  := Bild2;
    Hgrund.Picture.LoadFromFile (Pfad+'Hgrund1.bmp');
    Figur.Picture.LoadFromFile  (Pfad+'Insekt1.bmp');
  except
    Hgrund.Canvas.Brush.Color := clGreen;
    Hgrund.Canvas.FloodFill (10,10, clBlack, fsBorder);
    Figur.Canvas.Brush.Color := clGreen;
    Figur.Canvas.FloodFill (10,10, clBlack, fsBorder);
    Figur.Canvas.Ellipse (0,0, 50,50);
    Figur.Canvas.Brush.Color := clPurple;
    Figur.Canvas.FloodFill (10,10, clBlack, fsBorder);
  end;
  // Position der Figur bestimmen
  xPos := Figur.Left;
  yPos := Figur.Top;
  // Schrittweite bestimmen
  xDiff := 15; yDiff := 10;
  // Grenzen setzen
  xLinks  := Figur.Width - 2*xDiff;
  xRechts := Hgrund.Width - xLinks - 3*xDiff;
  yOben   := Figur.Height - 4*yDiff;
  yUnten  := Hgrund.Height - yOben - 5*yDiff;
end;

procedure TGame.Run;
begin
end;

procedure TGame.Step (Nr: integer);
begin
  case Nr of
    1: if Figur.Top  > yOben   then dec(yPos, yDiff)  // nach oben
                               else yPos := yUnten;
    2: if Figur.Left < xRechts then inc(xPos, xDiff)  // nach rechts
                               else xPos := xLinks;
    3: if Figur.Top  < yUnten  then inc(yPos, yDiff)  // nach unten
                               else yPos := yOben;
    4: if Figur.Left > xLinks  then dec(xPos, xDiff)  // nach links
                               else xPos := xRechts;
  end;
  Figur.Picture.LoadFromFile (Pfad+'Insekt'+IntToStr(Nr)+'.bmp');
  Figur.Left := xPos;
  Figur.Top  := yPos;
end;

destructor TGame.Free;
begin
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Bugging := TGame.Create (Image1, Image2);
  Doublebuffered := true;
  KeyPreview := True;
end;

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  case Key of
    vk_Up   : Bugging.Step (1);
    vk_Right: Bugging.Step (2);
    vk_Down : Bugging.Step (3);
    vk_Left : Bugging.Step (4);
  end;
end;

end.
