AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

zwei bälle in bewegung

Ein Thema von Nicole · begonnen am 28. Jun 2006 · letzter Beitrag vom 28. Jun 2006
Antwort Antwort
Nicole

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

zwei bälle in bewegung

  Alt 28. Jun 2006, 13:53
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
  Mit Zitat antworten Zitat
Benutzerbild von arbu man
arbu man

Registriert seit: 3. Nov 2004
Ort: Krefeld
1.108 Beiträge
 
Delphi 7 Professional
 
#2

Re: zwei bälle in bewegung

  Alt 28. Jun 2006, 14:00
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.
Björn
>> http://bsnx.net <<
Virtual DP Stammtisch v1.0"iw" am 19.09.2007 - ich war dabei!
  Mit Zitat antworten Zitat
Nicole

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

Re: zwei bälle in bewegung

  Alt 28. Jun 2006, 14:13
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?
  Mit Zitat antworten Zitat
Sascha L

Registriert seit: 4. Jun 2004
Ort: Hamm
390 Beiträge
 
Delphi 2006 Professional
 
#4

Re: zwei bälle in bewegung

  Alt 28. Jun 2006, 14:17
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.
Sascha
  Mit Zitat antworten Zitat
Benutzerbild von turboPASCAL
turboPASCAL

Registriert seit: 8. Mai 2005
Ort: Sondershausen
4.274 Beiträge
 
Delphi 6 Personal
 
#5

Re: zwei bälle in bewegung

  Alt 28. Jun 2006, 14:17
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 ?
Matti
Meine Software-Projekte - Homepage - Grüße vom Rüsselmops -Mops Mopser
  Mit Zitat antworten Zitat
Nicole

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

Re: zwei bälle in bewegung

  Alt 28. Jun 2006, 14:26
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?
  Mit Zitat antworten Zitat
Benutzerbild von fkerber
fkerber
(CodeLib-Manager)

Registriert seit: 9. Jul 2003
Ort: Ensdorf
6.723 Beiträge
 
Delphi XE Professional
 
#7

Re: zwei bälle in bewegung

  Alt 28. Jun 2006, 14:28
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
Frederic Kerber
  Mit Zitat antworten Zitat
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
Benutzerbild von Michael Habbe
Michael Habbe

Registriert seit: 10. Aug 2005
264 Beiträge
 
Turbo Delphi für Win32
 
#9

Re: zwei bälle in bewegung

  Alt 28. Jun 2006, 15:01
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!
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 23:06 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz