Bookstore

Recent

Author Topic: Idea?  (Read 660 times)

anbadaothcsts@gmail.com

  • New Member
  • *
  • Posts: 16
Idea?
« on: February 11, 2020, 10:35:20 am »
i'm new on pascal and have learnt it for almost 2 month. here's a code i made that will control a little circle around the console with both WASD and arrow key. If you have any idea for this, like a game pls tell me.
Code: Pascal  [Select]
  1. program move;
  2. uses crt;
  3. var x,y:integer;
  4.     key        :char;
  5.  
  6. begin
  7. clrscr;
  8. x:=1;
  9. y:=1;
  10. gotoxy(x,y);
  11. write('o');
  12. repeat
  13.     repeat
  14.     until keypressed; {scan keyboard}
  15. key:=readkey;
  16.     if key = #0 then key:=readkey;
  17.     case key of
  18.         #72,'W','w': y:=y-1;
  19.         #75,'A','a': x:=x-2;
  20.         #80,'S','s': y:=y+1;
  21.         #77,'D','d': x:=x+2;
  22.     end; {end case key}
  23.     if x = -1 then x:=x+2;
  24.     if y = 0  then y:=y+1;
  25. clrscr;
  26. gotoxy(x,y);
  27. write('o');
  28. gotoxy(x,y);
  29. until key = #27;
  30. end.
  31.  

Handoko

  • Hero Member
  • *****
  • Posts: 3434
  • My goal: build my own game engine using Lazarus
Re: Idea?
« Reply #1 on: February 11, 2020, 12:07:03 pm »
Hello anbadaothcsts,
Welcome to the forum.

Your WASD console program reminded me an old text-mode arcade game I ever played "Kingdom Of Kroz".

It may look outdated in modern technology era but Kroz is still fun. You can create one, it isn't too hard and the original version was built using Turbo Pascal.

https://en.wikipedia.org/wiki/Kroz

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8022
Re: Idea?
« Reply #2 on: February 11, 2020, 12:08:38 pm »
Or Moria/Angband and friends.  https://en.wikipedia.org/wiki/Angband_(video_game)

The commercial Diablo games series has its origins in this genre.

lucamar

  • Hero Member
  • *****
  • Posts: 2560
Re: Idea?
« Reply #3 on: February 11, 2020, 12:17:31 pm »
Some comments, with your kind permission:

1 - Since ReadKey already waits for a key to be pressed, your empty loop checking for KeyPressed is superfluous.

2 - This code:
Code: Pascal  [Select]
  1.         if x = -1 then x:=x+2;
  2.         if y = 0  then y:=y+1;
is equivalent to this:
Code: Pascal  [Select]
  1.         if x = -1 then x:=1;
  2.         if y = 0  then y:=1;
which is somewhat faster (no operations involved) and makes clearer what it really means.

3 - Your code unneccessarily clears the screen and writes the "o"  in the same place it was when the key isn't one of those you're looking for.

4 - Since you position the cursor back under the recently written character, you may just write a space over it to delete it rather than incurring the cost of clearing the whole screen.

Taking all into account, this might be a better (though not yet "perfect") way to make it:

Code: Pascal  [Select]
  1. program move;
  2. uses crt;
  3. var
  4.     x,y: integer;
  5.     key: char;
  6.     doDraw: Boolean;
  7.  
  8. procedure WriteO;
  9. begin
  10.     if x = -1 then x:=1;
  11.     if y = 0  then y:=1;
  12.     write(' ');
  13.     gotoxy(x,y);
  14.     write('o');
  15.     gotoxy(x,y);
  16. end;
  17.      
  18. begin
  19.     x:=1;
  20.     y:=1;
  21.     ClrScr;
  22.     WriteO;
  23.     repeat
  24.         key:=readkey;
  25.         if key = #0 then key:=readkey;
  26.         doDraw := true;
  27.         case key of
  28.             #72,'W','w': y:=y-1;
  29.             #75,'A','a': x:=x-2;
  30.             #80,'S','s': y:=y+1;
  31.             #77,'D','d': x:=x+2;
  32.             else doDraw := False;
  33.         end; {end case key}
  34.         if doDraw then WriteO;
  35.     until key = #27;
  36. end.
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: 16
Re: Idea?
« Reply #4 on: February 18, 2020, 04:24:12 pm »
i really appreciate your support but can someone deeply explain to me how does the command *procedure* worked? thanks

anbadaothcsts@gmail.com

  • New Member
  • *
  • Posts: 16
Re: Idea?
« Reply #5 on: February 18, 2020, 04:42:31 pm »
Some comments, with your kind permission:

1 - Since ReadKey already waits for a key to be pressed, your empty loop checking for KeyPressed is superfluous.

2 - This code:
Code: Pascal  [Select]
  1.         if x = -1 then x:=x+2;
  2.         if y = 0  then y:=y+1;
is equivalent to this:
Code: Pascal  [Select]
  1.         if x = -1 then x:=1;
  2.         if y = 0  then y:=1;
which is somewhat faster (no operations involved) and makes clearer what it really means.

3 - Your code unneccessarily clears the screen and writes the "o"  in the same place it was when the key isn't one of those you're looking for.

4 - Since you position the cursor back under the recently written character, you may just write a space over it to delete it rather than incurring the cost of clearing the whole screen.

Taking all into account, this might be a better (though not yet "perfect") way to make it:

Code: Pascal  [Select]
  1. program move;
  2. uses crt;
  3. var
  4.     x,y: integer;
  5.     key: char;
  6.     doDraw: Boolean;
  7.  
  8. procedure WriteO;
  9. begin
  10.     if x = -1 then x:=1;
  11.     if y = 0  then y:=1;
  12.     write(' ');
  13.     gotoxy(x,y);
  14.     write('o');
  15.     gotoxy(x,y);
  16. end;
  17.      
  18. begin
  19.     x:=1;
  20.     y:=1;
  21.     ClrScr;
  22.     WriteO;
  23.     repeat
  24.         key:=readkey;
  25.         if key = #0 then key:=readkey;
  26.         doDraw := true;
  27.         case key of
  28.             #72,'W','w': y:=y-1;
  29.             #75,'A','a': x:=x-2;
  30.             #80,'S','s': y:=y+1;
  31.             #77,'D','d': x:=x+2;
  32.             else doDraw := False;
  33.         end; {end case key}
  34.         if doDraw then WriteO;
  35.     until key = #27;
  36. end.

tysm!!!

i also have this code and really need you guys help
Code: Pascal  [Select]
  1. program move;
  2. uses crt;
  3. var x,y:integer;
  4.       key:char;
  5. begin
  6. x:=1;
  7. y:=1;
  8.       procedure WriteO;
  9. begin
  10.     if x = -1 then x:=1;
  11.     if y = 0  then y:=1;
  12.     write(' ');
  13.     gotoxy(x,y);
  14.     write('o');
  15.     gotoxy(x,y);
  16. end;
  17.      repeat
  18. key:=readkey;
  19. case key of
  20.             #72,'W','w':
  21. begin
  22. repeat
  23. delay(100);
  24. y:=y-1;
  25. WriteO;
  26. until keypressed;
  27. end;
  28.             #75,'A','a':
  29. begin
  30. repeat
  31. delay(100);
  32. x:=x-1;
  33. WriteO;
  34. until keypressed;
  35. end;
  36.             #80,'S','s':
  37. begin
  38. repeat
  39. delay(100);
  40. y:=y+1;
  41. WriteO;
  42. until keypressed;
  43. end;
  44.             #77,'D','d':begin
  45. repeat
  46. delay(100);
  47. x:=x+1;
  48. WriteO;
  49. until keypressed;
  50. end;
  51.       end;
  52. until key = #27;
  53. end.
  54.  

to prevent the character's cordinate hit 0, i gave each case the
Code: Pascal  [Select]
  1.  if x = 0 then x:=1;
or
Code: Pascal  [Select]
  1.  if y = 0 then y:=1
the begin-end; in each case keep the character keep going (like pacman) until there is a key pressed. to keep the character's move flowing i must use delay(time), is there any other way to do it?

it's okay to use delay but i don't want the player to spam keys and make the game delayed

Thaddy

  • Hero Member
  • *****
  • Posts: 9782
Re: Idea?
« Reply #6 on: February 18, 2020, 06:08:08 pm »
Code: Pascal  [Select]
  1. x:= max(x,1);
  2. y:= max(y,1);
I am more like donkey than shrek

lucamar

  • Hero Member
  • *****
  • Posts: 2560
Re: Idea?
« Reply #7 on: February 18, 2020, 10:01:28 pm »
i also have this code and really need you guys help [...]

Excuse me but, what a mess! It's quite obvious that you are a beginner :)

Quote
it's okay to use delay but i don't want the player to spam keys and make the game delayed

It's OK to use delay if you use it judiciously. Try this simple code to see how it might be done:

Code: Pascal  [Select]
  1. program MoveAround;
  2.  
  3. uses crt;
  4.  
  5. type
  6.   TSpriteDirection = (sdUp, sdDown, sdLeft, sdRight, sdNone);
  7.  
  8. var
  9.   x, y, MaxX, MaxY: integer;
  10.   key: char;
  11.   Direction: TSpriteDirection;
  12.  
  13.  
  14. procedure CheckCoords;
  15. begin
  16.   { if x is out of limits, set it to inside and stop }
  17.   if not x in [1..MaxX] then begin
  18.     if x < 1 then x := 1
  19.     else if x > MaxX then x := MaxX;
  20.     if Direction in [sdLeft, sdRight] then
  21.       Direction := sdNone;
  22.   end;
  23.   { if y is out of limits, set it to inside and stop }
  24.   if not y in [1..MaxY] then begin
  25.     if y <= 0 then y := 1
  26.     else
  27.       if y > MaxY then y := MaxY;
  28.     if Direction in [sdUp, sdDown] then
  29.       Direction := sdNone;
  30.   end;
  31. end;
  32.  
  33. procedure WriteO;
  34. begin
  35.   CheckCoords;
  36.   write(' ');
  37.   gotoxy(x,y);
  38.   write('o');
  39.   gotoxy(x,y);
  40. end;
  41.  
  42. procedure UpdatePosition;
  43. begin
  44.   case Direction of
  45.   sdUp: Dec(y);
  46.   sdDown: Inc(y);
  47.   sdLeft: Dec(x);
  48.   sdRight: Inc(x);
  49.   end;
  50. end;
  51.  
  52.  
  53. procedure SetDirection(NewDirection: TSpriteDirection);
  54. begin
  55.   if Direction <> NewDirection then
  56.     Direction := NewDirection;
  57. end;
  58.  
  59. begin
  60.   ClrScr;
  61.   x:=1;
  62.   y:=1;
  63.   MaxX := WindMaxX-1; {Minus 1 to prevent scrolling}
  64.   MaxY := WindMaxY-1; {Minus 1 to prevent scrolling}
  65.   WriteO;
  66.   Direction := sdNone;
  67.   { Loop until the user presses <Esc> }
  68.   repeat
  69.     key:=readkey;
  70.     case key of
  71.     #72,'W','w': SetDirection(sdUp);
  72.     #75,'A','a': SetDirection(sdLeft);
  73.     #80,'S','s': SetDirection(sdDown);
  74.     #77,'D','d': SetDirection(sdRight);
  75.     else Direction := sdNone;
  76.     end;
  77.     { Update position and redraw}
  78.     while (Direction <> sdNone) and not KeyPressed do begin
  79.       UpdatePosition;
  80.       WriteO;
  81.       Delay(100);
  82.     end;
  83.   until key = #27;
  84. end.

Note that there are a couple bugs in it (at least in the Linux console) that I haven't got time now to haunt and fix. I'll leave them to you as an exercise  ;D

HTH!
« Last Edit: February 18, 2020, 10:03:00 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: 16
Re: Idea?
« Reply #8 on: February 26, 2020, 02:12:23 pm »
i also have this code and really need you guys help [...]

Excuse me but, what a mess! It's quite obvious that you are a beginner :)

Quote
it's okay to use delay but i don't want the player to spam keys and make the game delayed

It's OK to use delay if you use it judiciously. Try this simple code to see how it might be done:

Code: Pascal  [Select]
  1. program MoveAround;
  2.  
  3. uses crt;
  4.  
  5. type
  6.   TSpriteDirection = (sdUp, sdDown, sdLeft, sdRight, sdNone);
  7.  
  8. var
  9.   x, y, MaxX, MaxY: integer;
  10.   key: char;
  11.   Direction: TSpriteDirection;
  12.  
  13.  
  14. procedure CheckCoords;
  15. begin
  16.   { if x is out of limits, set it to inside and stop }
  17.   if not x in [1..MaxX] then begin
  18.     if x < 1 then x := 1
  19.     else if x > MaxX then x := MaxX;
  20.     if Direction in [sdLeft, sdRight] then
  21.       Direction := sdNone;
  22.   end;
  23.   { if y is out of limits, set it to inside and stop }
  24.   if not y in [1..MaxY] then begin
  25.     if y <= 0 then y := 1
  26.     else
  27.       if y > MaxY then y := MaxY;
  28.     if Direction in [sdUp, sdDown] then
  29.       Direction := sdNone;
  30.   end;
  31. end;
  32.  
  33. procedure WriteO;
  34. begin
  35.   CheckCoords;
  36.   write(' ');
  37.   gotoxy(x,y);
  38.   write('o');
  39.   gotoxy(x,y);
  40. end;
  41.  
  42. procedure UpdatePosition;
  43. begin
  44.   case Direction of
  45.   sdUp: Dec(y);
  46.   sdDown: Inc(y);
  47.   sdLeft: Dec(x);
  48.   sdRight: Inc(x);
  49.   end;
  50. end;
  51.  
  52.  
  53. procedure SetDirection(NewDirection: TSpriteDirection);
  54. begin
  55.   if Direction <> NewDirection then
  56.     Direction := NewDirection;
  57. end;
  58.  
  59. begin
  60.   ClrScr;
  61.   x:=1;
  62.   y:=1;
  63.   MaxX := WindMaxX-1; {Minus 1 to prevent scrolling}
  64.   MaxY := WindMaxY-1; {Minus 1 to prevent scrolling}
  65.   WriteO;
  66.   Direction := sdNone;
  67.   { Loop until the user presses <Esc> }
  68.   repeat
  69.     key:=readkey;
  70.     case key of
  71.     #72,'W','w': SetDirection(sdUp);
  72.     #75,'A','a': SetDirection(sdLeft);
  73.     #80,'S','s': SetDirection(sdDown);
  74.     #77,'D','d': SetDirection(sdRight);
  75.     else Direction := sdNone;
  76.     end;
  77.     { Update position and redraw}
  78.     while (Direction <> sdNone) and not KeyPressed do begin
  79.       UpdatePosition;
  80.       WriteO;
  81.       Delay(100);
  82.     end;
  83.   until key = #27;
  84. end.

Note that there are a couple bugs in it (at least in the Linux console) that I haven't got time now to haunt and fix. I'll leave them to you as an exercise  ;D

HTH!

heyyy tysm, i do find the bugs  :)

lucamar

  • Hero Member
  • *****
  • Posts: 2560
Re: Idea?
« Reply #9 on: February 26, 2020, 05:24:24 pm »
heyyy tysm, i do find the bugs  :)

Would be nice if you told us what you found, if just for the ones that may be looking for the same help later ;D
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.