* * *

Author Topic: computer sience class in school  (Read 3318 times)

bradyhartsfield

  • New member
  • *
  • Posts: 41
computer sience class in school
« on: December 27, 2017, 12:19:13 am »
hey,
in my informatics lesson in school we had to program something in which we have an image. if u click on a button, one ellipse in form of a circle shows up. its moving and bouncing off the borders of the image. now our exercise was to add another ellipse (in form of a circle) and if they get in range of each other they should bounce back. we did that with the sentence of the pythagoras. now we had to add another 18 ellipses to have a total of 20 ellipses.
my problem is that i didnt understand how to use arrays and how to use them in my program and to be honest i didnt even understand my program, i just copied it from my friend because i was ill when they did that.

Code: Pascal  [Select]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
  9.   StdCtrls, ColorBox;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Button1: TButton;
  17.     Button2: TButton;
  18.     ColorListBox1: TColorListBox;
  19.     ColorListBox2: TColorListBox;
  20.     Image1: TImage;
  21.     Timer1: TTimer;
  22.     Timer2: TTimer;
  23.     procedure Button1Click(Sender: TObject);
  24.     procedure Button2Click(Sender: TObject);
  25.     procedure FormCreate(Sender: TObject);
  26.     procedure Image1MouseDown(Sender: TObject; Button: TMouseButton;
  27.       Shift: TShiftState; X, Y: Integer);
  28.     procedure Timer1Timer(Sender: TObject);
  29.     procedure Timer2Timer(Sender: TObject);
  30.  
  31.   private
  32.     { private declarations }
  33.   public
  34.     { public declarations }
  35.   end;
  36.  
  37. var
  38.   Form1: TForm1;
  39.   i,mx,my,r2,mx2,my2,vx2,vy2,Freix,Freiy,r1,mx1,my1,vx1,vy1:Integer;   //types set as integer
  40.   r,x,y,vx,vy: Array [1..20] of integer;   //array?
  41.  
  42.  
  43.  
  44. implementation
  45.  
  46. {$R *.lfm}
  47.  
  48. { TForm1 }
  49.  
  50. procedure TForm1.Button1Click(Sender: TObject);
  51. begin
  52.   Timer1.Enabled:=true;                       //activates timer (makes ellipses move)
  53. end;
  54.  
  55. procedure TForm1.Button2Click(Sender: TObject);
  56. begin
  57.   timer1.Enabled:=false;                       //deactivates timer (makes ellipses stop)
  58. end;
  59.  
  60. procedure TForm1.FormCreate(Sender: TObject);
  61. begin
  62.   doublebuffered:=true;        //reducing the lags
  63.    mx:=20;           //setting values
  64.    my:=20;
  65.    mx2:=20;
  66.    vx2:=6;
  67.    r2:=25;
  68.    my2:=748;
  69.    vy2:=-6;
  70.    r1:=10;
  71.    mx1:=10;
  72.    my1:=10;
  73.    timer1.enabled:=false; //timer at beginning deactivated
  74.  
  75.   for i := 1 to 20 do begin       //???
  76.        r[i]:= 5;
  77.        x[i]:=random(image1.width);
  78.        y[i]:=random(image1.height);
  79.        vx[i]:=random(10)-5;
  80.        vy[i]:=random(8)-4;
  81.        image1.canvas.ellipse(x[i]-r[i],y[i] - r[i], x[i] + r[i], y[i]+r[i]);
  82.   end;
  83. end;
  84.  
  85. procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
  86.   Shift: TShiftState; X, Y: Integer);
  87. begin
  88.   timer2.enabled:=true
  89. end;
  90.  
  91. procedure TForm1.Timer1Timer(Sender: TObject);
  92. begin
  93.   with image1.canvas do begin                         //creates the first ellipse
  94.   pen.color:=clblack ;
  95.   brush.color:=clblack;
  96.    ellipse(mx-r,my-r,mx+r,my+r);
  97.  
  98.         mx:=mx+vx;
  99.         my:=my+vy;
  100.  
  101.  pen.color:=$FFFFFF;
  102.  brush.color:=$FFFFFF;
  103.  ellipse(mx-r,my-r,mx+r,my+r);
  104.  
  105.   end;
  106.  
  107.   if my+r>=image1.Height       //makes it bounce off the borders
  108.   thenvy:=-vy;
  109.   if mx+r>=image1.width
  110.   then vx:=-vx;
  111.   if my-r<0
  112.   then vy:=-vy;
  113.   if mx-r<0
  114.   then vx:=-vx;
  115.  
  116.    with image1.canvas do begin        //creates the second ellipse?
  117.          pen.color:=clblack;
  118.          brush.color:=clblack    ;
  119.          image1.canvas.ellipse(mx2-r2,my2-r2,mx2+r2,my2+r2);
  120.            mx2:=mx2+vx2;
  121.         my2:=my2+vy2;
  122. pen.color:=$FFFFFF;
  123. brush.color:=$FFFFFF;
  124. ellipse(mx2-r2,my2-r2,mx2+r2,my2+r2);
  125.  
  126.    end;
  127.  
  128.   if my2+r2>=image1.Height         //makes the second ellipse bounce off the border?
  129.   then vy2:=-vy2;
  130.   if mx2+r2>=image1.width
  131.   then vx2:=-vx2;
  132.   if my2-r2<0
  133.   then vy2:=-vy2;
  134.   if mx2-r2<0
  135.   then vx2:=-vx2;
  136.  
  137.    if
  138.   sqrt(sqr(mx-mx2)+sqr(my-my2))<=r+r2    //pythagoras to let them bounce off of each other when coming too close
  139.   then  begin
  140. Freix:=vx;    //???
  141. vx:=vx2;
  142. vx2:=Freix;
  143.  
  144. Freiy:=vy;
  145. vy:=vy2;
  146. vy2:=Freiy;
  147.   end;
  148. end;
  149.  
  150. procedure TForm1.Timer2Timer(Sender: TObject);
  151. begin
  152.               with image1.canvas do begin            //???
  153.   pen.color:=clblack ;
  154.   brush.color:=clblack;
  155.    ellipse(mx1-r1,my1-r1,mx1+r1,my1+r1);
  156.  pen.color:=$FFFFFF;
  157.  brush.color:=$FFFFFF;
  158.  ellipse(mx1-r1,my1-r1,mx1+r1,my1+r1);
  159.              end;
  160. end;
  161.  
  162. end.
  163.  

thats the code, i wrote some notes after the slashes to try to understand it but i dont really get it. i dont want a perfect written program to copy, i just want a solution and i would create a new (and better) program with help but i am kind of messed up at the moment.

regards
brady (hodge was already in use)

taazz

  • Hero Member
  • *****
  • Posts: 4651
Re: computer sience class in school
« Reply #1 on: December 27, 2017, 08:08:48 am »
hey,
in my informatics lesson in school we had to program something in which we have an image. if u click on a button, one ellipse in form of a circle shows up. its moving and bouncing off the borders of the image. now our exercise was to add another ellipse (in form of a circle) and if they get in range of each other they should bounce back. we did that with the sentence of the pythagoras. now we had to add another 18 ellipses to have a total of 20 ellipses.
my problem is that i didnt understand how to use arrays and how to use them in my program and to be honest i didnt even understand my program, i just copied it from my friend because i was ill when they did that.

regards
brady (hodge was already in use)
lets break the code down to its components.
1) the needed components to draw the circle are.
  a) mx = x coordinate for the center of the circle.
  b) my = y coordinate for the center of the circle.
  c) r1  = radious of the circle.
  d) vx = speed and velocity in the X axis
  e) vy = speed and velocity in the Y axis
a timer on the form (a control that runs a method(procedure or function of an object) on predefined intervals) will execute call the code to calculate the next position of the circles.
To check if the circle has touched the right side of the image you simple calculate the right most point of the circle rp = (mx+r1) and if the point is beyond the right border (rp>= image.width) then it touches otherwise it does not. if it touches then reverse the Velocity on the X axis (vx = -vx). The same thing goes for all 4 borders of the image but on the top and bottom borders you reverse the velocity on the y axis (vy:=-vy) keep in mind that regardless of border (top or bottom in this case) the velocity reversion is always vy:=-vy.
Now knowing that for a single circle you apply it 19 more times having mx1 for the first and mx2 for the second etc or you create a record for the each circle that holds the required data something along the lines of
Code: Pascal  [Select]
  1. type
  2.   TMyCircle = record
  3.     mX : Integer;
  4.     mY : Integer;
  5.     R   : Integer;
  6.     vX : Integer;
  7.     vY : Integer;
  8.   end;
  9.  
and then you create an array for that record eg
Code: Pascal  [Select]
  1. var
  2.   MyCircles = array[0..19] of TMyCircle;
  3.  
or something along those lines and for each record in the array you test it against all the other records to see if there is one that might collide with it in any direction and with the borders of the image. That's all there is to it.
« Last Edit: December 27, 2017, 08:11:23 am by taazz »
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Thaddy

  • Hero Member
  • *****
  • Posts: 5200
Re: computer sience class in school
« Reply #2 on: December 27, 2017, 09:42:15 am »
My stepson is - by now - a game design graduate bachelor and I thought him such things like yours in box2d.
You can install box2d and look at the examples. I believe it is in the online package manager and the examples are clear and easy to use.
"Logically, no number of positive outcomes at the level of experimental testing can confirm a scientific theory, but a single counterexample is logically decisive."

bradyhartsfield

  • New member
  • *
  • Posts: 41
Re: computer sience class in school
« Reply #3 on: December 28, 2017, 01:04:26 am »
hey,
okay i am gonna check that stuff out tomorrow, but what do you mean with "create a new record". I dont get that part and where to add new code..?

regards
brady

taazz

  • Hero Member
  • *****
  • Posts: 4651
Re: computer sience class in school
« Reply #4 on: December 28, 2017, 09:33:08 pm »
hey,
okay i am gonna check that stuff out tomorrow, but what do you mean with "create a new record". I dont get that part and where to add new code..?

regards
brady
I have already demonstrated the record definition in my last post that is the "creation" eg.

Code: Pascal  [Select]
  1. type
  2.   TCircleRec = record
  3.     mX : Integer;
  4.     mY : Integer;
  5.     R   : Integer;
  6.     vX : Integer;
  7.     vY : Integer;
  8.   end;
  9.  
  10. function NewCircle(aCenterX,aCenterY, aRadious, aVelocityX, aVelocityY:integer):TCircleRec;
  11. begin
  12.   Result.mX := aCenterX;
  13.   Result.mY := aCenterY;
  14.   Result.R   := aRadious;
  15.   Result.vX := aVelocityX;
  16.   Result.vY := aVelocityY;
  17. end;
  18.  
  19. var
  20.   vDataArray : Array of TCircleRec;
  21.  
  22. Procedure initArray(const aLength:integer);
  23. var
  24.   vCntr : integer;
  25. begin
  26.   Randomize;
  27.   setLength(vDataArray, alength);
  28.   for vCntr := low(vdataArray) to High(vDataArray) do
  29.     vDataArray[vCntr] := NewCircle(random(640),Random(480), RAndom(100), random(20), random(20)):TCircleRec;
  30. end;
  31.  
records are stack based variables so after definition you declare a variable of that type like
Code: Pascal  [Select]
  1. var
  2.   vCircle : TCircleRec;
  3.  
that will define a variable long enough to hold all the needed data, you access the fields like this
Code: Pascal  [Select]
  1.   vCircle.mX := 10;
  2.   vCircle.mY := 10;
  3.   vCircle.R   := 8;
  4.   vCircle.vX := 6;
  5.   vCircle.vY := 6;
  6.  
everything you need to know to accomplish your task is in my posts. If you have problems with anything ask.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

bradyhartsfield

  • New member
  • *
  • Posts: 41
Re: computer sience class in school
« Reply #5 on: December 28, 2017, 09:57:27 pm »
hey,

thanks for your help, i watched some tutorials now and understood your way now.
but in our task my teacher especially told us to use arrays and we havent had records and results in our lessons.

i now know a way to fix my problem but not the way we have to fix it.

regards
brady

bradyhartsfield

  • New member
  • *
  • Posts: 41
Re: computer sience class in school
« Reply #6 on: December 28, 2017, 10:06:09 pm »
hey,

now i tried to add this:
Code: Pascal  [Select]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   doublebuffered := true;
  4.   mx := 10;
  5.   my := 10;
  6.   r := 10;
  7.   vx := 1;
  8.   vy := 1;
  9.   circles[1].mx1 := 10;
  10.   circles[1].my1 := 10;
  11.   circles[1].r1 := 10;
  12.   circles[1].vx1 := 1;
  13.   circles[1].vy1 :=1;
  14.   timer1.enabled := false;
  15. end;    

but it keeps saying "illegal qualifier" and it underlines "mx1", "my1", etc.

my var´s look like this:
Code: Pascal  [Select]
  1.   mx, my, r, vx, vy : integer;
  2.   mx1, my1, r1, vx1, vy1 : integer;
  3.   circles: array[1..19] of integer;

regards
brady


taazz

  • Hero Member
  • *****
  • Posts: 4651
Re: computer sience class in school
« Reply #7 on: December 28, 2017, 10:08:01 pm »
hey,

thanks for your help, i watched some tutorials now and understood your way now.
but in our task my teacher especially told us to use arrays and we havent had records and results in our lessons.

i now know a way to fix my problem but not the way we have to fix it.

regards
brady
in that case create an array for each field for example;
Code: Pascal  [Select]
  1. var
  2.   mX : array[0..19] of integer;
  3.   mY : array[0..19] of integer;
  4.   R : array[0..19] of integer;
  5.   vX : array[0..19] of integer;
  6.   vY : array[0..19] of integer;
  7. procedure initarrays;
  8. var
  9.   vCntr:Integer;
  10. begin
  11.   for vCntr := 0 to 19 do begin
  12.     mX[vCntr] := random(640); //select a random number  between 1 and 640;
  13.     mY[vCntr] := random(480); //select a random number  between 1 and 480;
  14.     R[vCntr]  := random(8);   //select a random number  between 1 and 8 for radious;
  15.     vX[vCntr] := random(80);  //select a random number  between 1 and 80;
  16.     vY[vCntr] := random(80);  //select a random number  between 1 and 80;
  17.   end;
  18. end;
  19.  

PS: Oh I see you have changed the first post to the new code you are on the right track.
« Last Edit: December 28, 2017, 10:10:01 pm by taazz »
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

bradyhartsfield

  • New member
  • *
  • Posts: 41
Re: computer sience class in school
« Reply #8 on: December 28, 2017, 10:23:56 pm »
hey,

okay that really helped me!

but if i put it like this
Code: Pascal  [Select]
  1. var
  2.   Form1: TForm1;
  3.   mx, my, r, vx, vy : integer;
  4.   mx1 : array[0..19] of integer;
  5.   my1 : array[0..19] of integer;
  6.   r1 : array[0..19] of integer;
  7.   vx1 : array[0..19] of integer;
  8.   vy1 : array[0..19] of integer;
  9.   circles : integer;          
Code: Pascal  [Select]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   doublebuffered := true;
  4.   mx := 10;
  5.   my := 10;
  6.   r := 10;
  7.   vx := 1;
  8.   vy := 1;
  9.   circles[mx1] := random(image1.width);
  10.   circles[my1] := random(image1.height);
  11.   circles[r1] := 5;
  12.   circles[vx1] := 1;
  13.   circles[vy1] :=1;
  14.   timer1.enabled := false;
  15. end;

it now says "illegal qualifier" and underlines the mx1, my1 etc of circles[mx1]:= random(image1.width);

regards
brady

molly

  • Hero Member
  • *****
  • Posts: 2214
Re: computer sience class in school
« Reply #9 on: December 28, 2017, 10:28:45 pm »
it now says "illegal qualifier" and underlines the mx1, my1 etc of circles[mx1]:= random(image1.width);
You are referring to circles as if it were an array (but in fact it is an integer). Look at user taazz' previous code snippet. The answer is there.

taking too long for the obvious:
Code: [Select]
    mX[vCntr] := random(640); //select a random number  between 1 and 640;
    mY[vCntr] := random(480); //select a random number  between 1 and 480;
    R[vCntr]  := random(8);   //select a random number  between 1 and 8 for radious;
    vX[vCntr] := random(80);  //select a random number  between 1 and 80;
    vY[vCntr] := random(80);  //select a random number  between 1 and 80;
Where VCntrl is a value 'looping' from 0 to 19. That is the exact same range as the indexes of your declared arrays.

Shame is that your own nature/logic is more or less telling you to use an array of records while your teacher does not allow it (as per your request). You can't fight logic/nature  :D
« Last Edit: December 28, 2017, 10:55:25 pm by molly »

