开发者

movement binding in c++ using ncurses

开发者 https://www.devze.com 2023-03-08 21:51 出处:网络
I can\'t get this movement binding to work.I\'m using the ncurses library, update_ch and oldch are global variables.KEYERR is a macro set to -120 (I just don\'t handle those keypresses).I\'m trying to

I can't get this movement binding to work. I'm using the ncurses library, update_ch and oldch are global variables. KEYERR is a macro set to -120 (I just don't handle those keypresses). I'm trying to开发者_StackOverflow restrict the player so he can't hold up, down, left, or right, but he has to keep pressing them to move. It's not working, you can still hold the keys down and move. Any suggestions? My logic must be off.

    if(update_ch != KEYERR)
    {
          oldch = update_ch;
    }
    update_ch = getch();
    if(oldch == update_ch)
    {
          update_ch = KEYERR;
    }


I'm trying to restrict the player so he can't hold up, down, left, or right, but he has to keep pressing them to move.

I am pretty sure this isn't possible with curses. If I remember correctly curses only receives characters from a terminal. It doesn't control anything about the process.

Measuring the time between to such readings might give you a hint if the user is holding a key instead of continuously pressing. I mean, when you do a reading, record the following

  • Key read
  • Time of read (millisecond precision)

When you read a value, ask the following:

  • Is it the same as the last key ?
  • What's the difference between the current time and the time of the last read ?

If it's the same key and the time difference is smaller than some threshold you can decide he's holding the key down.

0

精彩评论

暂无评论...
验证码 换一张
取 消