Tutorial: moving sunnyboys around killing master, common datestring =================================================================== Multiplayer program, where every player moves his own sunnyboy with arrow keys. Up to 4 players are can connect, virtual players replace missing players. Clients touching master's sunnyboy will kill the master. A date string is displayed, set by master and sent via common-block. step 0 The global declaration of the program in tut-network2.h, used by master player and client players, differs from tut-network1.h by adding the common-block definitions: ==>
/* struct for common-block, ** contains a string (char array) for master's datestring */ struct { char * date; // date string with arraysize "date_size" int date_size; // array size of "date" } pl_comm; |
/* common-block */ memset(&pl_comm,0,sizeof(pl_comm)); pl_comm.date_size=40; // datestring with arraysize 40 if (vg_nw_setcommon(NWVAR_CHAR,pl_comm.date_size,"date") < 1) {return(-1);} |
/* common-block */ pl_comm.date=(char *)vg_nw_getcommon("date"); |
if (i1==vg_nw_myplayer()) { // put master into right upper corner pl_data[i1-1].xpos=vg_bitmap_width(pl_data[i1-1].sunboy)/2; pl_data[i1-1].ypos=vg_bitmap_height(pl_data[i1-1].sunboy)/2; } |
/* set values for common-block */ pl_comm.date[0]='\0'; // clear datestring |
/* (go through all players) ... */ /* set timestring: common variable pl_comm.date */ {time_t zt=time(NULL); char * ptr; snprintf(pl_comm.date,pl_comm.date_size,"%s",ctime(&zt)); if ((ptr=strchr(pl_comm.date,'\n'))!=NULL) {*ptr='\0';} // kill Newline pl_comm.date[pl_comm.date_size-1]='\0'; // prevent overflow vg_nw_sendcommon(); // send common-block to network-server } /* give out common date */ vg_draw_text(NULL,RGB_WHITE,0,0,pl_comm.date,NULL,RGB_TRANS); /* (flush all out to window) ... */ |
/* (save master request values in x,y) ... */ /* check whether master clashed with another sunnyboy */ {int i1; for (i1=1; i1 <= vg_nw_maxplayer(); i1++) { if (i1==plno) {continue;} // don't check with yourself if (vg_nw_isalive(i1)) { /* player is alive */ int x1,y1,x2,y2; /* start position of master sunnyboy */ x1=pl_data[plno-1].xpos; y1=pl_data[plno-1].ypos; /* position of other sunnyboy */ x2=pl_data[i1-1].xpos; y2=pl_data[i1-1].ypos; /* check bitmap overlapping */ if (vg_bitmap_overlap(NULL,pl_data[plno-1].sunboy,x1,y1,x1+x,y1+y,pl_data[i1-1].sunboy,x2,y2,1)) { /* master sunnyboy clashed */ vg_nw_setdead(plno); // set dead to inform network-server return(0); // do not end the game (wait for the second keystroke) } } } } |
/* (go through all players) ... */ /* give out common date */ vg_draw_text(NULL,RGB_WHITE,0,0,pl_comm.date,NULL,RGB_TRANS); /* (flush all out to window) ... */ |