molly

  • Hero Member
  • *****
  • Posts: 2214
Re: computer sience class in school
« Reply #10 on: December 28, 2017, 11:09:06 pm »
@bradyhartsfield
forget about video tutorials. Try to read and understand this what is written on arrays. The tutorial discusses each aspect and elements of (one dimensional) arrays.

If you do not understand what is written there then create some small new project(s) and experiment/play with the example code that is listed.

bradyhartsfield

  • New member
  • *
  • Posts: 41
Re: computer sience class in school
« Reply #11 on: December 28, 2017, 11:22:39 pm »
hey,

okay thanks, maybe i am having trouble because i am not speaking english as my mother language but thanks anyways.

regards
brady

molly

  • Hero Member
  • *****
  • Posts: 2214
Re: computer sience class in school
« Reply #12 on: December 28, 2017, 11:33:58 pm »
My native tongue isn't English either  :)

A heads up though: most programming languages uses English for its basic constructs.

Try to use google/bing (or whatever) translator or try to find some good tutorial(s) written in your native tongue.

Note that for basic constructs such as variable declarations, arrays etc. you can use any documentation that is written for Delphi as these tutorials/documentation apply for Free Pascal as well. Delphi is usually more wide-spread than Free Pascal.

PS: strange thing for a Stephen King reader   ;D
« Last Edit: December 28, 2017, 11:39:00 pm by molly »

bradyhartsfield

  • New member
  • *
  • Posts: 41
Re: computer sience class in school
« Reply #13 on: December 28, 2017, 11:46:32 pm »
hey,

this works now, but its creating infinite ellipses and they arent moving. they arent moving because i dont know how to insert
Code: Pascal  [Select]
  1. mx1:=mx1+vx1;
  2.       my1:=my1+vy1;
into my code.

Code: Pascal  [Select]
  1. for c := 0 to 19 do begin
  2.       mX1[c] := random(image1.width);
  3.       mY1[c] := random(image1.height);
  4.       R1[c]  := 10;
  5.       vX1[c] := 1;
  6.       vY1[c] := 1;
  7.       image1.canvas.ellipse(mx1[c]-r1[c], my1[c] - r1[c], mx1[c] + r1[c], my1[c]+r1[c]);
  8.       mx1:=mx1+vx1;
  9.       my1:=my1+vy1;
  10. end;  

as you can guess the
Code: Pascal  [Select]
  1.       mx1:=mx1+vx1;
  2.       my1:=my1+vy1;
at the bottom doesnt do its job.

in addition to that, when everything works and i have 20 ellipses moving and bouncing off the borders, i need to add the pythagoras. my biggest problem atm is that i dont know where to add the required code.

regards
brady

PS: Haha i hoped someone would realize it! :D But seriously, i just liked the name, and thats the only thing, from him!

« Last Edit: December 28, 2017, 11:53:48 pm by bradyhartsfield »

taazz

  • Hero Member
  • *****
  • Posts: 4651
Re: computer sience class in school
« Reply #14 on: December 28, 2017, 11:54:31 pm »
hey,

this works now, but its creating infinite ellipses and they arent moving. they arent moving because i dont know how to insert
Code: Pascal  [Select]
  1. mx1:=mx1+vx1;
  2.       my1:=my1+vy1;
into my code.

Code: Pascal  [Select]
  1. for c := 0 to 19 do begin
  2.       mX1[c] := random(image1.width);
  3.       mY1[c] := random(image1.height);
  4.       R1[c]  := 10;
  5.       vX1[c] := 1;
  6.       vY1[c] := 1;
  7.       image1.canvas.ellipse(mx1[c]-r1[c], my1[c] - r1[c], mx1[c] + r1[c], my1[c]+r1[c]);
  8.       mx1:=mx1+vx1;
  9.       my1:=my1+vy1;
  10. end;  

as you can guess the
Code: Pascal  [Select]
  1.       mx1:=mx1+vx1;
  2.       my1:=my1+vy1;
at the bottom doesnt do its job.

regards
brady

PS: Haha i hoped someone would realize it! :D But seriously, i just liked the name, and thats the only thing, from him!
Now I feel that you are not trying to understand. I'm sorry you are on your own.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus