unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, JPEG, ExtCtrls;
type
TSpielfeld =
class(TForm)
GroupBox1: TGroupBox;
Stop: TButton;
Bewegen: TButton;
Timer1: TTimer;
Bild: TImage;
Shape1: TShape;
Shape2: TShape;
Shape3: TShape;
Shape4: TShape;
procedure BewegenClick(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure StopClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
type
tBall =
class x,y,vx,vy,r : Single;
farbe : tColor;
procedure Init(fNeu: tColor;
xNeu,yNeu,vxNeu,vyNeu,rNeu : Single);
procedure ZeigeDich;
procedure BewegeDich;
end;
CONST Max = 4;
var
Spielfeld: TSpielfeld;
Ball :
array[1..Max]
of tBall;
i : Integer;
Farbe: tColor;
implementation
{$R *.DFM}
procedure tBall.Init(fNeu: tColor;
xNeu,yNeu,vxNeu,vyNeu,rNeu: Single);
begin
farbe := fNeu;
r := rNeu;
x := xNeu;
y := yNeu;
vx := vxNeu;
vy := vyNeu;
end;
procedure TSpielfeld.FormCreate(Sender: TObject);
// Bälle zufällig auf dem Bildschirm erzeugen, ohne Bewegung
var i,j: integer; r,x,y,vx,vy : single;
begin
with Bild.Canvas
do
begin
Brush.Color := clGreen;
Pen.Color := clBlack;
pen.width := 5;
Rectangle(0, 0, Bild.Width, Bild.Height);
end;
for i := 1
to max
do
begin
x := Random(Spielfeld.Bild.Width-50) + 25;
y := Random(Spielfeld.Bild.Height-50)+ 25;
vx := Random(5)-4; vy := Random(5)-4;
Ball[i].Init(clWhite,x,y,vx,vy,20);
Ball[i].ZeigeDich;
end
end;
procedure tBall.ZeigeDich;
// Ball anzeigen
begin
with Spielfeld.Bild.Canvas
do
begin
pen.width := 2;
Brush.Color := clRed;
Pen.Color := clBlack;
Ellipse(Round(x-r),Round(y-r),Round(x+r),Round(y+r));
end;
end;
procedure tBall.BewegeDich;
// Bälle bewegen, verschwinden lassen und neu zeichnen
begin
with Spielfeld.Bild
do
begin
Canvas.Pen.Color := clGreen;
Canvas.Brush.Color := clGreen;
Canvas.Ellipse(Round(x-r),Round(y-r),Round(x+r),Round(y+r));
x := x +vx;
y := y + vy;
if (x >Width-r-4)
then
begin x := Width-r-4;
vx := -vx
end;
if x < r + 4
then
begin x := r + 4;
vx := -vx
end;
if (y >Height-r-4)
then
begin y := Height-r -4 ;
vy := -vy
end;
if y < r + 4
then
begin
y := r + 4;
vy := -vy;
end;
Canvas.Pen.Color := clBlack;
Canvas.Brush.Color := clRed;
Canvas.Ellipse(Round(x-r),Round(y-r),Round(x+r),Round(y+r));
end;
end;
procedure Stoss;
// Bälle anstoßen
var h,d : single; i,j: integer;
begin
for i := 1
to max-1
do
for j := i+1
to max
do
begin
d:=sqrt(sqr(Ball[i].x-Ball[j].x)+sqr(Ball[i].y-Ball[j].y));
if d <= Ball[i].r+Ball[j].r
then begin
h := Ball[i].vx; Ball[i].vx := Ball[j].vx; Ball[j].vx := h;
h := Ball[i].vy; Ball[i].vy := Ball[j].vy; Ball[j].vy := h;
end
end
end;
procedure TSpielfeld.BewegenClick(Sender: TObject);
// Timer aktivieren
begin
if Bewegen.Caption = '
Bewegen'
then
begin
Bewegen.Caption := '
Stoppen';
Timer1.Enabled := True;
end
else begin
Bewegen.Caption := '
Bewegen';
Timer1.Enabled := False;
end
end;
procedure TSpielfeld.Timer1Timer(Sender: TObject);
// Alle Bälle rollen
begin
for i := 1
to max
do
begin
Ball[i].ZeigeDich;
Ball[i].BewegeDich;
end;
Stoss;
for i := 1
to max
do Ball[i].ZeigeDich;
end;
procedure TSpielfeld.StopClick(Sender: TObject);
// Programm schließen
begin
Halt
end;
Initialization;
Randomize;
for i:=1
to max
do ball[i] := tBall.Create;
Finalization;
for i:=1
to max
do Ball[i].Destroy;
end.