I am making an ASCII movement system in ncurses with C. As of now if I press movement buttons character moves just fine (example of movement function):
void hero_move_right(){
if (allow_move_to(Y, X + 1)){
mvaddch(Y, X, EMPTY);
X += 1;
mvaddch(Y, X, MVRIGHT);
}
}
Loop in main looks like this:
int input;
bool key_was_preessed = false;
do {
input = getch();
switch (input){
case w:
case W:
hero_move_up();
key_was_preessed = true;
break;
case s:
case S:
hero_move_down();
key_was_preessed = true;
break;
case d:
case D:
hero_move_right();
key_was_preessed = true;
break;
case a:
case A:
hero_move_left();
key_was_preessed = true;
break;
/*determines of the game has started*/
if (key_was_preessed == true){
mvaddch(((row - MX)/2 + 1), ((col - MY)/2 + 1), EMPTY);
move(Y, X);
}
}
} while ((input != q) && (input != Q));
Right now a player can press or hold keys to move around. Is there a way to detect if the key is on hold? (I tried storing last two pressed buttons in an array and comparing them to each other but this way the player can only move around by constantly pressing different keys. ex: was - w-w-w became - w-s-w-d-w etc.) The idea is to be able to move around by just pressing buttons so that the speed of movement depends on how fast the player can press buttons. You can check the code of the project here: https://github.com/theonlymoby/collector
Thanks for the help!