### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: Showing off what my 7 year old did with Lazarus!  (Read 3530 times)

#### engkin

• Hero Member
• Posts: 2977
##### Re: Showing off what my 7 year old did with Lazarus!
« Reply #15 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;
« Last Edit: October 19, 2021, 08:18:22 pm by engkin »

#### Tony Stone

• Full Member
• Posts: 115
##### Re: Showing off what my 7 year old did with Lazarus!
« Reply #16 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!

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.
« Last Edit: October 20, 2021, 03:01:18 am by Tony Stone »