Bookstore

Recent

Author Topic: [request] How can i improve this code?  (Read 653 times)

anbadaothcsts@gmail.com

  • New Member
  • *
  • Posts: 17
[request] How can i improve this code?
« on: February 26, 2020, 02:46:22 pm »
I'm working on some random code that will turn a key into a switch, it was something like this:
Code: Pascal  [Select]
  1. program Switch   ;
  2. uses crt         ;
  3. var key  :char   ;
  4.     dojob:boolean;
  5. begin
  6.     dojob:=false;
  7. repeat
  8.     key:=readkey;
  9.     if keypressed then
  10.         case dojob of
  11.             true : dojob:=false;
  12.             false: dojob:=true ;
  13.     end;
  14.    
  15.         case dojob of
  16.             true :  {do job};            
  17.             false:  {stop doing job};
  18.         end;
  19. until key = #27;
  20. end.
  21.  
Yes, i do aware that this code can't not compile, i just asking if anyone could improve it TvT

Handoko

  • Hero Member
  • *****
  • Posts: 3438
  • My goal: build my own game engine using Lazarus
Re: [request] How can i improve this code?
« Reply #1 on: February 26, 2020, 04:59:38 pm »
Not sure what you want, here I rewrote it using my writing style. It's been a very long time I didn't write console mode program. Here is my console mode animation:

Code: Pascal  [Select]
  1. program Animation;
  2. uses
  3.   Crt;
  4.  
  5. const
  6.   Running:   Boolean = True;
  7.   Animating: Boolean = True;
  8.   Key:       Char    = #0;
  9.   X:         Integer = 35;
  10.   PosX:      Integer = 40;
  11.   PosY:      Integer = 0;
  12.   Movement:  Integer = 1;
  13.  
  14. begin
  15.   cursoroff;
  16.   ClrScr;
  17.   repeat
  18.  
  19.     // Do falling ASCII
  20.     if Key > #0 then
  21.     begin
  22.       GotoXY(PosX, PosY);
  23.       Write(' ');
  24.       Inc(PosY);
  25.       if PosY > 20 then
  26.       begin
  27.         PosX := Random(80);
  28.         PosY := 0;
  29.         Key  := #0;
  30.       end
  31.       else
  32.       begin
  33.         GotoXY(PosX, PosY);
  34.         Write(Char(Key));
  35.       end
  36.     end;
  37.  
  38.     // Do horizontal animation
  39.     if Animating then
  40.     begin
  41.       GotoXY(X, 10);
  42.       Write(' o===o ');
  43.       Inc(X, Movement);
  44.       if (X < 10) or (X > 60) then
  45.         Movement := -Movement;
  46.     end;
  47.  
  48.     // Check keyboard input
  49.     if KeyPressed then
  50.     begin
  51.       Key := ReadKey;
  52.       case Key of
  53.         #27: Running   := False;
  54.         #32: Animating := not(Animating);
  55.       end;
  56.     end;
  57.  
  58.     Delay(100);
  59.   until not(Running);
  60.   ClrScr;
  61.   cursoron;
  62. end.

Line #54 is the switch to enable/disable horizontal animation.
« Last Edit: February 26, 2020, 05:51:49 pm by Handoko »

jamie

  • Hero Member
  • *****
  • Posts: 2490
Re: [request] How can i improve this code?
« Reply #2 on: February 26, 2020, 10:54:38 pm »
I believe the op was looking for a simple way to do Boolean toggles …


 This is how its done..

Code: Pascal  [Select]
  1. if KeyPressed Then DoJob := Not DoJob.
  2.  

That toggles
 :D
Number 1 at blue screen app creations!

lucamar

  • Hero Member
  • *****
  • Posts: 2588
Re: [request] How can i improve this code?
« Reply #3 on: February 26, 2020, 11:32:29 pm »
In your code (the OP's I mean) you're calling KeyPressed right after you ReadKey, which means that KeyPressed will most frequently be False unless you're ultra-quick typing or you press only extended keys.

If your intention is what I think it is, this code will work better:

Code: Pascal  [Select]
  1. program Switch;
  2. uses
  3.   crt;
  4. var
  5.   key: char;
  6.   dojob: boolean;
  7. begin
  8.   dojob:=false;
  9.   repeat
  10.     if KeyPressed then
  11.       dojob := not dojob;
  12.     key:=ReadKey;
  13.     if dojob and (key <> #27) then
  14.       {do job}
  15.     else
  16.       {stop doing job};
  17.   until key = #27;
  18. end.

That will start doing the job as soon as you press any key other than <Esc>, will switch between doing/stopping the job if you press any key other than <Esc> and will stop the job and exit if you press <Esc>.

Is that it?


Note: for brevity's sake I have skipped dealing with extended keys (those which ReadKey as #00). It should be easy to take into account, though.
« Last Edit: February 26, 2020, 11:35:48 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.6/FPC 3.0.4 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

anbadaothcsts@gmail.com

  • New Member
  • *
  • Posts: 17
Re: [request] How can i improve this code?
« Reply #4 on: February 29, 2020, 01:28:16 pm »
Not sure what you want, here I rewrote it using my writing style. It's been a very long time I didn't write console mode program. Here is my console mode animation:

Code: Pascal  [Select]
  1. program Animation;
  2. uses
  3.   Crt;
  4.  
  5. const
  6.   Running:   Boolean = True;
  7.   Animating: Boolean = True;
  8.   Key:       Char    = #0;
  9.   X:         Integer = 35;
  10.   PosX:      Integer = 40;
  11.   PosY:      Integer = 0;
  12.   Movement:  Integer = 1;
  13.  
  14. begin
  15.   cursoroff;
  16.   ClrScr;
  17.   repeat
  18.  
  19.     // Do falling ASCII
  20.     if Key > #0 then
  21.     begin
  22.       GotoXY(PosX, PosY);
  23.       Write(' ');
  24.       Inc(PosY);
  25.       if PosY > 20 then
  26.       begin
  27.         PosX := Random(80);
  28.         PosY := 0;
  29.         Key  := #0;
  30.       end
  31.       else
  32.       begin
  33.         GotoXY(PosX, PosY);
  34.         Write(Char(Key));
  35.       end
  36.     end;
  37.  
  38.     // Do horizontal animation
  39.     if Animating then
  40.     begin
  41.       GotoXY(X, 10);
  42.       Write(' o===o ');
  43.       Inc(X, Movement);
  44.       if (X < 10) or (X > 60) then
  45.         Movement := -Movement;
  46.     end;
  47.  
  48.     // Check keyboard input
  49.     if KeyPressed then
  50.     begin
  51.       Key := ReadKey;
  52.       case Key of
  53.         #27: Running   := False;
  54.         #32: Animating := not(Animating);
  55.       end;
  56.     end;
  57.  
  58.     Delay(100);
  59.   until not(Running);
  60.   ClrScr;
  61.   cursoron;
  62. end.

Line #54 is the switch to enable/disable horizontal animation.

Not what i wanted but tysm anyway <3

anbadaothcsts@gmail.com

  • New Member
  • *
  • Posts: 17
Re: [request] How can i improve this code?
« Reply #5 on: February 29, 2020, 01:29:31 pm »
In your code (the OP's I mean) you're calling KeyPressed right after you ReadKey, which means that KeyPressed will most frequently be False unless you're ultra-quick typing or you press only extended keys.

If your intention is what I think it is, this code will work better:

Code: Pascal  [Select]
  1. program Switch;
  2. uses
  3.   crt;
  4. var
  5.   key: char;
  6.   dojob: boolean;
  7. begin
  8.   dojob:=false;
  9.   repeat
  10.     if KeyPressed then
  11.       dojob := not dojob;
  12.     key:=ReadKey;
  13.     if dojob and (key <> #27) then
  14.       {do job}
  15.     else
  16.       {stop doing job};
  17.   until key = #27;
  18. end.

That will start doing the job as soon as you press any key other than <Esc>, will switch between doing/stopping the job if you press any key other than <Esc> and will stop the job and exit if you press <Esc>.

Is that it?


Note: for brevity's sake I have skipped dealing with extended keys (those which ReadKey as #00). It should be easy to take into account, though.

Just what i needed, tysm <3

anbadaothcsts@gmail.com

  • New Member
  • *
  • Posts: 17
Re: [request] How can i improve this code?
« Reply #6 on: February 29, 2020, 01:43:08 pm »
I believe the op was looking for a simple way to do Boolean toggles …
lmao true

lucamar

  • Hero Member
  • *****
  • Posts: 2588
Re: [request] How can i improve this code?
« Reply #7 on: February 29, 2020, 03:26:45 pm »
Just what i needed, tysm <3

Glad to be of help.

You've to remember to deal with extended keys, which should be as simple as doing:
Code: Pascal  [Select]
  1. key := ReadKey;
  2. if key = #00 then ReadKey;
if you want to treat them as "normal" keys and just ignore them.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.6/FPC 3.0.4 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

anbadaothcsts@gmail.com

  • New Member
  • *
  • Posts: 17
Re: [request] How can i improve this code?
« Reply #8 on: March 09, 2020, 03:10:20 pm »
just aside question: How many extended keys are there? And what's their input (like if you press the spacebar, it should gave the output #13)
Just what i needed, tysm <3

Glad to be of help.

You've to remember to deal with extended keys, which should be as simple as doing:
Code: Pascal  [Select]
  1. key := ReadKey;
  2. if key = #00 then ReadKey;
if you want to treat them as "normal" keys and just ignore them.

eljo

  • Full Member
  • ***
  • Posts: 181
Re: [request] How can i improve this code?
« Reply #9 on: March 09, 2020, 03:17:42 pm »
just aside question: How many extended keys are there? And what's their input (like if you press the spacebar, it should gave the output #13)
The first keyboards had only 88 keys the newer have at least 101 and some multimedia keyboards have more than that anything outside the 88 keys is an extended key. By the way the first 30 characters are control characters and space is not a control character, #13 is the character for line feed.


lucamar

  • Hero Member
  • *****
  • Posts: 2588
Re: [request] How can i improve this code?
« Reply #10 on: March 10, 2020, 12:17:12 pm »
just aside question: How many extended keys are there? And what's their input (like if you press the spacebar, it should gave the output #13)

I don't remember exactly but around 25 multiplied by the modifiers (Shift, Ctrl, etc.). The input they give (with ReadKey) is #00 followed by a second key press with their scancode. Any mid-level Turbo Pascal guide should tell you which they are. Let me see if I can unearth one from the bowels of some disk :)

Note that you cn investigate them yourself with a program such as:
Code: Pascal  [Select]
  1. uses crt;
  2. var
  3.   k: Char;
  4. begin
  5.   repeat
  6.     k := ReadKey;
  7.     if k <> #00 then begin
  8.       if k > #31 then
  9.         Writeln('Normal Key: #', Ord(k), '=',k)
  10.       else
  11.         Writeln('Control Key: #', Ord(k));
  12.     end else begin
  13.       k := ReadKey;
  14.       Writeln('Extended Key: #', Ord(k));
  15.     end;
  16.   until k = #27; {<Esc> exists}
  17. end.
« Last Edit: March 10, 2020, 12:32:39 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.6/FPC 3.0.4 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Thaddy

  • Hero Member
  • *****
  • Posts: 9809
Re: [request] How can i improve this code?
« Reply #11 on: March 10, 2020, 12:29:41 pm »
var key = #0;  :D
I am more like donkey than shrek