Advertisement
nigu

difesa aerea avanzata

Dec 13th, 2014
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.19 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <curses.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <pthread.h>
  6. #include <time.h>
  7.  
  8. #define SOPRA 65
  9. #define SOTTO 66
  10. #define SINISTRA 68
  11. #define DESTRA 67
  12.  
  13. #define MAXX 80
  14. #define MAXY 24
  15.  
  16. #define DELAY 50000
  17.  
  18. struct position {
  19.     int x;
  20.     int y;
  21. };
  22.  
  23. struct position nemico1;
  24. struct position nemico2;
  25. struct position amico;
  26. struct position balloon;
  27.  
  28. int collision;
  29.  
  30. pthread_mutex_t mtx;
  31.  
  32.  
  33. void AreaGioco(){
  34.  
  35.     int i=0;
  36.     int scudi=3;
  37.    
  38.    
  39.    
  40.     while (collision==1){
  41.         pthread_mutex_lock(&mtx);
  42.         mvprintw(0,0,"SCUDI %d", scudi);
  43.         pthread_mutex_unlock(&mtx);
  44.        
  45.        
  46.         if (!(i++%100)){
  47.             pthread_mutex_lock(&mtx);
  48.             mvaddch(balloon.y,balloon.x,' ');
  49.             balloon.x = random()%MAXX;
  50.             balloon.y = random()%MAXY;
  51.             mvaddch(balloon.y,balloon.x,'O');
  52.             refresh();
  53.             pthread_mutex_unlock(&mtx);
  54.            
  55.         }
  56.        
  57.         if (amico.x == balloon.x && amico.y == balloon.y){
  58.             if (scudi<6){
  59.                 scudi++;
  60.             }
  61.            
  62.             pthread_mutex_lock(&mtx);
  63.             balloon.x = random()%MAXX;
  64.             balloon.y = random()%MAXY;
  65.             pthread_mutex_unlock(&mtx);
  66.            
  67.         }
  68.        
  69.        
  70.        
  71.         if (((amico.x == nemico1.x && amico.y == nemico1.y) || (amico.x == nemico1.x && amico.y == nemico1.y))||
  72.             ((amico.x == nemico2.x && amico.y == nemico2.y) || (amico.x == nemico2.x && amico.y == nemico2.y))){
  73.             if (scudi>0){
  74.                 scudi--;
  75.             } else {           
  76.                 pthread_mutex_lock(&mtx);
  77.                 collision=0;
  78.                 pthread_mutex_unlock(&mtx);
  79.             }
  80.            
  81.         }
  82.        
  83.        
  84.        
  85.        
  86.         pthread_mutex_lock(&mtx);
  87.         refresh();
  88.         pthread_mutex_unlock(&mtx);
  89.         usleep(DELAY);
  90.    
  91.     }
  92.    
  93. }
  94.  
  95. void *aereonemico1(){
  96.    
  97.     int rand_Y;
  98.     int i=0;
  99.    
  100.     //struct position old1;
  101.    
  102.     nemico1.x = 0;
  103.    
  104.    
  105.     srand(time(NULL));
  106.    
  107.     while (collision=1){
  108.        
  109.         nemico1.y = 1+random()%MAXY-1;     
  110.        
  111.         for (i=1;i<MAXX+1;i++){
  112.             pthread_mutex_lock(&mtx);
  113.             mvaddch(nemico1.y,nemico1.x,' ');
  114.             pthread_mutex_unlock(&mtx);
  115.             nemico1.x=i;
  116.             pthread_mutex_lock(&mtx);
  117.             mvaddch(nemico1.y,nemico1.x,'+');
  118.             refresh();
  119.             pthread_mutex_unlock(&mtx);
  120.            
  121.             usleep(DELAY);
  122.        
  123.         }
  124.         pthread_mutex_lock(&mtx);
  125.         mvaddch(nemico1.y,nemico1.x,' ');
  126.         pthread_mutex_unlock(&mtx);
  127.     }
  128.  
  129.  
  130. }
  131.  
  132.  
  133.  
  134.  
  135. void *aereonemico2(){
  136.    
  137.     int rand_Y;
  138.     int i=0;
  139.    
  140.     //struct position old1;
  141.    
  142.     nemico2.y = 0;
  143.    
  144.    
  145.     srand(time(NULL));
  146.    
  147.     while (collision=1){
  148.        
  149.         nemico2.x = 1+random()%MAXX-1;     
  150.        
  151.         for (i=1;i<MAXY+1;i++){
  152.             pthread_mutex_lock(&mtx);
  153.             mvaddch(nemico2.y,nemico2.x,' ');
  154.             pthread_mutex_unlock(&mtx);
  155.             nemico2.y=i;
  156.             pthread_mutex_lock(&mtx);
  157.             mvaddch(nemico2.y,nemico2.x,'X');
  158.             refresh();
  159.             pthread_mutex_unlock(&mtx);
  160.            
  161.             usleep(DELAY);
  162.        
  163.         }
  164.         pthread_mutex_lock(&mtx);
  165.         mvaddch(nemico2.y,nemico2.x,' ');
  166.         pthread_mutex_unlock(&mtx);
  167.     }
  168.  
  169.  
  170. }
  171.  
  172.  
  173. void *aereoamico(){
  174.  
  175.     char key;
  176.    
  177.     amico.x = MAXX/2;
  178.     amico.y = MAXY/2;
  179.    
  180.     pthread_mutex_lock(&mtx);
  181.     mvaddch(amico.y,amico.x,'#');
  182.     refresh();
  183.     pthread_mutex_unlock(&mtx);
  184.    
  185.     while (collision=1){
  186.         key = getch();
  187.        
  188.         pthread_mutex_lock(&mtx);
  189.         mvaddch(amico.y,amico.x,' ');
  190.         pthread_mutex_unlock(&mtx);
  191.        
  192.         pthread_mutex_lock(&mtx);
  193.         switch(key){
  194.             case SOPRA:
  195.                 if (amico.y>0)
  196.                     amico.y--;
  197.                 //mvaddch(amico.y,amico.x,'#');
  198.                 break;
  199.             case SOTTO:
  200.                 if (amico.y<MAXY)
  201.                     amico.y++;
  202.                 //mvaddch(amico.y,amico.x,'#');
  203.                 break;
  204.             case SINISTRA:
  205.                 if (amico.x>0)
  206.                     amico.x--;
  207.                 //mvaddch(amico.y,amico.x,'#');
  208.                 break;
  209.             case DESTRA:
  210.                 if (amico.x<MAXX-1)
  211.                     amico.x++;
  212.                 //mvaddch(amico.y,amico.x,'#');
  213.                 break;
  214.             default:
  215.                 break;
  216.         }
  217.         mvaddch(amico.y,amico.x,'#');
  218.         refresh();
  219.         pthread_mutex_unlock(&mtx);
  220.    
  221.     }
  222.    
  223.    
  224.  
  225.  
  226. }
  227.  
  228.  
  229. int main (){
  230.    
  231.     pthread_t nemico1;
  232.     pthread_t nemico2;
  233.     pthread_t amico;
  234.    
  235.     pthread_mutex_init(&mtx, NULL);
  236.    
  237.     initscr();
  238.     noecho();
  239.     curs_set(0);
  240.    
  241.     collision=1;
  242.    
  243.     pthread_create(&nemico1, NULL, aereonemico1, NULL);
  244.     pthread_create(&nemico2, NULL, aereonemico2, NULL);
  245.     pthread_create(&amico, NULL, aereoamico, NULL);
  246.    
  247.     AreaGioco();
  248.    
  249.     pthread_join(nemico1,NULL);
  250.     pthread_join(nemico2,NULL);
  251.     pthread_join(amico,NULL);
  252.    
  253.     pthread_mutex_destroy(&mtx);
  254.    
  255.     endwin();
  256.    
  257.     printf("-- GAME OVER --");
  258.    
  259.    
  260.     return 0;
  261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement