I write a client for a console game, 1vs1. In the game one player have to catch the other, and every player is rappresented with a char, printed on the console. I use the mvaddch() to delete the old position, and to print the new position on the console.
My code generate 2 process:
- Process A: It get the input from the keyboard and update the position on the screen;
- Process B: it get the input from the server and update the position of the enemy on the screen;
My problem is the old position of the enemy is not deleted (overwriting with ‘ ‘), and so process B generates a snake of charactes on the screen. Process A work good.
initscr();
noecho();
curs_set(0);
//process A is created now
switch ( pid = fork() ) {
case -1: perror("fork() fallita"); exit(0);
case 0: {char c; struct pos old_position = {welcome_position.c, welcome_position.x, welcome_position.y};
struct pos position = {welcome_position.c, welcome_position.x, welcome_position.y};
mvaddch(position.y, position.x, position.c);
while (1) {
switch(c=getch()) {
case SU: if(position.y>0) { position.y-=1; } break;
case GIU: if(position.y<MAXY-1){ position.y+=1; } break;
case SINISTRA: if(position.x>0){ position.x-=1; } break;
case DESTRA: if(position.x<MAXX-1){ position.x+=1; } break;
default: break; }
if ((position.x != old_position.x) || (position.y != old_position.y)) {
send(sock, &position, sizeof(struct pos), 0);
mvaddch(old_position.y, old_position.x, ' ');
mvaddch(position.y, position.x, position.c);
refresh();
old_position.x = position.x;
old_position.y = position.y; }} }
default: break ; }
// Process B is here
struct pos position;
struct pos old_position={' ', -1,-1};
while (1) {
while ( recv(sock, &position, sizeof(struct pos), 0) < 1 )
mvaddch(old_position.y, old_position.x, ' '); // THE PROBLEM
mvaddch(position.y, position.x, position.c); // Works => snake
refresh();
old_position.x = position.x;
old_position.y = position.y;}
endwin();
kill(pid);
printf("\n-----------------------------\n");
}
If you don’t want the entire trail showing, you have to keep a record of the previous position of each character (player) and arrange to write a blank at the old position and the correct mark at the new position. If you’re feeling fancy, you might use a coloured blank, one colour for each player, so you can see where each has been, even though the current positions are marked differently.
Unfortunately, without the colouration mentioned, that looks like what you’re doing.
You should make sure you don’t use the
-1coordinates;mvaddch()is probably not error checked and will go trampling out of bounds, doing who knows what damage. Don’t risk it. (Consider using0, 0as the old position; it won’t matter if you write a blank over a blank. The only thing that matters is that the other player is not where you write the blank.)Note that it is crucial that only one process is doing the drawing. If you have two processes attempting to do so, you will lose one or the other images some of the time. This is one reason reason why it is hard to add a clock, say, to the top right corner of a terminal screen. You do seem to have two processes trying to write to the screen.
Style-wise, you need to use more functions. That is, the different processes should each have their code in a separate function, so that it is easier to see what they are doing. Stacking close braces
}three deep on a single line is not good style either.