Bueno ahí les dejo un ejemplo de Colisión que necesitaba para unas cosa. Esta creado en Free Pascal usando como IDE Lazarus.

La Bola Roja La Mueven Con las flechas para darle dirección.

Imagen:
Imagen


Codigo:
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  windows, Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs,
  ExtCtrls, ComCtrls;

type
   Circulo=record
    X:Integer;
    Y:Integer;
    Tam:integer;
    Rad:double;
end;




  { TForm1 }

  TForm1 = class(TForm)
    StatusBar1: TStatusBar;
    Timer1: TTimer;
    procedure FormCreate(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure FormPaint(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;
  const
  iStep:integer=10;
var
  Form1: TForm1;
  X:integer=0;
  Y:Integer=0;
  P1:Circulo;
  P2:Circulo;
  Direction:word;
  Colision:Boolean=false;
implementation

{$R *.lfm}

{ TForm1 }



procedure TForm1.FormCreate(Sender: TObject);
begin
Form1.Caption:='Colisión Ejemplo';
Form1.Color:=clBlack;
Form1.Width:=500;
Form1.Height:=500;
StatusBar1.Panels.Add;
StatusBar1.Panels.Add;
StatusBar1.Panels[0].Width:=round(Form1.Width/3);




with Canvas do
begin
    Form1.Canvas.Brush.Color:=clred;
    pen.width   := 1;
    pen.Color:=clWhite;
end;
  P1.X:=0;
  P1.Y:=0;
  P1.Tam:=40;
  P1.Rad:=P1.Tam/2;

  P2.X:=40*5;
  P2.Y:=40*5;
  P2.Tam:=60;
  P2.Rad:=P2.Tam/2;


end;

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState
  );

begin

case Key of
    VK_LEFT: Direction:=VK_LEFT;     //Left
    VK_RIGHT:Direction:=VK_RIGHT;    //Right
    VK_UP:  Direction:=VK_UP;        //Up
    VK_DOWN: Direction:=VK_DOWN;     //Down
  end;

end;

procedure TForm1.FormPaint(Sender: TObject);
begin
Form1.Canvas.Brush.Color:=clred;
canvas.Ellipse(P1.X,P1.Y,P1.Tam+P1.X,P1.Tam+P1.Y);
Form1.Canvas.Brush.Color:=clgreen;
canvas.Ellipse(P2.X,P2.Y,P2.Tam+P2.X,P2.Tam+P2.Y);
Form1.Canvas.Brush.Color:=clWhite;
 if Colision=true then
 begin
     Form1.Canvas.pen.Color:=clblue;
     Form1.canvas.Line(P1.X+Round(P1.Rad),P1.Y+Round(P1.Rad),P2.X+Round(P2.Rad),P2.Y+Round(P2.Rad));
     StatusBar1.Panels[0].Text:='Hay Colision'   //form1.Canvas.TextOut(0,0,'Hay Colision')
     end
     else
     begin
     Form1.Canvas.pen.Color:=clWhite;
     Form1.canvas.Line(P1.X+Round(P1.Rad),P1.Y+Round(P1.Rad),P2.X+Round(P2.Rad),P2.Y+Round(P2.Rad));
     StatusBar1.Panels[0].Text:='No Hay Colision' //Form1.Canvas.TextOut(0,0,'No Hay Colision')
     end
end;


procedure TForm1.Timer1Timer(Sender: TObject);
  var
  dX,dY:double;
  Dis:double;
begin

dX:= ((P2.X+P2.Rad)-(P1.X+P1.Rad));
dY:= ((P2.Y+P2.Rad)-(P1.Y+P1.Rad));
Dis:=sqrt((dX * dX) + (dY * dY));

   StatusBar1.Panels[1].Text:='Distancia entre Bola Roja y Bola Verde: ' + inttostr(round(Dis-P1.Rad-P2.Rad));

   If ((Dis)<P1.Rad+P2.Rad) then
        Colision:=true
        else
       Colision:=false;


 case Direction of
    VK_LEFT: if not(P1.X<=0) then  P1.X-=iStep;                     //Left
    VK_RIGHT:if not(P1.X>=Form1.Width-P1.Tam) then  P1.X+=iStep;   //Right
    VK_UP:  if not(P1.Y<=0) then P1.Y-=iStep;                       //Up
    VK_DOWN: if not(P1.Y>=Form1.Height-P1.Tam-StatusBar1.Height) then P1.Y+=iStep;   //Down
  end;
  Form1.Invalidate;
end;

end.

Link Proyecto+Compilado.
[Enlace externo eliminado para invitados]
Clave: Pink


Saludos
Imagen
Responder

Volver a “Fuentes”