Autor Beitrag
Dragon777
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 41

XP Prof
D3 Prof
BeitragVerfasst: Di 23.08.05 11:40 
Hallo leute bin neu hier und anfänger im programmieren.
Ich bräuchte eure hilfe: und zwar will ich das sich mein kreis zufällig im formular
bewegt und an der Wand abprallt.

Oder wie kann ich die bewgung des kreises mit den pfeiltasten der tastatur kontrolieren?

Kann mir einer sagen wie ich das machen kann?
Hier ist mal mein bisheriger 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:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
unit Unit1;

interface

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

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

const
  Fmax = 5;
  Farbe: Array[0..Fmax-1] of TColor =
    (clred, clblue, clyellow,clwhite,clblack);

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
  // Hintergrundfarbe
  Color := clblack;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
var x,y,Dicke: Integer;
begin
   // Startpunkt und Durchmesser
   x := 55;
   y := 250;
   Dicke :=  (50);
   // mit Farbe gefüllter Kreis
   canvas.Brush.color := farbe[random(4)];
   canvas.Ellipse (x, y, x+Dicke, y+dicke);
end;


end.



Danke schonmal im voraus :)


Zuletzt bearbeitet von Dragon777 am Di 23.08.05 12:48, insgesamt 1-mal bearbeitet
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Di 23.08.05 12:14 
Zunächst mal: Ich helfe gerne bei der Umsetzung.

ABER: Überleg dir mal, was eigentlich passieren muss, und das mal unabhängig davon, wie das dann in Delphi geht.

Beispiel: Du bewegst den Kreis ein Stück in eine bestimmte Richtung. Dann musst du testen, ob du am Rand des Formulars angekommen bist. Dann muss ggf. die Richtung geändert werden.

Also musst du die Richtung auch in einer Variablen haben.

Frage: Wie siehts mit den mathematischen Grundlagen dafür aus? Also du musst praktisch den Mittelpunkt des Kreises an einer Geraden entlang verschieben. Den Kreis darum zu zeichnen ist das wenigste.

Gegeben hast du also den aktuellen Punkt als x und y, die Richtung in Form eines Winkels, und die Strecke, um die verschoben werden soll.
Kannst du daraus den neuen Punkt errechnen?
rochus
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 416

Win XP Prof, Fedora Core 4, SuSE 7.0
D7 Ent, D2005 Pers
BeitragVerfasst: Di 23.08.05 12:18 
du musst in deinem ontimer prüfen, ob du an die "Ecke" deines formulars stößt und wenn ja, dann musst du einfach nur je nachdem an welche ecke du stößt statt +x eine -x und statt +y ein -y nehmen:

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:
{ pseudo code! }

type
  TMeinFormular = class(TForm)
  private
    FVorzeichenY, FVorzeichenX: Integer;
  end;


constructor TMeinFormular.Create;
begin
  FVorzeichenY := 1;
  FVorzeichenX := 1;
end;

procedure Timer1Timer;
  function HitTextY:
  begin
    { prüft, ob der kreis an die "wand" von Y kommt und liefer true zurück, wenn ja }    
  end;

  function HitTestX:
  begin
    { wie oben nur mit X }
  end;
begin
  if HitTestY then FVorzeichenY := FVorzeichenY*-1;
  if HitTestX then FVorzeichenX := FVorzeichenX*-1;

  Kreis.X := Kreis.X + (wert * FVorzeichenX);
  Kreis.Y := Kreis.Y + (wert * FVorzeichenY);
end;


oder so...

_________________
Im Nachhinein ist man immer ein Schlauch!
"Dream as if you'll live forever, live as if you'll die today!" James Dean
Dragon777 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 41

XP Prof
D3 Prof
BeitragVerfasst: Di 23.08.05 12:31 
rochus des funktioniert leider net so bei mir.

jaenicke
entschudilge bitte ,aber ich weiß net wie ich des umsetzen soll was du mir geschrieben hast.
rochus
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 416

Win XP Prof, Fedora Core 4, SuSE 7.0
D7 Ent, D2005 Pers
BeitragVerfasst: Di 23.08.05 12:57 
PSEUDO CODE!
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:
48:
49:
50:
51:
52:
53:
54:
const
  DISTANCE = 10;
  SIZE_X   = 5;
  SIZE_Y   = 5;

type
  TForm1 = class(TForm)

  public
   { Public-Deklarationen }
   kPosX: Integer;
   kPosY: Integer;

   vY, vX: Integer;
  end;



procedure TForm1.Timer1Timer;
  function HitTestX: Boolean;
  begin
   Result := (kPosX>Form1.ClientWidth) or (kPosX<0);
  end;

  function HitTestY: Boolean;
  begin
   Result := (kPosY>Form1.ClientHeight) or (kPosY<0);
  end;

begin
  kPosX := kPosX + (DISTANCE * vX);
  kPosY := kPosY + (DISTANCE * vY);

  if HitTestX then begin
    vX := vX*-1;
    kPosX := kPosX + (DISTANCE * vX * 2);
  end;
  if HitTestY then begin
    vY := vY*-1;
    kPosY := kPosY + (DISTANCE * vY * 2);
  end;


  Canvas.Brush.Color := clRed;
  Canvas.Ellipse(kPosX, kPosY, kPosX+SIZE_X, kPosY+SIZE_Y);
end;

procedure TForm1.FormCreate;
begin
  kPosX := 0;
  kPosY := 0;
  vY := 1;
  vX := 1;
end;

_________________
Im Nachhinein ist man immer ein Schlauch!
"Dream as if you'll live forever, live as if you'll die today!" James Dean
Amateur
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 777

(Win98, WinMe) WinXP Prof
D3 Prof, D6 Pers, D2k5 Pers., Turbo C++ Explorer
BeitragVerfasst: Di 23.08.05 13:01 
die beiden haben aber recht...
ich hab nur ma so drüber geguckt...
is mir nur aufgefallen dass in dem code nen kleiner fehler is, kann sein dass noch mehr drin sind jedenfalls:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
function HitTextY:
  begin
    { prüft, ob der kreis an die "wand" von Y kommt und liefer true zurück, wenn ja }    
  end;

  function HitTestX:


müsste so sein:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
function HitTestY;
  begin
    { prüft, ob der kreis an die "wand" von Y kommt und liefer true zurück, wenn ja }    
  end;

  function HitTestX;


-> also zum einen muss es hittesty heißen damit das mit dem teil drunter passt und nach hittesty sowie hittestx muss nen semikolen kein doppelpunkt...

weiß net ob noch mehr solcher fehler drin sind..

ansonsten sind die ideen der beiden richtig...

du musst immer prüfenlassen ob der kreis an den rand kommt oder schon drüber is und dann die richtung ändern..

und das es sich zufällig bewegt kann man mit random machen...

das mit winkeln und so weiß ich net...

ansonsten kommt es drauf an ob du den kreis mit canvas darstellst oder mit nem image
bei nem image kann man den rand einfach per image.left und image.width bzw. für y dann image.top und image.height überprüfen..

bei nem canvas is das nen bisschen mehr rechenarbeit..

auf jedenfall sollteste dir erstma ne skizze machen mit den variablen die du zum zeichnen und überprüfen und verschieben brauchst dann is das ganze einfacher als wenn man sich das immer vorstellen muss..

das wären so meine tipps an dich ansonsten poste code rein dann kann man dir eher sagen was dran falsch is..

ok EDIT: jetzt hat rochus die fehler von oben in seinem zweiten code rausgenommen.. nix für ungut...
mfg amateur
F34r0fTh3D4rk
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 5285
Erhaltene Danke: 29

Win Vista (32), Win 7 (64)
Eclipse, SciTE, Lazarus
BeitragVerfasst: Di 23.08.05 13:59 
Dragon777 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 41

XP Prof
D3 Prof
BeitragVerfasst: Di 23.08.05 13:59 
sorry leute habs noch net gerafft muß wohl erst noch en paar tutorials durchackern.

trotzdem danke.
Dragon777 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 41

XP Prof
D3 Prof
BeitragVerfasst: Di 23.08.05 15:01 
Danke F34r0fTh3D4rk das hat mir geholfen
F34r0fTh3D4rk
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 5285
Erhaltene Danke: 29

Win Vista (32), Win 7 (64)
Eclipse, SciTE, Lazarus
BeitragVerfasst: Di 23.08.05 18:46 
ich versuche schon seit ich angefangen habe mit delphi was mit bällen zu machen, aber nie bekomme ichs hin, diesmal bin ich aber schon ein stück weiter 8)