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:
var
movement:array[1..4] of boolean;
k:char;
repeat
clear/flush keyboard buffer {that's the part i need the code/commands for}
if keypressed=true then
begin
k:=readkey;
if ord(k)=77 {right arrow key} then movement[1]:=true;
end;
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
Comments
[code]var
movement : array[1..4] of boolean;
k : char;
begin
repeat
{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
end;
{[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;
end.[/code]
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:
var
movement : array[1..4] of boolean;
k : char;
begin
repeat
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}
begin
movement[1]:=false;
movement[2]:=false;
end;
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;
end;
if movement[1]=true then
y := y + 1;
if movement[2]=true then
y := y - 1;
until ord(k)=27;
end.
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.