Autor Beitrag
Sylvus
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 195



BeitragVerfasst: Mi 05.03.08 18:55 
Hi,
ich muss einen Ball programmieren der von den Wänden abprallt. Der kann aber auch wie beim Billard in den Ecken aufgefangen werden. Nun dachte ich, dass ich die Ecken mit einer If Anweisung auffange. Aber das geht nicht. Kann mir wer sagen ob mein Code richtig ist?
Viele Grüße Sylvus

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
procedure TForm1.Timer1Timer(Sender: TObject);
begin
if ((x1<37)AND(y1<37)) OR ((x1<37)AND(y1>373)) OR ((x1>761)AND(y1<37)) OR ((x1>761)AND(y1<373)) then
Timer1.Enabled:=false;

if (y1<17) OR (y1>351) then
h1:=h1-(2*h1);
if (x1<17) OR (x1>741) then
h:=h-(2*h);

an:=an+h;
xan:=xan-h1;
Image1.Canvas.pen.color:=clwhite;
x1:=100+an; y1:=100+xan; x2:=120+an; y2:=120+xan;
Image1.Canvas.Ellipse(x1,y1,x2,y2);
Image1.Canvas.pen.color:=clgreen;
Image1.Canvas.Ellipse(x1,y1,x2,y2);

end;


Byee
Sinspin
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1341
Erhaltene Danke: 120

Win 10
RIO, CE, Lazarus
BeitragVerfasst: Mi 05.03.08 19:35 
1. Ich habe noch nie eine if Schleife gesehen! Dies welche Wunderding musst du mir mal zeigen.
2. Auch wenn es ohne Arbeit spart, formatiere deinen Quelltext ein bisschen. Und schreibe nur einen Befehl pro Zeile!
3. Was willst du mit x2 und y2? Die sind doch absolut nicht nötig. Die zweite Ecke für den Ball kanst du doch mit x1+N und y1+N berechnen(N ist der durchmesser deines Balls)
4. Was ist mit 100+... und 120+...? das macht keinen Sinn! Die Initalisierung von x1 und y1 machst du einmalig, außerhalb vom Timer. Am besten im OnCreate oder OnShow. Du musst ja auch die anderen werte noch Initialisieren (h1 und h)

Hier mal eine (nicht getestete) Fassung deiner Vorgabe:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
procedure TForm1.Timer1Timer(Sender: TObject);
begin
  if ((x1<37)AND(y1<37)) OR ((x1<37)AND(y1>373)) OR ((x1>761)AND(y1<37)) OR ((x1>761)AND(y1<373)) then
  Timer1.Enabled:=false;

  if (y1<17) OR (y1>351) then
    h1:=h1-(2*h1);
  if (x1<17) OR (x1>741) then
    h:=h-(2*h);

  //x1:=100+an; 
  //y1:=100+xan; 
  x1:=an+h;
  y1:=xan-h1;
  //x2:=120+an;  // überflüssig
  //y2:=120+xan; // überflüssig
  Image1.Canvas.pen.color:=clwhite;
  Image1.Canvas.Ellipse(x1,y1,x1+20,y1+20); // die größe deines Balls ist 20 pixel
  
  Image1.Canvas.pen.color:=clgreen;
  Image1.Canvas.Ellipse(x1,y1,x1+20,y1+20);
end;

_________________
Wir zerstören die Natur und Wälder der Erde. Wir töten wilde Tiere für Trophäen. Wir produzieren Lebewesen als Massenware um sie nach wenigen Monaten zu töten. Warum sollte unser aller Mutter, die Natur, nicht die gleichen Rechte haben?
Sylvus Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 195



BeitragVerfasst: Mi 05.03.08 21:00 
ja ok funktioniert jetzt wie folgt:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
procedure TForm1.Button1Click(Sender: TObject);
begin
Timer1.Enabled:=true;
x1:=random(300)+100; 
y1:=random(300)+100;
h:=random(10); 
h1:=random(10);
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
if ((x1<20)AND(y1<20)) OR ((x1<20)AND(y1>349)) OR ((x1>729)AND(y1<20)) OR ((x1>729)AND(y1>349)) then
Timer1.Enabled:=false;
if (y1<17) OR (y1>351) then
h1:=h1-(2*h1);
if (x1<17) OR (x1>741) then
h:=h-(2*h);
  x1:=x1+h;
  y1:=y1-h1;
Image1.Canvas.pen.color:=clwhite;
Image1.Canvas.Ellipse(x1,y1,x1+20,y1+20);
Image1.Canvas.pen.color:=clgreen;
Image1.Canvas.Ellipse(x1,y1,x1+20,y1+20);
end;


Danke nochmal, bis bald :) Grüße Sylvus
Hidden
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 2242
Erhaltene Danke: 55

Win10
VS Code, Delphi 2010 Prof.
BeitragVerfasst: Mi 05.03.08 21:55 
user profile iconSylvus hat folgendes geschrieben:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
  if ((x1 < 20) AND (y1 < 20)) OR ((x1 < 20) AND (y1 > 349)) OR ((x1 > 729)AND(y1 < 20)) OR ((x1>729)AND(y1>349)) then
    Timer1.Enabled:=false;
  if (y1 < 17) OR (y1 > 351) then
    h1 := h1 - (2 * h1);
  if (x1 < 17) OR (x1 > 741) then
    h := h - (2 * h);
  x1:=x1+h;
  y1:=y1-h1;
  Image1.Canvas.pen.color:=clwhite;
  Image1.Canvas.Ellipse(x1,y1,x1+20,y1+20);
  Image1.Canvas.pen.color:=clgreen;
  Image1.Canvas.Ellipse(x1,y1,x1+20,y1+20);
  end;


Hi,

Die markierte Stelle deckt etwa diesen definitionsbereich ab:(Ich spiele jetzt mal Plotter :? )
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
xxx......xxx
xxx......xxx
xxx......xxx
............
............
............
xxx......xxx
xxx......xxx
xxx......xxx

was du vermutlich willst ist aber dies hier:
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
xxxxxxxxxxxx
xxxxxxxxxxxx
xxxxxxxxxxxx
xxx......xxx
xxx......xxx
xxx......xxx
xxxxxxxxxxxx
xxxxxxxxxxxx
xxxxxxxxxxxx

(die Funktion soll doch abfragen, ob das Objekt den Bildschirm verlässt?)
Die Abfrage sollte einfach lauten:
ausblenden Delphi-Quelltext
1:
2:
if (x < x0) or (x > x1) or (y < y0) or (y > y1) then
  ...


Vieles lässt sich kompliziert schreiben ;D
h1 := h1 - (2 * h1); <=> h1 := -h1; //wolltest du das? Die Aussage ist identisch!

Vieles lässt sich verstehen, indem du einfach Computer spielst: setze ein Beispiel in deine Formeln ein und Rechne im Kopf!

mfG,

_________________
Centaur spears can block many spells, but no one tries to block if they see that the spell is a certain shade of green. For this purpose it is useful to know some green stunning hexes. (HPMoR)
Bernhard Geyer
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 721
Erhaltene Danke: 3



BeitragVerfasst: Mi 05.03.08 23:26 
user profile iconSinspin hat folgendes geschrieben:
1. Ich habe noch nie eine if Schleife gesehen! Dies welche Wunderding musst du mir mal zeigen.

Off-Topic:
Wenn DeddyH (www.delphipraxis.net/user25229.html) sich hier auch herumtreibt kostet das eine Forenrunde :-)
nagel
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 708

Win7, Ubuntu 10.10

BeitragVerfasst: Do 06.03.08 00:52 
@Hidden:

user profile iconSylvus hat folgendes geschrieben:
Der kann aber auch wie beim Billard in den Ecken aufgefangen werden.


Ich glaub der wollte es schon so wie er es geschrieben hat :wink: .
Sylvus Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 195



BeitragVerfasst: Do 06.03.08 01:13 
Titel: Der, der das so wollte
schreibt jetzt mal noch nen Schlusssatz. :)
Also mit den Ecken war richtig, jetzt kamen nur noch zwei Seitenlöcher dazu.
Das mit dem h:=-h; stimmt und funktioniert auch super, danke!
Hier nochmal der funktionierende Quellcode:
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
//[...]
var
  Form1: TForm1;
  x1,y1,h,h1:integer;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
Timer1.Enabled:=true;

x1:=random(300)+100; y1:=random(300)+100;
h:=random(10); h1:=random(10);
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
Image1.Canvas.pen.color:=clgreen;
Image1.Canvas.brush.color:=clgreen;
Image1.Canvas.Ellipse(x1,y1,x1+20,y1+20);
if ((x1>380)AND(y1<20)) OR ((x1>380)AND(y1>349)) then
begin
if ((x1<400)AND(y1<20)) OR ((x1<400)AND(y1>349)) then
 Timer1.Enabled:=false;
end;
if ((x1<20)AND(y1<20)) OR ((x1<20)AND(y1>349)) OR ((x1>729)AND(y1<20)) OR ((x1>729)AND(y1>349)) then
  Timer1.Enabled:=false;
if (y1<17) OR (y1>351) then
  h1:=-h1;
if (x1<17) OR (x1>741) then
  h:=-h;
x1:=x1+h;
y1:=y1-h1;
Image1.Canvas.pen.color:=clwhite;
Image1.Canvas.brush.color:=clwhite;
Image1.Canvas.Ellipse(x1,y1,x1+20,y1+20);
end;
end.