Autor Beitrag
150gozilla
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 37

Windows XP Pro
Delphi5 Pro
BeitragVerfasst: Mo 10.10.05 12:26 
Hallo, ich habe ein Problem.
Und zwar, habe ich eine Form, und da bewegt sich ein Kreis von Links nach Rechts.
So wie bekomm ich es hin, das er am ende der Form wieder in die andere richtung läuft?

Hier mal mein Code:

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:
var
  Form1: TForm1;
  x1: Integer = 10;
  x2: Integer = 60;
  y1: Integer = 100;
  y2: Integer = 150;

##############################################

procedure TForm1.Timer1Timer(Sender: TObject);
begin
 Canvas.Ellipse(x1,y1,x2,y2);
 Inc(x1,2);
 Inc(x2,2);
 Canvas.Ellipse(x1,y1,x2,y2)
end;

procedure TForm1.FormPaint(Sender: TObject);
begin
 Canvas.Pen.Mode := pmCopy;
 Canvas.Brush.Color := clRed;
 Canvas.Rectangle(200,50,400,200);
 Canvas.Pen.Mode := pmXOR;
 Canvas.Ellipse(x1,y1,x2,y2 );
 Timer1.enabled := True
end;


Moderiert von user profile iconKlabautermann: Code- durch Delphi-Tags ersetzt

_________________
Lieber locker vom Hocker als hecktisch übern Ecktisch


Zuletzt bearbeitet von 150gozilla am Mo 10.10.05 13:06, insgesamt 1-mal bearbeitet
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: Mo 10.10.05 13:00 
user profile icon150gozilla hat folgendes geschrieben:
Hallo, ich habe ein Problem.
Und zwar, habe ich keine Form, und da ...

alles was dann folgt macht für mich keinen sinn mehr :?

ausblenden Delphi-Quelltext
1:
2:
if left + width > form1.width then
  andersrum //bzw x + radius
150gozilla Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 37

Windows XP Pro
Delphi5 Pro
BeitragVerfasst: Mo 10.10.05 13:04 
user profile iconF34r0fTh3D4rk hat folgendes geschrieben:
user profile icon150gozilla hat folgendes geschrieben:
Hallo, ich habe ein Problem.
Und zwar, habe ich keine Form, und da ...

alles was dann folgt macht für mich keinen sinn mehr :?

ausblenden Delphi-Quelltext
1:
2:
if left + width > form1.width then
  andersrum //bzw x + radius


sorry, ich meinte "eine"

_________________
Lieber locker vom Hocker als hecktisch übern Ecktisch
Horst_H
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1654
Erhaltene Danke: 244

WIN10,PuppyLinux
FreePascal,Lazarus
BeitragVerfasst: Di 11.10.05 09:40 
Hallo,

Statt mit Konstanten solltest Du einfach mit globalen Variablen dx,dy arbeiten.
Statt x1,x2 lieber x1 und x1+r....
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:
const 
  r = 90 ;
var   
  Form1: TForm1;   
  dx : Integer = 2;   
  dy : Integer = 2;   

  x1: Integer = 10;   
  y1: Integer = 60; 
    

##############################################   

procedure TForm1.Timer1Timer(Sender: TObject);   
begin   
 Canvas.Ellipse(x1,y1,x1+r,y1+r);   
 Inc(x1,dx);
Falls (X1 kleiner 0) oder (x1+r groesser Weite von Canvas) dann 
   begin
   dec(x1,dx+dx);
   dx := -dx
   end;

 Inc(y1,dy);
   
Falls (Y1 kleiner 0) oder (Y1 Groesser (Höhe von Canvas-r)) dann 
   begin
   dec(Y1,dy+dy);
   dy := -dy
   end;
 Canvas.Ellipse(x1,y1,x1+r,y1+r)   
end;   

procedure TForm1.FormPaint(Sender: TObject);   
begin   
 Canvas.Pen.Mode := pmCopy;   
 Canvas.Brush.Color := clRed;   
 Canvas.Rectangle(200,50,400,200);   
 Canvas.Pen.Mode := pmXOR;   
 Canvas.Ellipse(x1,y1,x1+r,y1+r );   
 Timer1.enabled := True   
end;


Da ich das jetzt nicht testen kann, musst du es selbst probieren.
Falls Width und height unterschiedlich sind laueft der Kreis auf unterschiedlichen Bahnen . Am besten teilerfremde Zahlen waehlen.

Gruss Horst
150gozilla Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 37

Windows XP Pro
Delphi5 Pro
BeitragVerfasst: Di 11.10.05 11:14 
mhhh,d as funktioniert nicht.... trozdemd anke

_________________
Lieber locker vom Hocker als hecktisch übern Ecktisch
Horst_H
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1654
Erhaltene Danke: 244

WIN10,PuppyLinux
FreePascal,Lazarus
BeitragVerfasst: Di 11.10.05 13:52 
Hallo,

funktioniert das ontimer ohne die If´s ?
Wird ein Kreis gezeichnet?

Gruss Horst
Logikmensch
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 390

Win XP
Delphi 2007 Prof., XE2, XE5
BeitragVerfasst: Mi 19.10.05 05:53 
Titel: Ein Bällchen - ein Bällchen
Hallo!

Ich will hier nicht den Supermacker raushängen lassen, aber da ich den Compiler gerade offen hatte, habe ich mal das Programm von Horst_H reingezogen und kleine Optimierungen gemacht. Der Ball ist ein bisschen kleiner, weil er sich sonst gleich am Rand verfranst. :) Also bitte nicht ärgern, wenn ich hier gleich den fertigen Source präsentiere.

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:
55:
56:
57:
58:
59:
60:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Timer1: TTimer;
    procedure FormCreate(Sender: TObject);
    procedure FormPaint(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private
    { Private-Deklarationen }
    x1,y1,dx,dy:integer;
    r:integer;
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Timer1Timer(Sender: TObject);
begin
 Canvas.Ellipse(x1-r,y1-r,x1+r,y1+r); //alte Ellipse löschen
 if (X1-r<0) or (x1+r>clientwidth) then dx := -dx;
 if (Y1<0) or (Y1+r>clientheight) then dy := -dy;
 Inc(x1,dx);
 Inc(y1,dy);
 Canvas.Ellipse(x1-r,y1-r,x1+r,y1+r); //neue Ellipse zeichnen
end;

procedure TForm1.FormPaint(Sender: TObject);
begin
 Canvas.Pen.Mode := pmCopy;
 Canvas.Brush.Color := clRed;
 Canvas.Rectangle(200,50,400,200);
 Canvas.Pen.Mode := pmXOR;
 Canvas.Ellipse(x1-r,y1-r,x1+r,y1+r); //Ellipse das erste Mal neu zeichnen
 Timer1.Interval:=5;
 Timer1.enabled := True
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  x1:=clientwidth div 2;
  y1:=clientheight div 2;
  dx:=1;
  dy:=1;
  r:=10;
end;

end.


Ich habe mir erlaubt, den Timer schneller zu stellen, dafür die Steprate auf 1 zu reduzieren. Aber es funzt.

Liebe Grüße,

Claus.
AXMD
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 4006
Erhaltene Danke: 7

Windows 10 64 bit
C# (Visual Studio 2019 Express)
BeitragVerfasst: Mi 19.10.05 06:46 
@Logikmensch: ein paar with-do hätten deinem Code gegen Ende hin sicherlich nicht geschadet ;)

AXMD
Logikmensch
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 390

Win XP
Delphi 2007 Prof., XE2, XE5
BeitragVerfasst: Mi 26.10.05 05:16 
Titel: with..do
Hallo AXMD!

Ja, damit hast Du recht. Ich benutze normalerweise auch with..do, aber keine Ahnung, warum ich es hier nicht anwendete. Ich gelobe Besserung. :oops:
Nachtrag: Der Code mit den vielen Canvas.* stammt aber von 150Gozilla. :-)

Liebe Grüße,

Claus.