我希望我的程序从标准inputstream同时读取数字键盘和键盘。 我不知道这是可能的,这就是为什么我问。
我的程序工作正常,但是如果第一个玩家仍然按下箭头,第二个玩家不能同时按z / x来移动开关,除非第一个玩家停止按下箭头。
#include <stdio.h> #include <pthread.h> #include <windows.h> void first_player_arrow_press(); void second_player_Z_or_X_press(); void *second_player(); int arrow = 0, button_pressed = 0; int z = 50; int main() { pthread_t my_thread; int x = 50; char buff[] = "################"; // the paddle char buff1[] = " "; HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE); while (1) { arrow = 0; button_pressed = 0; first_player_arrow_press(); switch (arrow) { case 1: x = x; COORD clear1 = {x, 0}; SetConsoleCursorPosition(hOutput, clear1); WriteConsoleA(hOutput, buff1, 15, NULL, NULL); x = x - 5; COORD coord = {x, 0}; SetConsoleCursorPosition(hOutput, coord); WriteConsoleA(hOutput, buff, 15, NULL, NULL); break; case 2: x = x; COORD clear2 = {x, 0}; SetConsoleCursorPosition(hOutput, clear2); WriteConsoleA(hOutput, buff1, 15, NULL, NULL); x = x + 5; COORD coord1 = {x, 0}; SetConsoleCursorPosition(hOutput, coord1); WriteConsoleA(hOutput, buff, 15, NULL, NULL); break; } pthread_create(&my_thread, NULL, second_player, NULL); pthread_join(my_thread, NULL); } return 0; } void first_player_arrow_press() { int ch = getch(); if (ch == 0 || ch == 224) { switch (getch()) { case 75: // left arrow = 1; break; case 77: // right arrow = 2; break; } } } void second_player_Z_or_X_press() { int ch = getch(); switch (getch()) { case 122: case 90: // left button_pressed = 1; // pressed Z , go left break; case 120: case 88: // pressed X , go right button_pressed = 2; break; } } void *second_player() { HANDLE hOutput1 = GetStdHandle(STD_OUTPUT_HANDLE); char buff[] = "################"; char buff1[] = " "; second_player_Z_or_X_press(); switch (button_pressed) { case 1: z = z; COORD clears = {z, y}; SetConsoleCursorPosition(hOutput1, clears); WriteConsoleA(hOutput1, buff1, 15, NULL, NULL); z = z - 5; COORD coords = {z, y}; SetConsoleCursorPosition(hOutput1, coords); WriteConsoleA(hOutput1, buff, 15, NULL, NULL); break; case 2: z = z; COORD clears2 = {z, y}; SetConsoleCursorPosition(hOutput1, clears2); WriteConsoleA(hOutput1, buff1, 15, NULL, NULL); z = z + 5; COORD coords1 = {z, y}; SetConsoleCursorPosition(hOutput1, coords1); WriteConsoleA(hOutput1, buff, 15, NULL, NULL); break; } }
这也可能是键盘上的重影问题。 如果你看亚马逊的游戏键盘,一些将有一套反重影组合键。 这些是组合使用时不相互冲突的键。 例如,HAVIT HV-KB380L LED在x / z和箭头上具有防重影。