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.htmlhttp://www.freepascal.org/docs-html/rtl/crt/keypressed.htmlThe 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-loophttp://gameprogrammingpatterns.com/game-loop.htmlI 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.
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, Forms, ExtCtrls, LCLType, Controls;
type
{ TForm1 }
TForm1 = class(TForm)
Shape1: TShape;
Timer1: TTimer;
procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure Timer1Timer(Sender: TObject);
end;
TDirection = (None, MoveUp, MoveDown, MoveLeft, MoveRight);
var
Form1: TForm1;
Direction: TDirection = None;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState
);
begin
case Key of
VK_UP: Direction := MoveUp;
VK_DOWN: Direction := MoveDown;
VK_LEFT: Direction := MoveLeft;
VK_RIGHT: Direction := MoveRight;
VK_ESCAPE: Halt; // Press Esc to quit
end;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
case Direction of
MoveUp:
begin
Shape1.Top := Shape1.Top - 3;
if Shape1.Top < 0 then Shape1.Top := 0;
end;
MoveDown:
begin
Shape1.Top := Shape1.Top + 3;
if Shape1.Top > (Form1.Height-Shape1.Height) then Shape1.Top := Form1.Height-Shape1.Height;
end;
MoveLeft:
begin
Shape1.Left := Shape1.Left - 3;
if Shape1.Left < 0 then Shape1.Left := 0;
end;
MoveRight:
begin
Shape1.Left := Shape1.Left + 3;
if Shape1.Left > (Form1.Width-Shape1.Width) then Shape1.Left := Form1.Width-Shape1.Width;
end;
end;
end;
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.