Thema: Delphi zwei bälle in bewegung

Einzelnen Beitrag anzeigen

Nicole

Registriert seit: 1. Feb 2006
18 Beiträge
 
#8

Re: zwei bälle in bewegung

  Alt 28. Jun 2006, 14:48
okay,ich glaub ich habs soweit...außer das der noch an einer stelle anzeigt nicht genügend witrkliche parameter


Delphi-Quellcode:
 unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Image1: TImage;
    Timer1: TTimer;
    procedure FormCreate(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

  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
  Form1: TForm1;
  ball: tball;
   ball2:tball;
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 tball.zeigedich;
begin
  with form1.image1.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 form1.Image1 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 TForm1.FormCreate(Sender: TObject);
var x,y:integer;
begin
  with image1.canvas do
    begin
      pen.width:=5;
      brush.color:=clwhite;
      rectangle(0,0,image1.width,image1.height);
      pen.width:=2;
      brush.style:=bssolid;
      pen.mode:=pmnotxor;
    end;
  ball.init(clred,random(image1.width-50)+25, random(image1.height-50)+25,random(9)-4,random(9)-20,20);
  ball.zeigedich;
 repeat
 x:= random(unit1.form1.width-50)+25;
 y:= random(unit1.form1.height-50)+25;
 until
 sqrt(sqr(x-ball.x)+sqr(y-ball.y)) <=42;
 ball2.init(clblue,x,y,random(9)-4,random(9)-4),20); <------da zeigt,er nciht genügend wirkliche parameter,was mach ich jetzt?
 ball2.zeigedich;
end;
procedure stoss;
  var h:single;
begin
if sqrt(sqr(ball.x-ball2.x)+sqr(ball.y-ball2.y))<40
  then begin
  h:=ball.vx;ball.vx:=ball2.vx; ball2.vx:=h;
  h:=ball.vy;ball.vy:=ball2.vy; ball2.vy:=h;
end;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  ball.zeigedich; ball2.zeigedich;
  image1.canvas.pixels[round(ball.x),round(ball.y)]:=clblack;
  image1.canvas.Pixels[round(ball2.x),round(ball2.y)]:=clblue;
  ball.bewegedich; ball2.bewegedich;
stoss;
ball.zeigedich; ball2.zeigedich;
end;
initialization
  randomize;
  ball:=tball.create;
  ball2:=tball.create;
finalization
  ball.destroy;
  ball2.destroy;

end.
  Mit Zitat antworten Zitat