Recent

Author Topic: How to let user Input something while keep runing the Program in Free Pascal  (Read 3260 times)

Berklesse

  • New Member
  • *
  • Posts: 16
Hi all , so i  wonder how can i let the user Input something *by example w,a,s,d..* while the Program is runing ,
To understand more here is an Example ; A Snake game where the Snake keeps moving toward a  direction until
the user deside to change it ..
Hope you understand me , and hope you can help me .
THANK YOU SO MUCH ! :)

Handoko

  • Hero Member
  • *****
  • Posts: 3773
  • My goal: build my own game engine using Lazarus
Hello Berklesse,
Welcome to this forum.

To read the user input you can use ReadKey, but to do it while the program running (without pause) you need to check the input buffer using KeyPressed. Read more here:
http://www.freepascal.org/docs-html/rtl/crt/readkey.html
http://www.freepascal.org/docs-html/rtl/crt/keypressed.html

The very basic of game programming is the game main loop. Once you already understand game looping, you will be easier to learn other tricks. To learn about game looping read here:
http://gamedev.stackexchange.com/questions/651/tips-for-writing-the-main-game-loop
http://gameprogrammingpatterns.com/game-loop.html

I saw this topic's title mention "Free Pascal". Yep, for game programming it is usually better to use Free Pascal rather than Lazarus. But because I use Lazarus not Free Pascal, here I write a short demo about detecting user keyboard input using Lazarus.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, Forms, ExtCtrls, LCLType, Controls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Shape1: TShape;
  16.     Timer1: TTimer;
  17.     procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  18.     procedure Timer1Timer(Sender: TObject);
  19.   end;
  20.  
  21.   TDirection = (None, MoveUp, MoveDown, MoveLeft, MoveRight);
  22.  
  23. var
  24.   Form1: TForm1;
  25.   Direction: TDirection = None;
  26.  
  27. implementation
  28.  
  29. {$R *.lfm}
  30.  
  31. { TForm1 }
  32.  
  33. procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState
  34.   );
  35. begin
  36.   case Key of
  37.     VK_UP:    Direction := MoveUp;
  38.     VK_DOWN:  Direction := MoveDown;
  39.     VK_LEFT:  Direction := MoveLeft;
  40.     VK_RIGHT: Direction := MoveRight;
  41.     VK_ESCAPE: Halt; // Press Esc to quit
  42.   end;
  43. end;
  44.  
  45. procedure TForm1.Timer1Timer(Sender: TObject);
  46. begin
  47.    case Direction of
  48.      MoveUp:
  49.        begin
  50.          Shape1.Top := Shape1.Top - 3;
  51.          if Shape1.Top < 0 then Shape1.Top := 0;
  52.        end;
  53.      MoveDown:
  54.        begin
  55.          Shape1.Top := Shape1.Top + 3;
  56.          if Shape1.Top > (Form1.Height-Shape1.Height) then Shape1.Top := Form1.Height-Shape1.Height;
  57.        end;
  58.      MoveLeft:
  59.        begin
  60.          Shape1.Left := Shape1.Left - 3;
  61.          if Shape1.Left < 0 then Shape1.Left := 0;
  62.        end;
  63.      MoveRight:
  64.        begin
  65.          Shape1.Left := Shape1.Left + 3;
  66.          if Shape1.Left > (Form1.Width-Shape1.Width) then Shape1.Left := Form1.Width-Shape1.Width;
  67.        end;
  68.    end;
  69. end;
  70.  
  71. end.

You can download the attached file to test it using Lazarus.

Note:
- The code doesn't optimize for graphics performance.
- You need to use LCLType unit to be able to use key names (VK_UP, VK_DOWN, etc).
- The code doesn't use proper game loop, alternatively it use TTimer.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8267
  • Programming + Glam Metal + Tae Kwon Do = Me
Hi all , so i  wonder how can i let the user Input something *by example w,a,s,d..* while the Program is runing ,
To understand more here is an Example ; A Snake game where the Snake keeps moving toward a  direction until
the user deside to change it ..
Hope you understand me , and hope you can help me .
THANK YOU SO MUCH ! :)
A base to get you started (use arrow instead of wsad, though):
Code: Pascal  [Select][+][-]
  1. uses
  2.   Crt;
  3.  
  4. type
  5.   TDirection = (dirUp,dirLeft,dirRight,dirDown);
  6. var
  7.   Ch: Char;
  8.   Dir: TDirection;
  9.   X,Y: Word;
  10. begin
  11.   CursorOff;
  12.   Dir := dirDown;
  13.   X   := 40;
  14.   Y   := 1;
  15.   repeat
  16.     if KeyPressed then begin
  17.       Ch := ReadKey;
  18.       if Ch = #0 then Ch := ReadKey;
  19.       case Ch of
  20.         #72: Dir := dirUp;
  21.         #75: Dir := dirLeft;
  22.         #77: Dir := dirRight;
  23.         #80: Dir := dirDown;
  24.       end;
  25.     end;
  26.     Delay(250);
  27.     case Dir of
  28.       dirUp    : if Y > 1 then Dec(Y);
  29.       dirDown  : if Y < 25 then Inc(Y);
  30.       dirLeft  : if X > 1 then Dec(X);
  31.       dirRight : if X < 80 then Inc(X);
  32.     end;
  33.     ClrScr;
  34.     GoToXY(X,Y);
  35.     Write('*');
  36.   until UpCase(Ch) = 'Q';
  37.   CursorOn;
  38. end.
  39.  
I don't really recommend Crt unit but it's indeed a lot simpler than using Video and Keyboard unit.

Berklesse

  • New Member
  • *
  • Posts: 16
THANK YOU GUYS EVER SO MUCH YOU HELPED ME ALOT :')  :D :D

 

TinyPortal © 2005-2018