Advertisement
nigu

difesa_aerea_thread

Dec 4th, 2014
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.32 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <unistd.h>
  3. #include <pthread.h>
  4. #include <ncurses.h>
  5.  
  6. #define UP 65   /* Cursore sopra */
  7. #define DW 66   /* Cursore sotto */
  8. #define SX 68   /* Cursore sinistra */
  9. #define DX 67   /* Cursore destra */
  10. #define MAXX 80 /* Dimensione dello schermo di output (colonne) */
  11. #define MAXY 20 /* Dimensione dello schermo di output (righe)   */
  12. #define DELAY 50000   /* Ritardo nel movimento degli aerei nemici (da adattare) */
  13.  
  14. /* Prototipi delle funzioni adoperate */
  15. void* Nemico(void* args);
  16. void* Amico(void* args);
  17. void Area(void);
  18.  
  19. int collision;
  20.  
  21. pthread_mutex_t muta=PTHREAD_MUTEX_INITIALIZER;
  22.  
  23.  
  24. struct position {
  25.   int  x;        /* Coordinata X */
  26.   int  y;        /* Coordinata Y */
  27. };
  28.  
  29. struct position nemico;
  30. struct position amico;
  31.  
  32. int main() {
  33.        
  34.         pthread_t thread_nemico;
  35.         pthread_t thread_amico;
  36.        
  37.        
  38.         srand(time(NULL)); /* Inizializza generatore di numeri casuali */
  39.        
  40.         nemico.x=0;
  41.         nemico.y=0;
  42.        
  43.         amico.x = MAXX/2;
  44.         amico.y = MAXY/2;
  45.        
  46.                
  47.         initscr(); /* Inizializza schermo di output */
  48.         noecho(); /* Imposta modalità della tastiera */
  49.         curs_set(0); /* Nasconde il cursore */
  50.                
  51.        
  52.         pthread_create (&thread_nemico, NULL, &Nemico, NULL);
  53.         pthread_create (&thread_amico, NULL, &Amico, NULL);
  54.        
  55.         Area();
  56.        
  57.         endwin();               /* Ripristino la modalità di funzionamento usuale */
  58.  
  59.         printf("\n\n\nGAME OVER\n\n\n");                        /* Termino il gioco ed esco dal programma */
  60.        
  61.         //pthread_join(thread_nemico, NULL);
  62.         //pthread_join(thread_amico, NULL);
  63.        
  64.         return 0;              
  65. }
  66.  
  67.  
  68. void* Nemico(void* args) {
  69.  
  70.         int YX=0;                               /* Variabile per direzione movimento aereo */
  71.        
  72.         struct position prev;
  73.        
  74.         prev.x = 1;
  75.         prev.y = 1;
  76.        
  77.         while(1) {
  78.                
  79.                 if(random()<RAND_MAX/2)
  80.                         YX=0;
  81.                 else
  82.                         YX=1;  
  83.        
  84.                 if(YX==0) {
  85.                         nemico.y = random()%MAXY;
  86.                         for(nemico.x=2;nemico.x<MAXX;nemico.x++) { /* Effettuo il percorso da x=0 a x=MAXX */
  87.                                 pthread_mutex_lock(&muta);
  88.                                 mvaddch(prev.y, prev.x, ' ');
  89.                                 mvaddch(nemico.y, nemico.x, '+');
  90.                                 refresh();
  91.                                 pthread_mutex_unlock(&muta);
  92.                                 prev.x=nemico.x;
  93.                                 prev.y=nemico.y;
  94.                                 usleep(DELAY); /* Inserisco una pausa per rallentare il movimento */
  95.                         }
  96.                 }
  97.                 else {
  98.                         nemico.x = random()%MAXX; /* Genero casualmente una posizione x di partenza */
  99.                         for(nemico.y=2;nemico.y<MAXY;nemico.y++) { /* Effettuo il percorso da y=0 a y=MAXY */
  100.                             pthread_mutex_lock(&muta);
  101.                                 mvaddch(prev.y, prev.x, ' ');
  102.                                 mvaddch(nemico.y, nemico.x, '+');
  103.                                 refresh();
  104.                                 pthread_mutex_unlock(&muta);
  105.                                 prev.x=nemico.x;
  106.                                 prev.y=nemico.y;
  107.                                 usleep(DELAY); /* Inserisco una pausa per rallentare il movimento */
  108.                         }
  109.                 }
  110.          }
  111. }
  112.  
  113. /*
  114. ----------------------------------------------------------------------
  115.  Funzione 'Aereo Amico' - Movimento tramite i tasti cursore
  116. ----------------------------------------------------------------------
  117. */
  118. void* Amico(void* args) {
  119.    
  120.         char c;
  121.         struct position prev;
  122.         prev.x = amico.x;
  123.         prev.y = amico.y;
  124.         pthread_mutex_lock(&muta);
  125.            
  126.             mvaddch(amico.y, amico.x, '#');
  127.             refresh();
  128.             pthread_mutex_unlock(&muta);
  129.        
  130.         while(1) { /* Lettura dei tasti cursore */
  131.             c = getch();
  132.             pthread_mutex_lock(&muta);
  133.             if(c==UP && amico.y > 0)
  134.                             amico.y-=1;    
  135.             if(c==DW && amico.y < MAXY-1)
  136.                             amico.y+=1;
  137.             if(c==SX && amico.x > 0)                                              
  138.                             amico.x-=1;                                    
  139.             if(c==DX && amico.x < MAXX-1)  
  140.                             amico.x+=1;
  141.             pthread_mutex_unlock(&muta);
  142.             pthread_mutex_lock(&muta);
  143.            
  144.             mvaddch(prev.y, prev.x, ' ');
  145.             mvaddch(amico.y, amico.x, '#');
  146.            
  147.             refresh();
  148.             pthread_mutex_unlock(&muta);
  149.             prev.y=amico.y;
  150.             prev.x=amico.x;
  151.            
  152.         }
  153. }
  154.  
  155. void Area(void) {
  156.         struct position bonus1;        
  157.         struct position bonus2;
  158.         struct position bonus3;
  159.        
  160.         int i=0;
  161.         int collision=0;
  162.         int max_collision = 3;
  163.        
  164.  
  165.         do {
  166.            
  167.             mvprintw(0,1,"SCUDO %3d", max_collision);  
  168.             if(!(i++%100)) {
  169.                 pthread_mutex_lock(&muta);
  170.                 mvaddch(bonus1.y, bonus1.x, ' ');
  171.                 mvaddch(bonus2.y, bonus2.x, ' ');
  172.                 mvaddch(bonus3.y, bonus3.x, ' ');
  173.                 pthread_mutex_unlock(&muta);
  174.                
  175.                 bonus1.x = rand()%MAXX;
  176.                 bonus1.y = rand()%MAXY;
  177.                        
  178.                 bonus2.x = rand()%MAXX;
  179.                 bonus2.y = rand()%MAXY;
  180.                        
  181.                 bonus3.x = rand()%MAXX;
  182.                 bonus3.y = rand()%MAXY;
  183.                
  184.                 pthread_mutex_lock(&muta);
  185.                 mvaddch(bonus1.y,bonus1.x,'O');
  186.                 mvaddch(bonus2.y,bonus2.x,'O');
  187.                 mvaddch(bonus3.y,bonus3.x,'O');
  188.                 refresh();
  189.                 pthread_mutex_unlock(&muta);
  190.            
  191.             }
  192.             pthread_mutex_lock(&muta);
  193.             if ((nemico.x == amico.x) && (nemico.y == amico.y)){
  194.                 max_collision--;
  195.                 if (max_collision < 0){
  196.                     collision=1;
  197.                 }      
  198.             }
  199.             pthread_mutex_unlock(&muta);
  200.             pthread_mutex_lock(&muta);
  201.             if ((nemico.x == bonus1.x && nemico.y == bonus1.y)||
  202.             (nemico.x == bonus2.x && nemico.y == bonus2.y)||
  203.             (nemico.x == bonus3.x && nemico.y == bonus3.y)) {
  204.                 max_collision = 3;
  205.                 collision = 0;
  206.            
  207.             }
  208.             pthread_mutex_unlock(&muta);
  209.                
  210.         usleep(DELAY);
  211.        
  212.     } while(!collision);
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement