Key movements in turbo pascal

hi guys. i'm new to the forum. need some help with character movement in my game. now i'm quite experienced and been programming with turbo pascal since 2004. currently making a rpg game engine. been working on it for 3 months now (not my first attempt but my 4th). almost done. currently programming the battle system. however, the character movement has been bothering me ever since i started programming games. i can't get my character to move fluidly on screen. for example, pressing and holding the right arrow key makes the character move right once, then there is a slight pause and then the character keeps moving until i stop pressing the right arrow key. i would like to eliminate that pause/delay in character movement. i do understand that this happens because the keyboard buffer is not cleared/flushed. i am using booleans to remember which arrow keys are being pressed and setting the boolean to true when it is pressed. what i need to be able to do is to clear the keyboard buffer then scan for any key pressed with 'keypressed' then read the key that's pressed and set the corresponding boolean value to true or false, which then determines the horizontal or diagonal movements. if no key has been pressed i need to clear/flush the keyboard.

so the logic would be:

movement:array[1..4] of boolean;

clear/flush keyboard buffer {that's the part i need the code/commands for}
if keypressed=true then
if ord(k)=77 {right arrow key} then movement[1]:=true;
if movement[1]=true then x:=x+1;
until keypressed;

can anyone help me out with this? if you know of a better way to get the same result please let me know. thanks


  • quikcarlxquikcarlx Hollywood, Fl
    I don't have any special routines, but clearing up some of the coding could make it do better. If you already have a logic variable (boolean) then you don't need to check it against a boolean, just use it. I took a shot at your snippet of code to see if it could be improved.
    movement : array[1..4] of boolean;
    k : char;

    {clear/flush keyboard buffer {that's the part i need the code/commands for}
    if keypressed then begin
    k := readkey;
    movement[1] := ord(k) = 77
    {[color=Red] or a more terse version
    if keypressed then
    movement[1] := ord( readkey ) = 77; [/color]}
    if movement[1] then
    x := x + 1;
    until keypressed;

  • Thanks a lot. The result I get with your suggestion is the character keeps moving in one direction without stopping even if I let go of the key. I already had that problem in a different coding I tried. I understand the idea of not reading the key and storing it straight into the boolean. but i see two problems there. the boolean can be either 1 or 0, true or false. movement[1]:=ord(k)=77, ord(k)=77 gives a greater value than 0 or 1 and can't be stored into a boolean, i think, although your code works in terms of storing a non boolean value into a boolean. the other problem is i need to know before hand which key has been pressed in order to assign true or false to the relevant movement array cell. for example, i need to know the value of readkey and if it's ord() is 80 then movement[1]=true, if it's 72 then movement[2]=true.

    what i'm trying to do is to program the movement of the player as follows: while pressing down arrow key keep moving down, when not pressing arrow keys stop moving (don't know how to program this bit). this is the idea. i have tried a lot of different ways and which ever way i try the character on screen keeps moving (trying to explain this in non coding terms).

    it seems to me that as long as there is a key pressed in the keyboard buffer the program will keep reading the value and executing player movement. i need to find a way to tell the program to stop moving the player if no arrow is being pressed. i've tried assigning a different value than those of arrow keys to the k variable (k:char), k:='o', but in order to know when to assign this i need to be able to know whether an arrow key is being pressed or not.

    so i could have something like:

    movement : array[1..4] of boolean;
    k : char;

    if keypressed=false then {will the program somehow clear the buffer when i'm not pressing a key, don't think so, i think it only clears when using readkey}
    if keypressed=true then begin
    k := readkey; {keyboard buffer presumably cleared here, hence the pause in player movement i think}
    if (ord(k)=80) then movement[1] := true;
    if (ord(k)=72) then movement[2] := true;
    if movement[1]=true then
    y := y + 1;
    if movement[2]=true then
    y := y - 1;
    until ord(k)=27;

    other programming languages like c++ actually has a command that can read the buffer or value for a specific key pressed. for example, in turbo pascal you have keypressed which just checks if any key has been pressed. in c++ it has something like down.keypress which checks if the down arrow key only has been pressed not the entire keyboard. now with this more specific command you can program the player movements without the delay while maintaining an arrow key pressed.

    how would you suggest i go about programming this? thanks for the help.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!