I’m trying to write a client for a easy game console, 1vs1. The game: 1 player has to catch the other; every player is represented by a char, # and $. To manage the data, I use 3 processes:
- Process A. Process A gets the position of the enemy from the server, and writes it on a pipe;
- Process B. Process B gets the input from the keyboard,updates the position of the player, writes it on a pipe;
- Process C. Process C reads the pipe and displays the map with the two characters, every one representing a player.
Now, to display the characters in process C, I use the mvaddch(), provided from <curses.h>. My problem is it seems not working, but if I use mvaddch() in process B, it works… “and I can paint a snake composed of consecutive x’s”.
...
initscr();
noecho();
curs_set(0);
role = position.c;
if ( pipe(filedes) == -1) {perror("pipe() fallita"); exit(0); }
// process A is creaded now
switch ( pid_nemico = fork() ) {
case -1: perror("fork() fallita"); exit(0);
case 0: close(filedes[0]);
while (1) {
while ( recv(sock, &position, sizeof(struct pos), 0) < 1 )
write(filedes[1], &position, sizeof(struct pos)); } // position rappresenta la posizione del nemico
default: break ; } // che viene mandata dal server
//process B is created now, and within mvaddch works
switch ( pid = fork() ) {
case -1: perror("fork() fallita"); exit(0);
case 0: close(filedes[0]);
char c;
while (1) {
switch(c=getch()) {
case SU: if(position.y>0) { position.y-=1;
write(filedes[1], &position, sizeof(struct pos));
send(sock, &position, sizeof(struct pos), 0); } break;
case GIU: if(position.y<MAXY-1){ position.y+=1;
write(filedes[1], &position, sizeof(struct pos));
send(sock, &position, sizeof(struct pos), 0); } break;
case SINISTRA: if(position.x>0){ position.x-=1;
write(filedes[1], &position, sizeof(struct pos));
send(sock, &position, sizeof(struct pos), 0); } break;
case DESTRA: if(position.x<MAXX-1){ position.x+=1;
write(filedes[1], &position, sizeof(struct pos));
send(sock, &position, sizeof(struct pos), 0); } break;
default: break; }
}
default: break ; }
// And this is the remaining Process, process C, where mvaddch does not work
close(filedes[1]);
struct pos pos_guardia = {'#', 0, 0};
struct pos pos_ladro = {'$', 0, 0};
read(filedes[0], &position, sizeof(struct pos));
while ((position.c == '#') || (position.c == '$')) {
switch (position.c) {
case '#': printf("%d\n",mvaddch(pos_guardia.y, pos_guardia.x, ' ') );
mvaddch(position.y, position.x, '#');
pos_guardia.x = position.x;
pos_guardia.y = position.y;
break;
case '$': printf("%d\n",mvaddch(pos_ladro.y, pos_ladro.x, ' ') );
mvaddch(position.y, position.x, '$');
pos_ladro.x = position.x;
pos_ladro.y = position.y;
break;
case 'G': if (role == '#') { printf("Hai vinto la Partita");
}else{ printf("Hai perso la Partita"); }
break;
case 'L': if (role == '$') { printf("Hai vinto la Partita");
}else{ printf("Hai perso la Partita"); }
break;
default: printf("Pacchetto ricevuto non interpretato"); }
read(filedes[0], &position, sizeof(struct pos)); }
kill(pid_nemico);
kill(pid);
printf("\n-----------------------------\n");
}
Since you never
refresh()the display, there’s no occasion for the characters to move.The
curseslibrary only sends updates to the screen when you userefresh().