Lazarus

Miscellaneous => Other => Topic started by: Tony Stone on October 19, 2021, 02:42:09 am

Title: Showing off what my 7 year old did with Lazarus!
Post by: Tony Stone on October 19, 2021, 02:42:09 am
I know... this is silly and pointless for most here.  BUT, i feel the need to share it anyway!  She is proud of it, understood and figured out some of the code... and she is loving this stuff so far!  Lazarus is going to be part of our at home school routine.
 :D

She wants to make it do colors next.



(Mods, no hard feelings if you remove this post as not being useful to community.  I get it)
Title: Re: Showing off what my 7 year old did with Lazarus!
Post by: trev on October 19, 2021, 02:44:56 am
Great! Reminds me of the Etch-A-Sketch I had when a kid many moons ago.
Title: Re: Showing off what my 7 year old did with Lazarus!
Post by: 440bx on October 19, 2021, 02:57:31 am
She is proud of it
And I hope you're proud of her.  I'd say that's impressive considering it's from a 7 year old.
Title: Re: Showing off what my 7 year old did with Lazarus!
Post by: VTwin on October 19, 2021, 03:21:45 am
Nice!
Title: Re: Showing off what my 7 year old did with Lazarus!
Post by: Tony Stone on October 19, 2021, 03:28:39 am
She is proud of it
And I hope you're proud of her.  I'd say that's impressive considering it's from a 7 year old.

Yes, I AM VERY proud of her.  Her excitement too was awesome!  Some day she may be an amazing programmers who got her start with Lazarus!  To be clear, she didn't write the code all on her own.  We both read about functions, learned and tried together.  Its a very fun project so far.  I am hoping my younger daughter will get excited about this stuff as well. 

Her very first program I can confidently say beats any of the Hello World first programs!   :D

And thanks to all of the Lazarus/FPC developers for your hard work on this project! 
Title: Re: Showing off what my 7 year old did with Lazarus!
Post by: engkin on October 19, 2021, 08:05:17 am
i feel the need to share it anyway!

I am glad you did. She is obviously smart.

Great! Reminds me of the Etch-A-Sketch I had when a kid many moons ago.

+1
Title: Re: Showing off what my 7 year old did with Lazarus!
Post by: Thaddy on October 19, 2021, 09:20:20 am
(Mods, no hard feelings if you remove this post as not being useful to community.  I get it)
The mods will not even consider removing it.... :) ;D :-*

Reminds me the other way around: my mother, at age 72 then wrote her own contacts/address book in-Delphi - Object Pascal. (sadly she recently died at age 90).

That is just to show age does not matter. What does matter is the programming language.

Top Job!!!
Title: Re: Showing off what my 7 year old did with Lazarus!
Post by: MarkMLl on October 19, 2021, 09:34:43 am
Well done that young lady! (And well done parents as well.)

MarkMLl
Title: Re: Showing off what my 7 year old did with Lazarus!
Post by: Bart on October 19, 2021, 04:03:08 pm
Move to "Third party"  O:-)

Bart
Title: Re: Showing off what my 7 year old did with Lazarus!
Post by: Thaddy on October 19, 2021, 04:11:18 pm
Move to "Third party"  O:-)

Bart
NO!
Title: Re: Showing off what my 7 year old did with Lazarus!
Post by: Zvoni on October 19, 2021, 04:12:25 pm
Deserves an entry in https://wiki.freepascal.org/Projects_using_Lazarus_-_Educational_software
 O:-) O:-) O:-)
Title: Re: Showing off what my 7 year old did with Lazarus!
Post by: Bart on October 19, 2021, 04:20:05 pm
NO!

And there's the grumpy old man again...

Bart
Title: Re: Showing off what my 7 year old did with Lazarus!
Post by: Tony Stone on October 19, 2021, 04:29:12 pm
So tonight we are gonna work on making the arrow keys function a little more proper.  We were able to use the keypress event of the form to simple adjust the spinedits... which the spinedits onchange currently calls the drawing points.  But she wants it to be able to accept pressing 2 arrow keys at once to draw a diagonal line for example.  So I am currently looking for how to go about that.  So I guess that will be tonights lesson for us both.   :D  I have a feeling though that processing 2 keys at once is gonna add a lot of complexity for her to understand.  For me too!  lol

If it is too complicated we will work on selecting colors with a color picker component.  That should be simple enough to keep her interested and learning.   :D
Title: Re: Showing off what my 7 year old did with Lazarus!
Post by: Tony Stone on October 19, 2021, 05:37:51 pm
So tonight we are gonna work on making the arrow keys function a little more proper.  We were able to use the keypress event of the form to simple adjust the spinedits... which the spinedits onchange currently calls the drawing points.  But she wants it to be able to accept pressing 2 arrow keys at once to draw a diagonal line for example.  So I am currently looking for how to go about that.  So I guess that will be tonights lesson for us both.   :D  I have a feeling though that processing 2 keys at once is gonna add a lot of complexity for her to understand.  For me too!  lol

If it is too complicated we will work on selecting colors with a color picker component.  That should be simple enough to keep her interested and learning.   :D

So i did a little reading... and i think i need to track the key down and key up events.  and I am thinking of using a timer that basically moves the drawing point.  I am thinking this may be the simplest way to go.  hmmmm.... then the timer interval can be set to adjust drawing speed i suppose.  Unless I am missing an more obvious simple solution please let me know.  Gotta keep it simple for a child.  And simple for my adult-child brain.   ;)
Title: Re: Showing off what my 7 year old did with Lazarus!
Post by: howardpc on October 19, 2021, 06:53:19 pm
I think the simplest solution would be to use only two arrow keys (say Left and Right) in combination with two modifier keys (say Shift for Up and Ctrl for Down).
This means you can read all the information from a single KeyDown call.
However, if you want to try to intercept successive keystrokes, the following unit (to my surprise) works most of the time. Its accuracy does depend on the keyboard, and the typist's skill, and not having a fast keyboard repeat rate.
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, Forms, LazLogger;
  9.  
  10. const
  11.  
  12.   ArrowKeys: set of Byte = [37, 38, 39, 40];
  13.   Diagonals: set of Byte = [0, 74, 76, 154];
  14.  
  15. type
  16.  
  17.   TDiagonal = (dNone, dUpLeft, dUpRight, dDownLeft, dDownRight);
  18.  
  19.   TForm1 = class(TForm)
  20.   private
  21.     arr: array[1..2] of Byte;
  22.     even: Boolean;
  23.     function AddedDiagonalKey(aKey: Word; out diagonal: TDiagonal): Boolean;
  24.   protected
  25.     procedure KeyDown(var Key: Word; Shift: TShiftState); override;
  26.   end;
  27.  
  28.   function DiagonalToStr(aDiagonal: TDiagonal): String;
  29.  
  30. var
  31.   Form1: TForm1;
  32.  
  33. implementation
  34.  
  35. function DiagonalToStr(aDiagonal: TDiagonal): String;
  36. begin
  37.   WriteStr(Result, aDiagonal);
  38.   Delete(Result, 1, 1);
  39. end;
  40.  
  41. {$R *.lfm}
  42.  
  43. { TForm1 }
  44.  
  45. function TForm1.AddedDiagonalKey(aKey: Word; out diagonal: TDiagonal): Boolean;
  46. var
  47.   tmp: Int64;
  48. begin
  49.   if aKey > High(Byte) then
  50.     begin
  51.       Diagonal := dNone;
  52.       Exit(False);
  53.     end;
  54.   if even then arr[2] := Byte(aKey)
  55.   else arr[1] := Byte(aKey);
  56.   even := not even;
  57.   tmp := arr[1] * arr[2];
  58.   Dec(tmp, 1406);
  59.   Result := (arr[1] <> arr[2]) and (arr[1] in ArrowKeys) and (arr[2] in ArrowKeys) and
  60.             (Byte(tmp) in Diagonals);
  61.   case Result of
  62.     True: case tmp of
  63.             0:   diagonal := dUpLeft;
  64.             74:  diagonal := dDownLeft;
  65.             76:  diagonal := dUpRight;
  66.             154: diagonal := dDownRight;
  67.           end;
  68.     False: diagonal := dNone;
  69.   end;
  70. end;
  71.  
  72. procedure TForm1.KeyDown(var Key: Word; Shift: TShiftState);
  73. var
  74.   diagonal: TDiagonal;
  75. begin
  76.   inherited KeyDown(Key, Shift);
  77.   case AddedDiagonalKey(Key, diagonal) of
  78.     True:  DebugLn(['diagonal key combination ',DiagonalToStr(diagonal)]);
  79.     False: DebugLn('non-diagonal key combination');
  80.   end;
  81. end;
  82.  
  83. end.

Title: Re: Showing off what my 7 year old did with Lazarus!
Post by: engkin on October 19, 2021, 08:08:00 pm
Binary might be easy here. Each digit for one arrow key. Here is how to track the keys:
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  16.     procedure FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
  17.   private
  18.   public
  19.     TrackDownKeys:Byte;
  20.     procedure LetUsMove;
  21.   end;
  22.  
  23. var
  24.   Form1: TForm1;
  25.  
  26. implementation
  27. uses
  28.   LCLType;
  29.  
  30. {$R *.lfm}
  31.  
  32. { TForm1 }
  33.  
  34. const
  35.   CFourArrowKeys:array[VK_LEFT..VK_DOWN] of byte = (%0001 {Left}, %0010 {Up}, %0100 {Right}, %1000 {Down});
  36.  
  37. procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState
  38.   );
  39. begin
  40.   if Key in [VK_LEFT..VK_DOWN] then
  41.     TrackDownKeys:=TrackDownKeys or CFourArrowKeys[Key];
  42.  
  43.   LetUsMove;
  44. end;
  45.  
  46. procedure TForm1.FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
  47. begin
  48.   if Key in [VK_LEFT..VK_DOWN] then
  49.     TrackDownKeys:=TrackDownKeys and not CFourArrowKeys[Key];
  50.  
  51.   LetUsMove;
  52. end;
  53.  
  54. procedure TForm1.LetUsMove;
  55. begin
  56.   Caption:=TrackDownKeys.ToBinString;
  57. end;
  58.  
  59. end.

In this case Up and Left is %0011. As you can see it is visible.
Up and Right is %0110  and so on

Choosing which of the eight directions could be like:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.LetUsMove;
  2. begin
  3.   Caption:=TrackDownKeys.ToBinString;
  4.   case TrackDownKeys of
  5.   %0001:;//Left
  6.   %0010:;//Up
  7.   %0100:;//Right
  8.   %1000:;//Down
  9.   %0011:;//Left and Up
  10.   %0110:;//Right and Up
  11.   %1100:;//Right and down
  12.   %1001:;//Left and Down
  13.   end;
  14. end;

If you know the direction you can turn it into delta for x and y. Maybe like:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.LetUsMove;
  2. var
  3.   dx,dy:integer;
  4. begin
  5.   Caption:=TrackDownKeys.ToBinString;
  6.   case TrackDownKeys of
  7.   %0001:begin dx:=-1; dy:= 0; end;//Left
  8.   %0010:begin dx:= 0; dy:=-1; end;//Up
  9.   %0100:begin dx:=+1; dy:= 0; end;//Right
  10.   %1000:begin dx:= 0; dy:=+1; end;//Down
  11.   %0011:begin dx:=-1; dy:=-1; end;//Left and Up
  12.   %0110:begin dx:=+1; dy:=-1; end;//Right and Up
  13.   %1100:begin dx:=+1; dy:=+1; end;//Right and down
  14.   %1001:begin dx:=-1; dy:=+1; end;//Left and Down
  15.   end;
  16. end;
Title: Re: Showing off what my 7 year old did with Lazarus!
Post by: Tony Stone on October 20, 2021, 02:34:11 am
Thank you guys for the suggestions on the diagonal drawing aspect.  For our purposes and skill level it was a bit much.  But her and I tonight were able to complete the project!  And as I said before, this is almost all her design!!  Her ideas... I did the coding but walked her through what it was all doing and I did it based on logic she suggested!  It was a ton of fun to do this with her.  I am attaching the project.  It actually works very well!  Maybe it can even be useful to another beginner!  :D 8)

Feel free to comment on the code and design.  We are tough, we can take some criticism!   ;)

Edit:  just realized you may not find form1 right away as it will be off screen likely.  Use the Window menu then Center a Lost  Window if you need to get it on the screen.
TinyPortal © 2005-2018