Advertisement
Guest User

roberta

a guest
May 21st, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.28 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <curses.h>
  5. #include <time.h>
  6. #include <thread>
  7. #include <string>
  8. #include <vector>
  9. #include <math.h>
  10. #include <iostream>
  11.  
  12. using namespace std;
  13.  
  14. int RELAY = 20000;
  15.  
  16. int sleeping=0;
  17. pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  18. pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
  19.  
  20.  
  21. bool ramka=true;
  22. pthread_t threads1[20];
  23. class Ball
  24. {
  25. public:
  26. int color;
  27. double x; // wspolrzedne
  28. double y;
  29. double velx; // predkosc w osi x i y
  30. double vely;
  31. int time;
  32. bool En; // jest w ruchu
  33. bool sleep;
  34.  
  35. Ball()
  36. {
  37. En=false;
  38. sleep=false;
  39. x=40;
  40. y=20;
  41. velx=(rand()%5) - 2; // losowanie kierunku oraz predkosci w osi x
  42. vely=rand()%5; // losowanie predkosci w osi y
  43. if(velx==0 && vely==0) vely=-1; // jesli wylosuje 0 i 0, czyli bez ruchu to spadek swobodny
  44. time = RELAY;
  45. }
  46. void Wyp();
  47. };
  48.  
  49. int l_box=5;
  50. int r_box=10;
  51. int u_box=40;
  52. int d_box=45;
  53.  
  54. /*
  55. WYPISYWANIE
  56. */
  57. void Ball::Wyp()
  58. {
  59.  
  60. attrset(COLOR_PAIR(color));
  61. mvprintw(round(y), round(x), "o");
  62. attrset(COLOR_PAIR(0));
  63.  
  64. }
  65. vector <Ball> tab(20);
  66.  
  67. // Obsluga thread'a do rysowania, refresh
  68. void *ref(void *args)
  69. {
  70. while(true)
  71. {
  72. int maxX, maxY;
  73. getmaxyx(stdscr, maxY, maxX);
  74. char c=getch();
  75. if(c=='q' || c=='e') break; // q-quit e-exit
  76. clear();
  77.  
  78. for(Ball b : tab) // kazdy obiekt klasy Ball znajdujacy sie w tab
  79. {
  80. if(b.En==true)
  81. {
  82. b.Wyp(); // rysuj kazda kulke w ruchu
  83. }
  84. }
  85.  
  86.  
  87. if(ramka)
  88. {
  89. // malowanie kwadratu 5x5 (stale wartosci)
  90. for(int i=l_box; i<r_box-1; i++)
  91. {
  92. mvprintw(i, u_box, "x");
  93. mvprintw(i, d_box-1, "x");
  94. }
  95.  
  96. for(int i=u_box; i<d_box; i++)
  97. {
  98. mvprintw(l_box-1, i, "x");
  99. mvprintw(r_box-1, i, "x");
  100. }
  101.  
  102. for(int i=1;i<maxY;i++)
  103. {
  104. mvprintw(i, 0, "|");
  105. mvprintw(i, maxX-1, "|");
  106. }
  107. for(int i=1;i<maxX-1;i++)
  108. {
  109.  
  110. mvprintw(0, i, "_");
  111. mvprintw(maxY-1, i, "_");
  112. }
  113. }
  114.  
  115. refresh();
  116. usleep(RELAY);
  117. }
  118. pthread_exit(NULL);
  119. }
  120.  
  121. // Obsluga ruchu kulek, zmien ich stan En-> true, by je rysowac w wyzszej metodzie
  122. void *p_work(void *args){
  123. // sygnal zakonczenia
  124. int k=(int)args;
  125. Ball& b = tab.at(k);
  126.  
  127. int maxX, maxY;
  128. getmaxyx(stdscr,maxY, maxX);
  129.  
  130.  
  131. b.color=k%6;
  132. b.x=maxX/2; // wystartuj kulki ze srodka aktualnego ekranu
  133. b.y=20; // poczatkowa wysokosc
  134. int maxx = maxX;
  135. int maxy = maxY-1;
  136.  
  137. // obsluga znikania kulek w kwadracie
  138.  
  139. if(b.x>=l_box && b.x<=r_box)
  140. {
  141. if(b.y>=d_box && b.x<=u_box){
  142. b.En=false;
  143. b.sleep=true;
  144. /*pthread_mutex_lock(&mutex);
  145. sleeping++;
  146. pthread_cond_wait(&cond, &mutex);
  147. b.sleep=false;
  148. b.En=true;
  149. pthread_mutex_unlock(&mutex);
  150. */
  151.  
  152. }
  153. }
  154. else b.En=true; // zmiana stanu
  155.  
  156. while(true)
  157. {
  158. Ball& b = tab.at(k);
  159.  
  160. if (b.sleep){
  161. pthread_mutex_lock(&mutex);
  162. sleeping++;
  163. pthread_cond_wait(&cond, &mutex);
  164. b.sleep=false;
  165. pthread_mutex_unlock(&mutex);
  166. }
  167.  
  168. else {
  169. b.x+=b.velx; // przesuniecie kulki
  170. b.y-=b.vely;
  171.  
  172.  
  173. if(b.x>=maxx) // obsluga odbic
  174. {
  175. b.velx*=-1;
  176. b.velx/=2.0f;
  177. b.x+=b.velx;
  178. }
  179. if(b.x<=1)
  180. {
  181. b.velx*=-1;
  182. b.velx/=2.0f;
  183. b.x+=b.velx;
  184. }
  185. if(b.y>maxy)
  186. {
  187. b.vely*=-1;
  188. b.vely/=1.1f;
  189. b.y=maxy;
  190. }
  191. if(b.y<1)
  192. {
  193. b.vely*=-1;
  194. b.vely/=1.1f;
  195. b.y=1;
  196. }
  197. }
  198.  
  199. usleep(RELAY); //300000 -> skrocic
  200. b.vely-=0.2; // obsluga grawitacji
  201. }
  202. pthread_exit(NULL);
  203. }
  204.  
  205. // dodawanie kulek
  206.  
  207. void *addBalls(void *args)
  208. {
  209. int i=0;
  210. if (sleeping==5)
  211. {
  212. sleeping=0;
  213. pthread_cond_broadcast(&cond);
  214. }
  215.  
  216. std::vector<Ball>::iterator it=tab.begin();
  217. while(i<50)
  218. {
  219. int args=i;
  220.  
  221. pthread_create(&threads1[i], NULL, p_work, (void *) args);
  222. i++;
  223. sleep(1);
  224. }
  225. pthread_exit(NULL);
  226. }
  227.  
  228.  
  229.  
  230. int main(void) {
  231. srand(time(NULL));
  232. WINDOW * mainwin;
  233.  
  234. if ( (mainwin = initscr()) == NULL )
  235. {
  236. fprintf(stderr, "Blad. Nie udalo sie zainicjalizowac ncurses .\n");
  237. exit(EXIT_FAILURE);
  238. }
  239. noecho();
  240. cbreak();
  241. nodelay(stdscr, TRUE);
  242. start_color();
  243.  
  244. // w celu rozroznienia kulek zastosowalem kolory, lecz przez ograniczenia biblioteki
  245. // moglem uzyc tylko 6 wariantow (jest jeszcze kolor czarny, ale na czarnym tle go nie zobaczymy)
  246.  
  247. // ewentualnie mozemy uzyc funkcji init_color, aby zmienic podstawowe kolory
  248.  
  249. if ( has_colors()) {
  250.  
  251. init_pair(0, COLOR_RED, COLOR_BLACK);
  252. init_pair(1, COLOR_GREEN, COLOR_BLACK);
  253. init_pair(2, COLOR_BLUE, COLOR_BLACK);
  254. init_pair(3, COLOR_YELLOW, COLOR_BLACK);
  255. init_pair(4, COLOR_MAGENTA, COLOR_BLACK);
  256. init_pair(5, COLOR_CYAN, COLOR_BLACK);
  257. }
  258.  
  259. // obsluga watkow
  260.  
  261. pthread_t a;
  262. pthread_create(&a, NULL, ref, NULL );
  263. pthread_t b;
  264. pthread_create(&b, NULL, addBalls, NULL);
  265.  
  266. pthread_join(a, NULL);
  267.  
  268. // zamkniecie okna
  269. // popraw zamykanie watku
  270. delwin(mainwin);
  271. endwin();
  272. refresh();
  273.  
  274. return EXIT_SUCCESS;
  275. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement