Recent

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

bradyhartsfield

  • New Member
  • *
  • Posts: 41
Re: computer sience class in school
« Reply #15 on: December 28, 2017, 11:56:56 pm »
ok i will try

HeavyUser

  • Sr. Member
  • ****
  • Posts: 397
Re: computer sience class in school
« Reply #16 on: December 29, 2017, 08:36:33 am »
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
just a push to the right direction here is the code you provided with all the unneeded variables removed.
Code: Pascal  [Select][+][-]
  1. var
  2.   Form1: TForm1;
  3.   mx1 : array[0..19] of integer;
  4.   my1 : array[0..19] of integer;
  5.   r1 : array[0..19] of integer;
  6.   vx1 : array[0..19] of integer;
  7.   vy1 : array[0..19] of integer;
  8.   circles : integer;
  9. procedure TForm1.FormCreate(Sender: TObject);
  10. begin
  11.   doublebuffered := true;
  12.   for Circles := 0 to 19 do begin
  13.     mx1[circles] := random(image1.width);
  14.     my1[circles] := random(image1.height);
  15.     r1[circles] := 5;
  16.     vx1[circles] := 1;
  17.     vy1[circles] :=1;
  18.   end;
  19.   timer1.enabled := false;
  20. end;
and of course the timer code should only access the array too eg
Code: Pascal  [Select][+][-]
  1.       mx1[circles]:=mx1[circles]+vx1[circles];
  2.       my1[circles]:=my1[circles]+vy1[circles];
  3.  

bradyhartsfield

  • New Member
  • *
  • Posts: 41
Re: computer sience class in school
« Reply #17 on: December 29, 2017, 11:14:17 am »
hey,

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;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Button1: TButton;
  17.     Button2: TButton;
  18.     Image1: TImage;
  19.     Timer1: TTimer;
  20.     Timer2: TTimer;
  21.     procedure Button1Click(Sender: TObject);
  22.     procedure Button2Click(Sender: TObject);
  23.     procedure FormCreate(Sender: TObject);
  24.     procedure Timer1Timer(Sender: TObject);
  25.   private
  26.  
  27.   public
  28.  
  29.   end;
  30.  
  31. var
  32.   Form1: TForm1;
  33.   mx, my, r, vx, vy : integer;
  34.   mx1 : array[0..19] of integer;
  35.   my1 : array[0..19] of integer;
  36.   r1 : array[0..19] of integer;
  37.   vx1 : array[0..19] of integer;
  38.   vy1 : array[0..19] of integer;
  39.   c : integer;
  40.  
  41. implementation
  42.  
  43. {$R *.lfm}
  44.  
  45. { TForm1 }
  46.  
  47. procedure TForm1.Button1Click(Sender: TObject);
  48. begin
  49.   timer1.enabled := true;
  50. end;
  51.  
  52. procedure TForm1.Button2Click(Sender: TObject);
  53. begin
  54.   timer1.enabled := false;
  55. end;
  56.  
  57. procedure TForm1.FormCreate(Sender: TObject);
  58. begin
  59.   doublebuffered := true;
  60.   mx := 10;
  61.   my := 10;
  62.   r := 10;
  63.   vx := 7;
  64.   vy := 7;
  65.   timer1.enabled := false;
  66.   for c := 0 to 19 do begin
  67.     mx1[c] := random(image1.width);
  68.     my1[c] := random(image1.height);
  69.     r1[c] := 10;
  70.     vx1[c] := 7;
  71.     vy1[c] :=7;
  72.   end;
  73. end;
  74.  
  75. procedure TForm1.Timer1Timer(Sender: TObject);
  76. begin
  77.   with image1.canvas do begin
  78.     pen.color := clblack;
  79.     brush.color := clblack;
  80.     ellipse(mx-r, my-r, mx+r, my+r);
  81.     mx:=mx+vx;
  82.     my:=my+vy;
  83.     pen.color:=clred;
  84.     brush.color:=clred;
  85.     ellipse(mx-r, my-r, mx+r, my+r);
  86.  
  87.     pen.color := clblack;
  88.     brush.color := clblack;
  89.     ellipse(mx1[c]-r1[c], my1[c]-r1[c], mx1[c]+r1[c], my1[c]+r1[c]);
  90.     mx1[c]:=mx1[c]+vx1[c];
  91.     my1[c]:=my1[c]+vy1[c];
  92.     pen.color:= clred;
  93.     brush.color:=clred;
  94.     ellipse(mx1[c]-r1[c], my1[c]-r1[c], mx1[c]+r1[c], my1[c]+r1[c]);
  95.  
  96.   end;
  97.  
  98.   if my+r>=image1.height
  99.   then vy:= -vy;
  100.   if mx+r>=image1.width
  101.   then vx:=-vx;
  102.   if my-r<0
  103.   then vy:=-vy;
  104.   if mx-r<0
  105.   then vx:=-vx;
  106.  
  107.   if my1[c]+r1[c]>=image1.height
  108.   then vy1[c]:= -vy1[c];
  109.   if mx1[c]+r1[c]>=image1.width
  110.   then vx1[c]:=-vx1[c];
  111.   if my1[c]-r1[c]<0
  112.   then vy1[c]:=-vy1[c];
  113.   if mx1[c]-r1[c]<0
  114.   then vx1[c]:=-vx1[c];
  115.  
  116.  
  117.  
  118.  
  119.   end;
  120.  
  121.  
  122.  
  123. end.
  124.  

Now I have to circles, bouncing off the borders, but the second one "spawns" at the same spot always and its only the second, sorry for not understanding but i am looking for a forum in my language now cause i think i am pissing off everyone here.

regards
brady

UPDATE: i managed to add the pythagoras and now the 2 bounce off of eachother when they touch.
« Last Edit: December 29, 2017, 11:27:46 am by bradyhartsfield »

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: computer sience class in school
« Reply #18 on: December 29, 2017, 02:01:56 pm »
Sorry for not understanding but i am looking for a forum in my language now cause i think i am pissing off everyone here.
You did not piss off anyone. but you are here to get help and too much help will always be an obstacle in your growth. Some things you need to figure out for your self.
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: 14200
  • Probably until I exterminate Putin.
Re: computer sience class in school
« Reply #19 on: December 29, 2017, 04:57:16 pm »
Indeed, we are here to help but we are not here to do your classes: then you would have learned nothing which would be bad, don't you think?
Note that if you are following Object Pascal classes the risk that your teacher reads this forum is *very high*! So he would spot that you did not do the work yourself...
(This happens often..)

That's why we help, give advice, but never do your homework.
« Last Edit: December 29, 2017, 07:00:42 pm by Thaddy »
Specialize a type, not a var.

bradyhartsfield

  • New Member
  • *
  • Posts: 41
Re: computer sience class in school
« Reply #20 on: December 29, 2017, 07:13:55 pm »
Hey,
okay thanks, i am trying to finish this on my own.

regards
brady

bradyhartsfield

  • New Member
  • *
  • Posts: 41
Re: computer sience class in school
« Reply #21 on: December 29, 2017, 07:57:29 pm »
hey,

i have a question: do i have to repeat all my steps 20 times? isnt that the thing we wanted to avoid? because i spent some time thinking about everything and i tried something in a copy of my program and everything i tried didnt work (i dont want you to show me the solution i just want a "yes" or a "no" to my question above).

regars
brady

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
Re: computer sience class in school
« Reply #22 on: December 30, 2017, 12:07:12 am »
hey,

i have a question: do i have to repeat all my steps 20 times? isnt that the thing we wanted to avoid? because i spent some time thinking about everything and i tried something in a copy of my program and everything i tried didnt work (i dont want you to show me the solution i just want a "yes" or a "no" to my question above).

regars
brady

you know functions or procedures?

you know how to use a for loop?

bradyhartsfield

  • New Member
  • *
  • Posts: 41
Re: computer sience class in school
« Reply #23 on: December 30, 2017, 12:46:55 am »
thanks, now i have something to look up and something i can work with.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: computer sience class in school
« Reply #24 on: December 30, 2017, 03:33:17 am »
Programming is not so difficult as it might seem to be at first sight. It is all about logic.

Translating that logic into workable code might perhaps be a more daunting task, but for that we can offer help.

i have a question: do i have to repeat all my steps 20 times?
If you would not use a computer and/or programing language but would have to calculate things using your calculator, and drawing the circles on paper (one sheet per frame). How would you solve the problem then ?

If drawing circles to paper is too complicated, just pick some coins from your wallet and place them on a table and start moving them around and try to follow/understand the flow of the (moving) coins. Visualize and ask yourself what happens when two coins 'touches' each other ? And what happens if 3 coins touches each other at the same time ? And how do we 'know' that one or more coins touches at the same time ? How many times would you have to 'verify' whether two coins touched each other or not ?

Quote
isnt that the thing we wanted to avoid?
If you were able to answer the questions above then you know the answer, and also know why it would be impossible to avoid the repetition. Having said that, a programming language offers a solution for that and that solution is named "a loop".

Quote
because i spent some time thinking about everything and i tried something in a copy of my program and everything i tried didnt work (i dont want you to show me the solution i just want a "yes" or a "no" to my question above).
I am guessing that the answer that you seek for your question would be yes. but as i wrote above (and written by user Lainz, and earlier in this thread shown with code by user taazz) you can use a loop for that.

Seems to me that you're just trying as you get along in the hope something will eventually work without actually understanding a) the problem at hand and b) the basic constructs that the pascal language offers.

Both are something that you could improve upon.

You said you have working code for two ellipses that are able to bounce off each other (and "sidewalls"). imho that would be a much better starting point to introduce yourself to arrays and subsequently loops.

The whole point (at least one of them) of a loop is to avoid repetition and place repetitive statements inside that loop so that you (as a programmer) do not have to repeat the same statements over and over again.

btw: i've noticed somewhere the use of variables named Freix and Freiy which seems to indicate your native tongue is German ? There are excellent German forums that support Delphi and/or Free pascal and for sure there are German users around (not to mention the existence of books about Delphi/Pascal written in the German language).

Thaddy

  • Hero Member
  • *****
  • Posts: 14200
  • Probably until I exterminate Putin.
Re: computer sience class in school
« Reply #25 on: December 30, 2017, 08:35:29 am »
Or why not start with this excellent tutorial about the pong game.
Contains simple drawing, basic collision detection, direction and border handling.
http://lazplanet.blogspot.nl/2014/10/create-pong-game-for-yourself.html

You can not use it as is: you have more requirements, but it has all the elements you need in very simple code.
With that as a basis you should be able to solve your task.

Don't see we didn't help...
« Last Edit: December 30, 2017, 08:44:46 am by Thaddy »
Specialize a type, not a var.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: computer sience class in school
« Reply #26 on: December 30, 2017, 08:59:39 am »
It clear to me that providing source code is counter intuitive for the OP. No mater how many code examples we post it will only get worst from here on. The only thing he can do is ignore everything that was done so far through away all the code he has and write the app from the start on his 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

bradyhartsfield

  • New Member
  • *
  • Posts: 41
Re: computer sience class in school
« Reply #27 on: December 30, 2017, 01:35:51 pm »
hey,

in every tutorial they use 
Code: Pascal  [Select][+][-]
  1. student_height[1]:=10
  2. student_height[2]:=12
  3. ...
  4. ...
  5. student_height[10]:=20
but in my program i use c for everything, like
Code: Pascal  [Select][+][-]
  1. mx1[c]:=...
  2. my1[c]:=...
  3. ...
  4. ...
  5. ...
why is that? because i looked it up and its easier to create a loop with saying
Code: Pascal  [Select][+][-]
  1. for i := 1 to 10 do
but however that doesnt work in my case?

regards
brady

Thaddy

  • Hero Member
  • *****
  • Posts: 14200
  • Probably until I exterminate Putin.
Re: computer sience class in school
« Reply #28 on: December 30, 2017, 01:43:54 pm »
well, if you must use c and c is an ordinal type then write:
Code: Pascal  [Select][+][-]
  1. for c := 1 to 10 do
  2.   ...
  3.  

OTOH you declare your arrays as zero based (correct) so probably:
Code: Pascal  [Select][+][-]
  1. for c := 0 to 19 do...

You can also change/rename c to i....  Since c is an integer and i (from Index) is more natural for programmers (any language)... Doesn't matter too much, you can use c if you want but then use c as i... as I demonstrated...
« Last Edit: December 30, 2017, 01:50:07 pm by Thaddy »
Specialize a type, not a var.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: computer sience class in school
« Reply #29 on: December 30, 2017, 01:56:07 pm »
hey,

in every tutorial they use 
Code: Pascal  [Select][+][-]
  1. student_height[1]:=10
  2. student_height[2]:=12
  3. ...
  4. ...
  5. student_height[10]:=20
but in my program i use c for everything, like
Code: Pascal  [Select][+][-]
  1. mx1[c]:=...
  2. my1[c]:=...
  3. ...
  4. ...
  5. ...

why is that?
Twofold:
1) To show you how to (correctly) index an array
2) because there is no (direct) relation between individual students heights

Quote
because i looked it up and its easier to create a loop with saying
Code: Pascal  [Select][+][-]
  1. for i := 1 to 10 do

Because there is no direct relationship between individual student heights it makes no sense to assign them in a loop. _If_ on the other hand those students heights would have been read from a file or where provided by user input _then_ it would make sense to use a loop.

Quote
but however that doesnt work in my case?
See Thaddy's answer... but with an additional note to avoid those pesky index out of range errors:

Code: [Select]
for c := Low(NameOfArray) to High(NameOfArray) do
And there is even a more modern construct possible, but let's not go there right now because i fear that i'm already making things harder for you by showing the use of Low/High.
« Last Edit: December 30, 2017, 02:00:30 pm by molly »

 

TinyPortal © 2005-2018