Advertisement
nigu

tread

Nov 26th, 2014
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.20 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <curses.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #define MAXX 80 /* Numero di colonne dello schermo */
  6. #define MAXY 24 /* Numero di righe dello schermo */
  7. #define PASSO 1
  8. #define SPEED 500000
  9. #define N 2
  10.  
  11. struct pos {
  12.     char c;
  13.     int x; /* coordinata x */
  14.     int y; /* coordinata y */
  15. };
  16.  
  17. void* print_xs (void* parameters){
  18.     int r;
  19.     int dx, dy;
  20.     struct pos* p;
  21.    
  22.     p = (struct pos*) parameters;
  23.    
  24.     while (1){
  25.         r=random();
  26.         if(r<RAND_MAX/2)
  27.             dx=PASSO;
  28.         else
  29.             dx=-PASSO;
  30.         if(p->x+dx<1 || p->x+dx>=MAXX)
  31.             dx=-dx;
  32.         p->x+=dx;
  33.        
  34.         r=random();
  35.         if(r<RAND_MAX/2)
  36.             dy=PASSO;
  37.         else
  38.             dy=-PASSO;
  39.         if(p->y+dy<1 || p->y+dy>=MAXY)
  40.             dy=-dy;
  41.         p->y+=dy;
  42.        
  43.         mvaddch(p->y,p->x,p->c);
  44.         refresh();
  45.         usleep(SPEED);
  46.         mvaddch(p->y,p->x,' ');
  47.     }
  48.          
  49.     return NULL;   
  50. }
  51.  
  52. int main () {
  53.         pthread_t thread1_id;
  54.         struct pos thread1_args;
  55.        
  56.         initscr();
  57.         noecho();
  58.         curs_set(0);
  59.  
  60.         thread1_args.c = random();
  61.         thread1_args.x = random()%MAXX;
  62.         thread1_args.y = random()%MAXY;
  63.        
  64.         pthread_create (&thread1_id, NULL, &print_xs, &thread1_args);
  65.         pthread_join (thread1_id, NULL);
  66.        
  67.         endwin();
  68.         return 0;
  69.        
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement