* * *

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

Handoko

  • Hero Member
  • *****
  • Posts: 1918
  • My goal: build my own game engine using Lazarus
Re: computer sience class in school
« Reply #30 on: December 30, 2017, 02:00:44 pm »
... but however that doesnt work in my case?

I read your code on post #17 and I found some very serious issues:
- You didn't use any for-loop for doing the painting
- The variable "c" you used is not initialized
- Many duplicate/useless variables (mx, my, r, vx, vy)

bradyhartsfield

  • New member
  • *
  • Posts: 41
Re: computer sience class in school
« Reply #31 on: December 30, 2017, 02:02:18 pm »
hey,
okay i will try to work with the new informations you gave me and thanks for the advices!
regards
brady

bradyhartsfield

  • New member
  • *
  • Posts: 41
Re: computer sience class in school
« Reply #32 on: December 30, 2017, 02:09:50 pm »
hey,
i did it, i just need to program the bouncing off borders, i will show you when i have done it !!!!

regards
brady

bradyhartsfield

  • New member
  • *
  • Posts: 41
Re: computer sience class in school
« Reply #33 on: December 30, 2017, 02:15:35 pm »
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.     Button3: TButton;
  19.     Image1: TImage;
  20.     Timer1: TTimer;
  21.     Timer2: TTimer;
  22.     procedure Button1Click(Sender: TObject);
  23.     procedure Button2Click(Sender: TObject);
  24.     procedure Button3Click(Sender: TObject);
  25.     procedure FormCreate(Sender: TObject);
  26.     procedure Timer1Timer(Sender: TObject);
  27.   private
  28.  
  29.   public
  30.  
  31.   end;
  32.  
  33. var
  34.   Form1: TForm1;
  35.   mx, my, r, vx, vy, pyth1, pyth2 : integer;
  36.   mx1 : array[1..19] of integer;
  37.   my1 : array[1..19] of integer;
  38.   r1 : array[1..19] of integer;
  39.   vx1 : array[1..19] of integer;
  40.   vy1 : array[1..19] of integer;
  41.   c : integer;
  42.  
  43. implementation
  44.  
  45. {$R *.lfm}
  46.  
  47. { TForm1 }
  48.  
  49. procedure TForm1.Button1Click(Sender: TObject);
  50. begin
  51.   timer1.enabled := true;
  52. end;
  53.  
  54. procedure TForm1.Button2Click(Sender: TObject);
  55. begin
  56.   timer1.enabled := false;
  57. end;
  58.  
  59. procedure TForm1.Button3Click(Sender: TObject);
  60. begin
  61.   //zurücksetzen des programms
  62. end;
  63.  
  64. procedure TForm1.FormCreate(Sender: TObject);
  65. begin
  66.   randomize;   //lässt alle zahlen neu berechnen
  67.   doublebuffered := true;  //reduziert lags
  68.   mx := 10;  //legt werte fest
  69.   my := 10;
  70.   r := 15;
  71.   vx := 7;
  72.   vy := 7;
  73.   timer1.enabled := false;     //deaktiviert ersten timer
  74. for c := 1 to 19 do begin     //array
  75.    mx1[c] := random(951);
  76.    my1[c] := random(656);
  77.    r1[c] := random(15);
  78.    vx1[c] := random(15);
  79.    vy1[c] := random(15);
  80.   end;
  81. end;
  82.  
  83. procedure TForm1.Timer1Timer(Sender: TObject);
  84. begin
  85.   with image1.canvas do begin
  86.  
  87.     pen.color := clblack;
  88.     brush.color := clblack;
  89.     ellipse(mx-r, my-r, mx+r, my+r);
  90.     mx:=mx+vx;
  91.     my:=my+vy;
  92.     pen.color:=clblue;
  93.     brush.color:=clblue;
  94.     ellipse(mx-r, my-r, mx+r, my+r);
  95.  
  96.  
  97.    for c:= 1 to 19 do begin
  98.    pen.color := clblack;
  99.    brush.color := clblack;
  100.    ellipse(mx1[c]-r1[c], my1[c]-r1[c], mx1[c]+r1[c], my1[c]+r1[c]);
  101.    mx1[c]:=mx1[c]+vx1[c];
  102.    my1[c]:=my1[c]+vy1[c];
  103.    pen.color:= clblue;
  104.    brush.color:= clblue;
  105.    ellipse(mx1[c]-r1[c], my1[c]-r1[c], mx1[c]+r1[c], my1[c]+r1[c]);
  106.    end;
  107.  
  108.  
  109.  
  110.  
  111.   end;
  112.  
  113.   if my+r>=image1.height       //lässt von den wänden abprallen
  114.   then vy:= -vy;
  115.   if mx+r>=image1.width
  116.   then vx:=-vx;
  117.   if my-r<0
  118.   then vy:=-vy;
  119.   if mx-r<0
  120.   then vx:=-vx;
  121.  
  122.   for c:= 1 to 19 do begin
  123.   if my1[c]+r1[c]>=image1.height       //lässt von den wänden abprallen
  124.   then vy1[c]:= -vy1[c];
  125.   if mx1[c]+r1[c]>=image1.width
  126.   then vx1[c]:=-vx1[c];
  127.   if my1[c]-r1[c]<0
  128.   then vy1[c]:=-vy1[c];
  129.   if mx1[c]-r1[c]<0
  130.   then vx1[c]:=-vx1[c];
  131.  
  132.  if
  133.   sqrt(sqr(mx-mx1[c])+sqr(my-my1[c]))<=r+r1[c]   //pythagoras um die kugeln bei berührung von sich abprallen zu lassen
  134.  
  135.  then  begin            //kehrt Geschwindigkeiten um
  136.   pyth1:=vx;
  137.   vx:=vx1[c];
  138.   vx1[c]:=pyth1;
  139.   pyth2:=vy;
  140.   vy:=vy1[c];
  141.   vy1[c]:=pyth2;
  142.   end;
  143.  end;
  144. end;
  145.  
  146.  
  147.  
  148. end.
  149.  

hey,

it works now, i have 20 circles and they are bouncing off the borders, now i want to try to add the pythagoras so they bounce off each other as well!

regards
brady

PS: Thank you very very very much for all your help!

wp

  • Hero Member
  • *****
  • Posts: 4074
Re: computer sience class in school
« Reply #34 on: December 30, 2017, 02:47:11 pm »
Code: Pascal  [Select]
  1. var
  2.   mx1 : array[1..19] of integer;
  3.   my1 : array[1..19] of integer;
  4.   r1 : array[1..19] of integer;
  5.   vx1 : array[1..19] of integer;
  6.   vy1 : array[1..19] of integer;
[/code]
i have 20 circles
You say you have 20 circles, but your array indexes run from 1 to 19 - how many circles can be stored in these arrays?
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

bradyhartsfield

  • New member
  • *
  • Posts: 41
Re: computer sience class in school
« Reply #35 on: December 30, 2017, 02:56:45 pm »
hey,

and one is created manually.

regards
brady

bradyhartsfield

  • New member
  • *
  • Posts: 41
Re: computer sience class in school
« Reply #36 on: December 30, 2017, 03:18:46 pm »
hey,

i made a new application and started from 0. this is where i am now: i reorganized the code and worked with an array 1..20 and not 1..19 because its more clear.

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 Image1Click(Sender: TObject);
  25.     procedure Timer1Timer(Sender: TObject);
  26.   private
  27.  
  28.   public
  29.  
  30.   end;
  31.  
  32. var
  33.   Form1: TForm1;
  34.   mx : array[1..20] of integer;
  35.   my : array[1..20] of integer;
  36.   vx : array[1..20] of integer;
  37.   vy : array[1..20] of integer;
  38.   r  : array[1..20] of integer;
  39.   pyth1, pyth2, c : integer;
  40.  
  41.  
  42. implementation
  43.  
  44. {$R *.lfm}
  45.  
  46. { TForm1 }
  47.  
  48. procedure TForm1.Button1Click(Sender: TObject);
  49. begin
  50.   timer1.enabled := true;
  51. end;
  52.  
  53. procedure TForm1.Button2Click(Sender: TObject);
  54. begin
  55.   timer1.enabled := false;
  56. end;
  57.  
  58. procedure TForm1.FormCreate(Sender: TObject);
  59. begin
  60.   randomize;
  61.   doublebuffered := true;
  62.   timer1.enabled := false;
  63.   for c :=  1 to 20 do begin
  64.     mx[c] := random(image1.width);
  65.     my[c] := random(image1.height);
  66.     vx[c] := random(10);
  67.     vy[c] := random(10);
  68.     r[c] := 10;
  69.   end;
  70. end;
  71.  
  72. procedure TForm1.Image1Click(Sender: TObject);
  73. begin
  74.  
  75. end;
  76.  
  77. procedure TForm1.Timer1Timer(Sender: TObject);
  78. begin
  79.   with image1.canvas do begin
  80.     for c := 1 to 20 do begin
  81.       pen.color := clblack;
  82.       brush.color:=clblack;
  83.       ellipse(mx[c]-r[c], my[c]-r[c], mx[c]+r[c], my[c]+r[c]);
  84.       mx[c]:=mx[c]+vx[c];
  85.       my[c]:=my[c]+vy[c];
  86.       pen.color := clblue;
  87.       brush.color := clblue;
  88.       ellipse(mx[c]-r[c], my[c]-r[c], mx[c]+r[c], my[c]+r[c]);
  89.     end;
  90.   end;
  91.  
  92.   for c:= 1 to 20 do begin
  93.     if my[c]+r[c]>=image1.height       //lässt von den wänden abprallen
  94.     then vy[c]:= -vy[c];
  95.     if mx[c]+r[c]>=image1.width
  96.     then vx[c]:=-vx[c];
  97.     if my[c]-r[c]<0
  98.     then vy[c]:=-vy[c];
  99.     if mx[c]-r[c]<0
  100.     then vx[c]:=-vx[c];
  101.  
  102.   end;
  103. end;
  104.  
  105.  
  106. end.
  107.  

regards
brady

EDIT: everything works, now i will try to implement the pythagoras.
EDIT 2: thats the point where i need to use timer 2 as a kind of dummy right?
« Last Edit: December 30, 2017, 03:22:24 pm by bradyhartsfield »

Handoko

  • Hero Member
  • *****
  • Posts: 1918
  • My goal: build my own game engine using Lazarus
Re: computer sience class in school
« Reply #37 on: December 30, 2017, 03:37:32 pm »
EDIT 2: thats the point where i need to use timer 2 as a kind of dummy right?

Don't do it.

Based on my experience, it is a bad thing to do. At the beginning it seems harmless. But when the code grow more complicated, handling multiple timers will make it harder to control and debug.

bradyhartsfield

  • New member
  • *
  • Posts: 41
Re: computer sience class in school
« Reply #38 on: December 30, 2017, 03:48:47 pm »
okay, thanks,  i will check for another way then..

regards
brady

molly

  • Hero Member
  • *****
  • Posts: 2214
Re: computer sience class in school
« Reply #39 on: December 30, 2017, 04:18:34 pm »
Congratulations bradyhartsfield as that cleaned up code is starting to look like something  :)

That could work as a solid start-base for further additions and/or improvements.

A minor improvement/correction...


if you would put:
Code: [Select]
    r[c] := 10;
before the lines:

Code: [Select]
    mx[c] := random(image1.width);
    my[c] := random(image1.height);

Then you could use r[c] to calculate the correct mx and my coordinates for each individual circle.

Because, as you might perhaps have noticed: some balls are "stuck" in the border and are getting a headache because they keep bouncing back 'n' forth at the same location  ;D
« Last Edit: December 30, 2017, 04:20:21 pm by molly »

Handoko

  • Hero Member
  • *****
  • Posts: 1918
  • My goal: build my own game engine using Lazarus
Re: computer sience class in school
« Reply #40 on: December 30, 2017, 04:22:10 pm »
@bradyhartsfield

For doing animations or games, you need to understand Game Loop. You can search the web for more information about Game Loop. Here I will only explain what you currently need to know about it.

No matter what kinds of game they are, they usually have this process flow:

Code: Pascal  [Select]
  1. ProcessInit;
  2.  
  3. // (start of) Game Loop
  4. while (GameIsRunning) do begin
  5.   ProcessUserInput;
  6.   ProcessCalculation;
  7.   ProcessDrawing;
  8. end;
  9. // (end of) Game Loop
  10.  
  11. ProcessCleaningUp;

The line #4 - #8 is the Game Loop. It will run repeatedly until users choose to quit or the player's health <= 0 or the player has finished the game.

In your case:
- You do not need to do ProcessUserInput
- You use a Timer to do the Game Loop

So you can imagine, if you use multiple timers then the Game Loop will be separated into several sub process and they're overlapped. Handling overlapped process is hard, should be avoided.

Alternatively, you should separate the ProcessCalculation into several sub sections:
- CalculateBouncing
- CalculateNewPosition

Currently, you already able to perform the CalculateNewPosition. For the CalculateBouncing, you just need to add some code: Pythagoras formula and set the velocity of the circle. So what you need to do is to separate the code in the Timer1Timer into several sections (CalculateBouncing and CalculateNewPosition) before the drawing process.
« Last Edit: December 30, 2017, 04:32:13 pm by Handoko »

bradyhartsfield

  • New member
  • *
  • Posts: 41
Re: computer sience class in school
« Reply #41 on: December 30, 2017, 05:03:47 pm »
Quote
Congratulations bradyhartsfield as that cleaned up code is starting to look like something  :)

That could work as a solid start-base for further additions and/or improvements.

A minor improvement/correction...


if you would put:
Code: [Select]

    r[c] := 10;


before the lines:

Code: [Select]

    mx[c] := random(image1.width);
    my[c] := random(image1.height);


Then you could use r[c] to calculate the correct mx and my coordinates for each individual circle.

Because, as you might perhaps have noticed: some balls are "stuck" in the border and are getting a headache because they keep bouncing back 'n' forth at the same location  ;D

Thanks!

Quote
So you can imagine, if you use multiple timers then the Game Loop will be separated into several sub process and they're overlapped. Handling overlapped process is hard, should be avoided.

Alternatively, you should separate the ProcessCalculation into several sub sections:
- CalculateBouncing
- CalculateNewPosition

Currently, you already able to perform the CalculateNewPosition. For the CalculateBouncing, you just need to add some code: Pythagoras formula and set the velocity of the circle. So what you need to do is to separate the code in the Timer1Timer into several sections (CalculateBouncing and CalculateNewPosition) before the drawing process.

Thanks, I will try that.

regards
brady

EDIT:
So you want me to restructure my whole code and add into the game loop or do you want me to add the game loop below my actual code and implement the pythagoras and needed lines into it below my "main" code?
« Last Edit: December 30, 2017, 05:17:45 pm by bradyhartsfield »

Handoko

  • Hero Member
  • *****
  • Posts: 1918
  • My goal: build my own game engine using Lazarus
Re: computer sience class in school
« Reply #42 on: December 30, 2017, 06:02:15 pm »
Actually your code already has the game loop. You use Timer as the game loop. What you need to do now is to add the Pythagoras calculation before the moving calculation.

bradyhartsfield

  • New member
  • *
  • Posts: 41
Re: computer sience class in school
« Reply #43 on: December 30, 2017, 06:29:45 pm »
hey,

thanks

regards
brady

bradyhartsfield

  • New member
  • *
  • Posts: 41
Re: computer sience class in school
« Reply #44 on: December 30, 2017, 08:28:14 pm »
hey,

how do i tell apart the different x coordinates of the circles? i have tried it this way
Code: Pascal  [Select]
  1.   for c:= 1 to 20 do begin
  2.  
  3.   if sqrt(sqr(mx[c])+sqr(my[c])) <= r[c]
  4.   then  begin
  5. Pyth1 :=vx[c];
  6. vx[c] := vx[c];
  7. vx[c]:= Pyth1;
  8.  
  9. Pyth2 := vy[c];
  10. vy[c] := vy[c];
  11. vy[c] :=Pyth2;
  12.   end;    

to just be honest: I dont know how to implement the pythagoras now.

regards
brady

 

Recent

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