rendered paste bodyunit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Math, Controls, Forms,
Dialogs, ExtCtrls, JvExExtCtrls, JvImage, StdCtrls, JvTimer, JvShape;
type
TForm1 = class(TForm)
btn1: TButton;
tmr1: TJvTimer;
dir: TJvShape;
procedure btn1Click(Sender: TObject);
procedure tmr1Timer(Sender: TObject);
//
procedure main;
procedure colision;
//
private
{ Private declarations }
public
{ Public declarations }
end;
type
Tsprite = class(TObject)
private
image: TJvImage;
mass: Single;
x: Integer;
x2: Integer;
y: Integer;
y2: Integer;
cx: Integer;
cy: Integer;
vx: Integer;
vy: Integer;
raio: Integer;
public
constructor Create(filename: string; x: Integer = 1; y: Integer = 1);
procedure Refresh;
end;
function distance(x1: Integer; y1: Integer; x2: Integer; y2: Integer): Extended;
var
Form1: TForm1;
ball: array[1..3] of Tsprite;
implementation
{$R *.dfm}
constructor Tsprite.Create(filename: string; x: Integer = 1; y: Integer = 1);
begin
image := TJvImage.Create(form1);
image.Parent := Form1;
image.Transparent := true;
image.Left := x;
image.Top := y;
image.Picture.LoadFromFile(filename);
image.Center := true;
image.AutoSize := True;
raio := image.Width div 2;
mass := raio / 50;
Randomize;
vx := Random(6) + 2;
vy := Random(6) + 2;
Refresh;
end;
procedure Tsprite.Refresh;
begin
x := image.Left;
y := image.Top;
y2 := image.Top + image.Height;
x2 := image.Left + image.Width;
cx := image.Left + (image.Width div 2);
cy := image.Top + (image.Height div 2);
end;
procedure TForm1.btn1Click(Sender: TObject);
begin
btn1.Enabled := False;
Randomize;
ball[1] := Tsprite.Create('lol.bmp', Random(100), random(100));
ball[2] := Tsprite.Create('lol.bmp', Random(100) + 100, random(100) + 100);
ball[3] := Tsprite.Create('lol.bmp', Random(100) + 190, random(100) + 190);
tmr1.Enabled := True;
end;
procedure TForm1.tmr1Timer(Sender: TObject);
begin
main;
end;
procedure Tform1.main;
var
i: Integer;
begin
// move begin
colision;
for i := 1 to 3 do begin
ball[i].image.Left := ball[i].x + ball[i].vx;
ball[i].image.Top := ball[i].y + ball[i].vy;
ball[i].Refresh;
end;
// move end;
end;
procedure Tform1.colision;
var
n, i, ax, ay: Integer;
dist: Single;
begin
for n := 1 to 3 do begin // n = currentball
// wall hit
//x
if (ball[n].x <= 0)
or (ball[n].x2 >= Form1.ClientWidth) then
begin
ball[n].vx := -ball[n].vx;
// todo: atrito 10%
end;
//y
if (ball[n].y <= 0)
or (ball[n].y2 >= Form1.ClientHeight) then
begin
ball[n].vy := -ball[n].vy;
// todo: atrito 10%
end;
// other balls hit
for i := 1 to 3 do // i = other ball
if i <> n then begin
dist := distance(ball[i].cx, ball[i].cy, ball[n].cx, ball[n].cy);
if dist <= (ball[i].raio + ball[n].raio) then begin
form1.Caption := 'colidiu bola : ' + IntToStr(n) + ' , ' + IntToStr(i) + ' na distancia de ' + FloatToStr(dist);
// todo
//ball[n].vx:= ball[n].vx + ball[i].vx ;
//ball[n].vy:= ball[n].vy + ball[i].vy ;
//ball[n].vx:= ball[i].vx + ball[n].vx ;
//ball[n].vy:= ball[i].vy + ball[n].vy ;
end;
end;
end;
end;
function distance(x1: Integer; y1: Integer; x2: Integer; y2: Integer): Extended;
begin
Result := hypot(x1 - x2, y1 - y2);
end;
end.