unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TSpielfeld =
class(TForm)
Bild: TImage;
Ende: TButton;
Timer1: TTimer;
procedure EndeClick(Sender: TObject);
procedure Formcreate(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
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;
var
Spielfeld: TSpielfeld;
Ball1,Ball2 : TBall;
implementation
{$R *.DFM}
procedure TSpielfeld.EndeClick(Sender: TObject);
begin
halt
end;
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 TBall.ZeigeDich;
begin
with Spielfeld.Bild.Canvas
do
begin
brush.color := Farbe;
ellipse(Round(x-r),Round(y-r ),Round(x+r),Round(y+r))
end;
end;
procedure TBall.BewegeDich;
begin
x := x+vx;
y := y+vy;
with Spielfeld.Bild
do
begin
if (x > width - r - 1)
then
begin
x := width - r - 1;
vx := -vx
end;
if x < r + 1
then
begin
x := r + 1;
vx := -vx
end;
if (y > height - r - 1)
then
begin
y := height - r - 1;
vy := -vy
end;
if y < r + 1
then
begin
y := r+1;
vy := -vy
end
end
end;
procedure TSpielfeld.Formcreate(Sender: TObject);
var x,y,vx,vy:single;
begin
with Bild.Canvas
do
begin
pen.width := 5;
brush.Color := clwhite;
rectangle(0,0,Bild.width,Bild.Height);
pen.Width := 2;
brush.style := bssolid;
pen.mode := pmNOTXOR;
end;
Ball1.Init(clRed,random(Bild.width-50)+25,
random(Bild.height-50)+25,random(9)-4,random(9)-4,20);
Ball1.ZeigeDich;
repeat x:=random (Spielfeld.Bild.Width-50)+25;
y:=random (Spielfeld.Bild.Height-50)+25;
until sqrt (sqr(x-Ball1.x)+sqr(y-Ball1.y)) >= 42;
Ball2.Init (clRed,x,y,Random(9)-4,Random(9)-4,20);
Ball2.Zeigedich;
end;
procedure Stoss;
var h: single;
begin
if sqrt(sqr(Ball1.x-Ball2.x)+sqr(Ball1.Y-Ball2.Y)) < 40
then begin
h:= Ball1.vx; Ball1.vx:= Ball2.vy; Ball2.vx:= h;
h:= Ball1.vy; Ball1.vy:= Ball2.vy; Ball2.vy:= h;
end
end;
procedure TSpielfeld.Timer1Timer(Sender: TObject);
begin
Ball1.ZeigeDich;
Ball2.ZeigeDich;
Bild.Canvas.Pixels[Round(Ball1.x),Round(Ball1.y)] := clred;
Bild.Canvas.Pixels[Round(Ball2.x),Round(Ball2.y)] := clblue;
Ball1.BewegeDich;
Ball2.BewegeDich;
Stoss;
Ball1.ZeigeDich;
Ball2.ZeigeDich;
end;
Initialization
Randomize;
Ball1 := TBall.Create;
Finalization
Ball1.Destroy;
Ball2.destroy;
end.