Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi zwei bälle in bewegung (https://www.delphipraxis.net/72313-zwei-baelle-bewegung.html)

Nicole 28. Jun 2006 13:53


zwei bälle in bewegung
 
Hallo!
Ich möchte zwei Bälle in Bewegung haben,kriegs aber nur hin einen zu programmieren....
ich könnte doch eigentlich den gleichen delltext des ersten balls nehemen und die zeigedich funktion verändern,aber an welche stelle im quelltext setzt ich das dann alles?

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;

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);
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;
end;

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

finalization
  ball.destroy;


end.
danke nicole

arbu man 28. Jun 2006 14:00

Re: zwei bälle in bewegung
 
du braust noch eine zweite variable vom type TBall z.b.:

Delphi-Quellcode:
var
  Form1: TForm1;
  ball: tball;
  Ball2: TBall;
Dann kanst du diese wie "ball" behandeln.

Nicole 28. Jun 2006 14:13

Re: zwei bälle in bewegung
 
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);
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;
end;

procedure stoss;
  var h:single;
begin
if sprt(spr(ball.x-ball2.x)+spr(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;

finalization
  ball.destroy;


end.
is das so erstmal okay?

undd ann muss ich ja den zweiten ball noch programmieren und ich möcht wissen,an welcher stelle die anweisungen dafür müssen?

Sascha L 28. Jun 2006 14:17

Re: zwei bälle in bewegung
 
Du musst Ball2 auch noch - wie Ball1 - initialisieren (Ball2 := TBall.Create) und dann noch die Prozedur "Init" aufrufen.

Am besten schreibst du überall dasselbe mit Ball2, was auch bezüglich Ball1 steht.

turboPASCAL 28. Jun 2006 14:17

Re: zwei bälle in bewegung
 
Zitat:

Zitat von Nicole
Delphi-Quellcode:
// ...

initialization
  randomize;
  ball:=tball.create;
  // und noch Ball2
  ball2:=tball.create;

finalization
  ball.destroy;
  ball2.destroy; // <--<<<

end.
is das so erstmal okay?

undd ann muss ich ja den zweiten ball noch programmieren und ich möcht wissen,an welcher stelle die anweisungen dafür müssen?

Nicht so wirklich, Ball 2 muss ja auch erst einmal erzeuge werden oder ? ;)

Nicole 28. Jun 2006 14:26

Re: zwei bälle in bewegung
 
irgendwie häng ich grad völlig in der luft
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; zeigedich2;
    procedure bewegedich; bewegedich2;


     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 tball2.init(fneu: tcolor; xneu, yneu, vxneu, vyneu, rneu: single);
begin
  farbe:= fneu;
  r:= rneu;
  x:=xneu;
  y:=yneu;
  vx:=vxneu;
  vy:=vyneu;
end;

procedure tball2.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 tball2.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);
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;
end;

procedure stoss;
  var h:single;
begin
if sprt(spr(ball.x-ball2.x)+spr(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.

so,hätt ichs gemacht,aber es funktioniert so nciht...aber wie dann anders?

fkerber 28. Jun 2006 14:28

Re: zwei bälle in bewegung
 
Hi!

Nein, so geht das nicht!

Die Klasse TBall reicht ja vollkommen, Ball 2 ist ja eine Instanz davon, also alles mit TBall2 kannst du wieder löschen...

Was dir fehlt ist im FormCreate das Init von ball2!


Ciao Frederic

Nicole 28. Jun 2006 14:48

Re: zwei bälle in bewegung
 
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.

Michael Habbe 28. Jun 2006 15:01

Re: zwei bälle in bewegung
 
Zitat:

Zitat von Nicole
okay,ich glaub ich habs soweit...außer das der noch an einer stelle anzeigt nicht genügend witrkliche parameter


Delphi-Quellcode:
 ball2.init(clblue,x,y,random(9)-4,random(9)-4),20); <------da zeigt,er nciht genügend wirkliche parameter,was mach ich jetzt?
                                             ^ die Klammer muss weg!



Alle Zeitangaben in WEZ +1. Es ist jetzt 02:47 Uhr.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz