Autor Beitrag
Dragon06
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 25



BeitragVerfasst: Mi 06.12.06 14:37 
also wir sollen für die schule ein programm erstellen in dem ein kleiner button (bzw ein "ball") ^^ durch den bildschirm wandert und an allen ecken abprallt! also obne unten rechts und links soll er reflektieren! habe es aber bisher nicht ganz hingekriegt... er reflektiert zwar oben links in der ecke, aber wandert nicht in die anderen ecken... und unten prallt er außerhalb des fensters ab...

naja seht selbst...

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:
41:
42:
43:
44:
45:
46:
implementation
  Var
    vx:integer;
    vy:integer;

{$R *.DFM}

procedure TForm1.Button_startClick(Sender: TObject);
  begin
    vx:=-4;
    vy:=4;
  Timer1.Enabled:=True;
  end;

procedure TForm1.Timer1Timer(Sender: TObject);
   begin
    Button.left:=Button.left+vx;

  If Button.left<0
  Then
    begin
      Button.left:=-Button.left;
      vx:=-vx;
    end;

  If Button.left>552
  Then
    begin
      Button.left:=-Button.left;
      vy:=-vy;
    end;

    button.top:=button.top+vx;
    if button.top<0
    then
      begin
        button.top:=-button.top;
        vy:=-vy;
      end;




  end;

end.
Dragonclaw
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 196

Windows Vista
Delphi 7 Prof.
BeitragVerfasst: Mi 06.12.06 15:04 
Hallo!
Ich hab erstmal die Fehler makiert und, Sachen hinzu gefügt damit der Korrekt an allen Kanten abprallt.

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:
41:
42:
43:
44:
45:
46:
47:
implementation
  Var
    vx:integer;
    vy:integer;

{$R *.DFM}

procedure TForm1.Button_startClick(Sender: TObject);
  begin
    vx:=-4;
    vy:=4;
  Timer1.Enabled:=True;
  end;

procedure TForm1.Timer1Timer(Sender: TObject);
   begin
    Button.left:=Button.left+vx;

  If Button.left<0 Then
    begin
      Button.left:=1;
      vx:=-vx;
    end;

  If Button.left>500 Then
    begin
      Button.left:=499;
      vx:=-vx;
    end;

    button.top:=button.top+vx;

    if button.top<0 then
      begin
        button.top:=1;
        vy:=-vy;
      end;

    
    if button.top <500 then
      begin
        button.top:=499;
        vy:=-vy;
      end;

  end;

end.


So dich Sache ist jetzt nur das das Programm ziemlich langweilig ist. Denn so wie das da oben steht wandert er immer nur auf einer Diagonalen über die Form. Es wäre besser, wenn die Button nach dem Prinzip "Einfallswinkel = Ausfallswinkel" abprallen würde ODER in einem zufälligen Winkel zurück kommen würde.
Dragon06 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 25



BeitragVerfasst: Mi 06.12.06 15:19 
hmm ja stimmt, anders kann es ja eigentlich auch garnicht gehen dass der ball an allen seiten abprallt ^^ sonst würde er ja nur an 2 seiten abprallen!
aber wenn ich den quelltext jetzt veränder wie du es meintest, dann wird es irgendwie noch komisch ^^ der ball starten dann noch nicht mal im fenster drin :/
Dragonclaw
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 196

Windows Vista
Delphi 7 Prof.
BeitragVerfasst: Mi 06.12.06 15:29 
Gib mal als Heigth und Width für die Form Werte ÜBER 500 ein.
Dragon06 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 25



BeitragVerfasst: Mi 06.12.06 15:41 
user profile iconDragonclaw hat folgendes geschrieben:
Gib mal als Heigth und Width für die Form Werte ÜBER 500 ein.


hab ich...
also der ball prallt an der wand nach unten (außerhalb des fensters) ab und kommt dann wieder zurück! hmm
Dragonclaw
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 196

Windows Vista
Delphi 7 Prof.
BeitragVerfasst: Mi 06.12.06 15:43 
Dann zieh mal das Fenster so groß das du siehst wo er abprallt. Bzw mach die Zahlen WO er abprallt einfach kleiner
Dragon06 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 25



BeitragVerfasst: Mi 06.12.06 16:12 
welche zahlen jetzt? o_O
und würde der so jetzt an allen seiten/ecken abprallen?
Dragonclaw
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 196

Windows Vista
Delphi 7 Prof.
BeitragVerfasst: Mi 06.12.06 16:45 
du änderst einfach die 500 in 200 um, und die 499 in 199:
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:
41:
42:
43:
44:
45:
46:
47:
implementation
  Var
    vx:integer;
    vy:integer;

{$R *.DFM}

procedure TForm1.Button_startClick(Sender: TObject);
  begin
    vx:=-4;
    vy:=4;
  Timer1.Enabled:=True;
  end;

procedure TForm1.Timer1Timer(Sender: TObject);
   begin
    Button.left:=Button.left+vx;

  If Button.left<0 Then
    begin
      Button.left:=1;
      vx:=-vx;
    end;

  If Button.left>200Then
    begin
      Button.left:=199;
      vx:=-vx;
    end;

    button.top:=button.top+vx;

    if button.top<0 then
      begin
        button.top:=1;
        vy:=-vy;
      end;

    
    if button.top <200 then
      begin
        button.top:=199;
        vy:=-vy;
      end;
  end;

end.


und ja so prallt er an allen Ecken ab, nur da der Code in immer von links oben nach rechts unten schickt brauch man das eigentlich garnicht
Dragon06 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 25



BeitragVerfasst: Mi 06.12.06 16:54 
ja ok danke jetzt ist's wenigstens im fenster drin ^^ aber irgendwie auch nich richtig! er prallt unten z.b. auch nich gegen die kante sondern vor der kante kehrt er schon wieder um! ich glaub irgendwas is da grundlegend falsch oder? :-/

also mein ziel ist, dass der ball quer durch's bild wandert und gegen alle wände prallt (also zufällige bewegungen)...

halt wie so n flummi den man in nem eckigen raum rumwirft xD
GTA-Place
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
EE-Regisseur
Beiträge: 5248
Erhaltene Danke: 2

WIN XP, IE 7, FF 2.0
Delphi 7, Lazarus
BeitragVerfasst: Mi 06.12.06 17:10 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
if Button.Top > Form1.Height - Button.Height then
  // ...

if Button.Left > Form1.Width - Button.Width then
  // ...

_________________
"Wer Ego-Shooter Killerspiele nennt, muss konsequenterweise jeden Horrorstreifen als Killerfilm bezeichnen." (Zeit.de)
Dragon06 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 25



BeitragVerfasst: Mi 06.12.06 17:38 
*nachdenk* oO sry das check ich jetzt nicht ^^
tut mir leid aber mit dem programm komm ich grad garnich klar :/
wie kann ich denn überhaupt einstellen das er in irgendeinem zufälligen winkel zurück prallt? und wie definiert man nun genau die verschiedenen wände? also links rechts oben und unten??

mfg
GTA-Place
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
EE-Regisseur
Beiträge: 5248
Erhaltene Danke: 2

WIN XP, IE 7, FF 2.0
Delphi 7, Lazarus
BeitragVerfasst: Mi 06.12.06 17:39 
user profile iconDragon06 hat folgendes geschrieben:
und wie definiert man nun genau die verschiedenen wände? also links rechts oben und unten??mfg

Genau das hab ich grad gemacht. Form1.Width = Rechte Wand.

_________________
"Wer Ego-Shooter Killerspiele nennt, muss konsequenterweise jeden Horrorstreifen als Killerfilm bezeichnen." (Zeit.de)
Dragon06 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 25



BeitragVerfasst: Mi 06.12.06 17:51 
hmmm das hilft mir aber irgendwie auch nich weiter.. hmmm.. naja also thx schon mal! verstehen tu ich's aber trotzdem nicht :D wo und was muss ich denn da rein schreiben? könntest du das mal in den quellcode mit rein schreiben, da wo es hin gehört? das wäre nice! weil irgendwie krieg ich das ohne hilfe grad nich so wirklich gebacken
GTA-Place
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
EE-Regisseur
Beiträge: 5248
Erhaltene Danke: 2

WIN XP, IE 7, FF 2.0
Delphi 7, Lazarus
BeitragVerfasst: Mi 06.12.06 18:26 
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:
25:
26:
27:
28:
29:
30:
procedure TForm1.Timer1Timer(Sender: TObject);  
begin  
  Button.Left := Button.Left + VX;

  if Button.Left < 0 then
  begin
    Button.Left := 1;
    VX := -VX;
  end;

  if Button.Left > Form1.Width - Button.Left then
  begin
    Button.Left := Form1.Width - Button.Left;
    VX := -VX;
  end;

  Button.Top := Button.Top + VY;

  if Button.Top < 0 then
  begin
    Button.Top := 1;
    VY := -VY;
  end;

  if Button.Top > Form1.Height - Button.Height then
  begin
    Button.top := Form1.Height - Button.Height;
    VY := -VY;
  end;
end;

_________________
"Wer Ego-Shooter Killerspiele nennt, muss konsequenterweise jeden Horrorstreifen als Killerfilm bezeichnen." (Zeit.de)
Jetstream
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 222



BeitragVerfasst: Mi 06.12.06 18:37 
So schwer isses nun auch nicht, wenn du verstanden hast, was im Timer abläuft:
Dort wird in jedem Durchlauf der aktuelle Vektor (vx,vy) auf die Position des Buttons addiert.

Sobald jedoch der Button eine Kante trifft, wird die entsprechende Komponente im Vektor umgedreht.

Wann diese Kante getroffen ist, hängt von der Größe deines Fenster (Form1.height bzw Form1.width) und der Größe deines Buttons ab (Button1.height bzw Button1.width).

Beachte dabei, dass die Position des Buttons (Button1.Left bzw Button1.Top) immer an seiner linken oberen Ecke bestimmt wird.

Der Button hat die rechte Kante also getroffen, sobald gilt: Button1.Left+Button1.width > Form1.width

Lass dir das mal auf der Zunge zergehen und progge es dann analog für die untere Kante.

_________________
Die folgenden Klangbeispiele sind Ergänzungen zum methodischen Aufbau der Textbeilage und dürfen nicht losgelöst von dieser behandelt werden.
Dragon06 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 25



BeitragVerfasst: Mi 06.12.06 19:10 
GTA-Place, danke für den quellcode!

Jetstream, ok danke hab's verstanden wie du das meinst... eine frage noch, also links heißt es ja button.left und oben button.top ... und rechts und links?? weil wenn ich right und below angebe kommt "undefinierter bezeichner"
jakobwenzel
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1889
Erhaltene Danke: 1

XP home, ubuntu
BDS 2006 Prof
BeitragVerfasst: Mi 06.12.06 19:21 
Right und Below gibts nich, das musste aus Links/Oben und Breite/Höhe errechnen.

_________________
I thought what I'd do was, I'd pretend I was one of those deaf-mutes.
GTA-Place
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
EE-Regisseur
Beiträge: 5248
Erhaltene Danke: 2

WIN XP, IE 7, FF 2.0
Delphi 7, Lazarus
BeitragVerfasst: Mi 06.12.06 19:48 
ausblenden Delphi-Quelltext
1:
2:
Rechts := Button.Left + Button.Width;
Unten := Button.Top + Button.Height;

_________________
"Wer Ego-Shooter Killerspiele nennt, muss konsequenterweise jeden Horrorstreifen als Killerfilm bezeichnen." (Zeit.de)
Jetstream
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 222



BeitragVerfasst: Mi 06.12.06 19:55 
So schwer isses nicht ...

Nehmen wir mal an, es gäbe ein Button1.right, dann würde gelten: Button1.right - Button1.left = Button1.width

Jetzt überleg dir mal, was du anstelle von Button1.right auch noch schreiben kannst ;)

// Edit: Buuuuh, GTA-Place hat alles verraten :(

_________________
Die folgenden Klangbeispiele sind Ergänzungen zum methodischen Aufbau der Textbeilage und dürfen nicht losgelöst von dieser behandelt werden.
Dragon06 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 25



BeitragVerfasst: Mi 06.12.06 20:14 
xD ok...

nur wenn ich "Button.Right" in "Button.Left + Button.Width" ersetzte kommt die fehlermeldung "Inkompatible Typen" :/ was is das jetzt schon wieder xD oh man heute krieg ich aber auch garnichts hin ^^