Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <curses.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <time.h>
- #define SOPRA 65
- #define SOTTO 66
- #define SINISTRA 68
- #define DESTRA 67
- #define MAXX 80
- #define MAXY 24
- #define DELAY 50000
- struct position {
- int x;
- int y;
- char c;
- };
- void Area (int pipein);
- void Nemico1 (int pipeout);
- void Nemico2 (int pipeout);
- void Amico (int pipeout);
- int main()
- {
- int scudo = 3;
- int p[2];
- int pidN1;
- int pidN2;
- int pidA;
- initscr();
- noecho();
- curs_set(0);
- srand(time(NULL));
- pipe(p);
- pidN1 = fork();
- if (pidN1==0){
- /*sono nel processo figlio N1*/
- Nemico1(p[1]);
- }
- else {
- pidN2=fork();
- if (pidN2==0) {
- /*sono nel processo figlio N2*/
- Nemico2(p[1]);
- }
- else {
- pidA=fork();
- if (pidA==0){
- /*sono nel processo figlio A*/
- mvaddch(MAXY/2,MAXX/2,'#');
- Amico(p[1]);
- }
- else {
- /* sono nel padre Area*/
- Area(p[0]);
- }
- }
- }
- kill(pidN1);
- kill(pidN2);
- kill(pidA);
- endwin();
- printf("GAME OVER");
- return 0;
- }
- void Amico (pipeout){
- struct position amico;
- int key;
- amico.c = '#';
- amico.x=MAXX/2;
- amico.y=MAXY/2;
- write(pipeout,&amico,sizeof(amico));
- while (1){
- key = getch();
- switch (key) {
- case SOPRA:
- if (amico.y>0)
- amico.y--;
- break;
- case SOTTO:
- if (amico.y<MAXY-1)
- amico.y++;
- break;
- case DESTRA:
- if (amico.x<MAXX-1)
- amico.x++;
- break;
- case SINISTRA:
- if (amico.x>0)
- amico.x--;
- break;
- }
- write(pipeout,&amico,sizeof(amico));
- }
- }
- void Nemico1(pipeout){
- struct position nemico1;
- int i=0;
- nemico1.x = 1;
- nemico1.y = 2;
- nemico1.c = '+';
- while (1){
- nemico1.y = 2 + random()%MAXY-2;
- for (i=1;i<MAXX;i++){
- nemico1.x=i;
- write(pipeout,&nemico1,sizeof(nemico1));
- usleep(DELAY);
- }
- }
- }
- void Nemico2(pipeout){
- struct position nemico2;
- int i=0;
- nemico2.y = 2;
- nemico2.c = 'X';
- while (1){
- nemico2.x = random()%MAXX;
- for (i=2;i<MAXY;i++){
- nemico2.y=i;
- write(pipeout,&nemico2,sizeof(nemico2));
- usleep(DELAY);
- }
- }
- }
- void Area(pipein){
- int collision=0;
- int scudi=3;
- int i;
- struct position elemento;
- struct position nemico1;
- struct position nemico2;
- struct position amico;
- struct position balloon;
- while (!collision){
- mvprintw(0,1,"SCUDI %d", scudi);
- refresh();
- read(pipein,&elemento,sizeof(elemento));
- switch (elemento.c){
- case '+':
- mvaddch(nemico1.y,nemico1.x,' ');
- nemico1.x=elemento.x;
- nemico1.y=elemento.y;
- mvaddch(nemico1.y,nemico1.x,'+');
- break;
- case 'X':
- mvaddch(nemico2.y,nemico2.x,' ');
- nemico2.x=elemento.x;
- nemico2.y=elemento.y;
- mvaddch(nemico2.y,nemico2.x,'X');
- break;
- case '#':
- mvaddch(amico.y,amico.x,' ');
- amico.x=elemento.x;
- amico.y=elemento.y;
- mvaddch(amico.y,amico.x,'#');
- break;
- default:
- break;
- }
- if (!(i++%100)){
- mvaddch(balloon.y,balloon.x,' ');
- balloon.x=random()%MAXX;
- balloon.y=random()%MAXY;
- mvaddch(balloon.y,balloon.x,'O');
- }
- if ((amico.x == nemico1.x && amico.y == nemico1.y)||
- (amico.x == nemico2.x && amico.y == nemico2.y)){
- if (scudi>0){
- scudi--;
- }
- else {
- collision = 1;
- }
- }
- if (amico.x == balloon.x && amico.y == balloon.y){
- if (scudi<6){
- scudi++;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement