I have been trying to create pong game using ncurses library on a terminal. I have successfully created the two bars for both the players. The problem is that when both the players press their corresponding keys, only one of them was moving.
So, I searched the net and found that we can use windows, where we can have logical curser to each window and hence execute the commands from the players parallely. The code-snippet of my program looks as follows:
pthread_t player_1, player_2;
pthread_create(&player_1, NULL, (void*)&player_1_move, (void*)pl_window);
pthread_create(&player_2, NULL, (void*)&player_2_move, (void*)p2_window);
while(1)
{
char c=getch();
if(key is from p1)
//signal player_1 thread to refresh the bar position
if(key is from p2)
//refresh it for player_2 by signaling 2nd thread..
}
//here player_1_move and player_2_move are the functions for changing the bar position for each of the players..I have created two windows in which i draw these bars, which i haven't shown in my code..
The problem is that though I use the pthreads, i still can’t see both moving simultaneously, when both players press at-a-time.
Any suggestions plz…
As Craig said… a terminal doesn’t handle when 2 keys are pressed at the same time. It will choose any one of them.. That was the exact problem. and can’t be resolved even if we use
pthreads.