سؤال

I'm trying to make Tetris game in standard console. I need non-blocking getch(), so the blocks can fall without pressing any key. It would be nice to have function that returns -1 if no key pressed, otherwise the key code.

هل كانت مفيدة؟

المحلول

It's operating system specific but your library probably has a function called kbhit() or similar that will do this

نصائح أخرى

This is exactly what you wanted:

int getch_noblock() {
    if (_kbhit())
        return _getch();
    else
        return -1;
}

Basically kbhit() does the job of determining if a key is pressed.

Assumes Windows and Microsoft Visual C++.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top