unit Game1;

interface

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

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

  TForm1 = class(TForm)
    Image1: TImage;
    Image2: TImage;
    Timer1: TTimer;
    Button1: TButton;
    Button2: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

const Pfad = '.\bilder\';
var
  Form1: TForm1;
  Freeball: TGame;
  Moving: boolean;

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+'Ball1.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 := clRed;
    Figur.Canvas.FloodFill (10,10, clBlack, fsBorder);
  end;
  // Schrittwerte bestimmen
  xDiff   := 15;
  yDiff   := 10;
  // Grenzen setzen
  xLinks  := Figur.Width - 2*xDiff;
  xRechts := Hgrund.Width - xLinks - 3*xDiff;
  yOben   := Figur.Height - 3*yDiff;
  yUnten  := Hgrund.Height - yOben - 4*yDiff;
end;

procedure TGame.Run;
begin
  // Rand links und rechts kontrollieren
  if Figur.Left < xLinks  then xDiff := -xDiff;
  if Figur.Left > xRechts then xDiff := -xDiff;
  // Rand oben und unten kontrollieren
  if Figur.Top < yOben  then yDiff := -yDiff;
  if Figur.Top > yUnten then yDiff := -yDiff;
  // Ball bewegen
  Figur.Left := Figur.Left + xDiff;
  Figur.Top  := Figur.Top  + yDiff;
end;

procedure TGame.Turn;
begin
  xDiff := -xDiff;
  yDiff := -yDiff;
end;

destructor TGame.Free;
begin
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Freeball := TGame.Create (Image1, Image2);
  Moving := false;
  Doublebuffered := true;
  randomize;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Moving := not Moving;
  if Moving then Button1.Caption := 'Stopp'
            else Button1.Caption := 'Start';
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  Freeball.Turn;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  if Moving then Freeball.Run;
end;

end.
