Illegal qualifierand the j and the i is marked.
questions:You need to 'switch' (better worded swap) the movement of the two circles. No idea what you would like to do in a hypothetical second switch.
do i need two of those? so switch1 and switch2?
I can't compile your code. I got this error:that is because (x, x) and (y, y) is missing behind the both "switch".
questions:You need to 'switch' (better worded swap) the movement of the two circles. No idea what you would like to do in a hypothetical second switch.
do i need two of those? so switch1 and switch2?
I found many bugs in the code. Give me some time, I will write a detail bug fixes and suggestions.
why is there at the part with the two "r`s" summed up a "+12" at the end?That becomes clear when you remove the addition ;)
why is there at the part with the two "r`s" summed up a "+12" at the end?That becomes clear when you remove the addition ;)
Not enough, the code still buggy.Yes, i know :). but TS is starting to get on the right track (seeing from where it all started). That TS is allowed to use events in a pascal program but not records... a choice/restriction i will never understand though as it makes things much easier to read/understand.
As usually I will write a detail step-by-step explanation.Yes, thank you for that.
Be patient. :)I will. If not i would already have posted my (not 100% accurate and using record) code on the third or fourth post when TS started this topic in the other thread :)
Very interesting attempt on elastic collision....
vx[j] := -vx[j]; vy[j] := -vy[j];
Not exactly correct I know but it shows the problem with the current approach.Very interesting attempt on elastic collision....
vx[j] := -vx[j]; vy[j] := -vy[j];
Not exactly correct I know but it shows the problem with the current approach.Fair enough :)
procedure pythagoras; var d : real; var j,i, vx, vy : integer; begin for j := 1 to 20 do for i := 2 to 20 do begin d := sqrt(sqr(mx[j]-mx[i])+sqr(my[j]-my[i])); if d <= r[j]+r[i]+12 then begin vx[j] := -vx[j]; vy[j] := -vy[j]; end; end; end;
Step 1 - Remove Useless Items
These should be removed:
- Image2
- Timer2
- Global variables: zwischenspeicher1, zwischenspeicher2, c, c2, j, i, d
Note:
Good programmers usually avoid using global variables, use global variable only when you really need it.
Because variable c has been removed from global scope, now add the variable c it into:
- Procedure TForm1.FormCreate
- Procedure TForm1.Timer1Timer
This code still buggy. But you can download the code for comparison. I also formatted the code to make it more readable.
unit Unit1; {$mode objfpc}{$H+} interface uses Classes, Forms, Controls, Graphics, StdCtrls, ExtCtrls; type { TForm1 } TForm1 = class(TForm) Button1: TButton; Button2: TButton; Image1: TImage; Timer1: TTimer; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure FormCreate(Sender: TObject); procedure Timer1Timer(Sender: TObject); end; var Form1 : TForm1; mx : array[1..20] of integer; my : array[1..20] of integer; vx : array[1..20] of integer; vy : array[1..20] of integer; r : array[1..20] of integer; implementation {$R *.lfm} { TForm1 } procedure TForm1.Button1Click(Sender: TObject); begin Timer1.Enabled := True; end; procedure TForm1.Button2Click(Sender: TObject); begin Timer1.Enabled := False; end; procedure switch (Var a,b:integer); var zwischenspeicher: Integer; begin zwischenspeicher := a; a := b; b := zwischenspeicher; end; procedure pythagoras; var d: Real; j,i, vx, vy : Integer; begin for j := 1 to 20 do for i := 2 to 20 do begin d := sqrt(sqr(mx[j]-mx[i])+sqr(my[j]-my[i])); if d <= r[j]+r[i]+12 then begin switch(vx, vx); switch(vy, vy); end; end; end; procedure TForm1.FormCreate(Sender: TObject); var c: Integer; begin Randomize; Form1.Color := clPurple; DoubleBuffered := True; Timer1.Enabled := False; for c := 1 to 20 do begin r[c] := 20; mx[c] := Random(Image1.Width); my[c] := Random(Image1.Height); vx[c] := Random(10); vy[c] := Random(10); end; end; procedure TForm1.Timer1Timer(Sender: TObject); var c: Integer; begin with image1.canvas do begin for c := 1 to 20 do begin Pen.Color := clBlack; Brush.Color := clBlack; Ellipse(mx[c]-r[c], my[c]-r[c], mx[c]+r[c], my[c]+r[c]); mx[c] := mx[c] + vx[c]; my[c] := my[c] + vy[c]; Pen.Color := clPurple; Brush.Color := clPurple; Ellipse(mx[c]-r[c], my[c]-r[c], mx[c]+r[c], my[c]+r[c]); end; end; for c:= 1 to 20 do begin if my[c]+r[c] >= Image1.Height then vy[c] := -vy[c]; if mx[c]+r[c] >= Image1.Width then vx[c] := -vx[c]; if my[c]-r[c] < 0 then vy[c] := -vy[c]; if mx[c]-r[c] < 0 then vx[c] := -vx[c]; end; end; end.
mx[i] := Random(Image1.Width);
my[i] := Random(Image1.Height);
Set anchoring for the image and resize the form, add these code into TFrom1.FormCreate:for what is that? because i dont just want to copy but to understand why i am doing things.
Image1.Anchors := [akTop, akLeft, akRight, akBottom];
Width := 800;
Height := 600;
[…]FYI, you can shorten/simplify that expression with Math.hypot (https://freepascal.org/docs-html/current/rtl/math/hypot.html) to
d := sqrt(sqr(mx[j]-mx[i])+sqr(my[j]-my[i]));