Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <unistd.h> /* for sleep() */
  4. #include <curses.h>
  5. #include <time.h>
  6. #include <thread>
  7. #include <string>
  8. #include <vector>
  9. #include <math.h>
  10. using namespace std;
  11.  
  12. bool ramka=true;
  13. pthread_t threads1[100];
  14. //v=s/t s=vt t=s/v
  15. class Ball
  16. {
  17. public:
  18. double x;
  19. double y;
  20. double velx;
  21. double vely;
  22. int time;
  23. Ball()
  24. {
  25. x=40;
  26. y=20;
  27. velx=(rand()%5) - 2;
  28. vely=((rand()%3)-2);
  29. if(velx==0 && vely==0) vely=-1;
  30. time = 50000;
  31. }
  32. void Wyp();
  33. };
  34. void Ball::Wyp()
  35. {
  36. mvprintw(round(y), round(x), "o");
  37.  
  38.  
  39.  
  40.  
  41. }
  42. vector <Ball> tab(100);
  43. void *ref(void *args)
  44. {
  45. while(true)
  46. {
  47. clear();
  48. for(Ball &b : tab)
  49. {
  50. b.Wyp();
  51.  
  52. }
  53. if(ramka)
  54. {
  55. for(int i=1;i<41;i++)
  56. {
  57. mvprintw(i, 0, "|");
  58. mvprintw(i, 80, "|");
  59. }
  60. for(int i=1;i<81;i++)
  61. {
  62.  
  63. mvprintw(0, i, "_");
  64. mvprintw(40, i, "_");
  65. }
  66. }
  67. refresh();
  68. usleep(30000);
  69. }
  70. }
  71. void *p_work(void *args){
  72.  
  73. int k=(int)args;
  74. Ball& b = tab.at(k);
  75. int maxx = 79;
  76. int maxy = 39;
  77. while(true)
  78. {
  79. b.x+=b.velx;
  80. b.y-=b.vely;
  81.  
  82. if(b.x>=maxx)
  83. {
  84. b.velx=b.velx*(-1);
  85. b.x+=b.velx;
  86. }
  87. if(b.x<=1)
  88. {
  89. b.velx=b.velx*(-1);
  90. b.x+=b.velx;
  91. }
  92. if(b.y>maxy)
  93. {
  94. b.vely=b.vely*(-1);
  95. b.y=maxy;
  96. }
  97. if(b.y<1)
  98. {
  99. b.vely=b.vely*(-1);
  100. b.y=1;
  101. }
  102.  
  103.  
  104. usleep(300000);//sqrt(pow(b.velx,2)+pow(b.vely,2)));
  105. b.vely-=0.2;
  106. }
  107.  
  108. }
  109.  
  110.  
  111. void *addBalls(void *args)
  112. {
  113. while(true)
  114. {
  115.  
  116. //sleep(3);
  117. Ball a;
  118. tab.push_back(a);
  119. pthread_t t;
  120. pthread_create(&t,NULL, p_work,&a);
  121. //threads.push_back(t);
  122. sleep(1);
  123. }
  124. }
  125.  
  126. int main(void) {
  127. srand(time(NULL));
  128.  
  129.  
  130. WINDOW * mainwin;
  131.  
  132.  
  133. /* Initialize ncurses */
  134.  
  135. if ( (mainwin = initscr()) == NULL ) {
  136. fprintf(stderr, "Error initialising ncurses.\n");
  137. exit(EXIT_FAILURE);
  138. }
  139.  
  140. start_color(); /* Initialize colours */
  141.  
  142.  
  143.  
  144. if ( has_colors() && COLOR_PAIRS >= 13 ) {
  145.  
  146. int n = 1;
  147.  
  148. init_pair(1, COLOR_RED, COLOR_BLACK);
  149. init_pair(2, COLOR_GREEN, COLOR_BLACK);
  150. init_pair(3, COLOR_YELLOW, COLOR_BLACK);
  151. init_pair(4, COLOR_BLUE, COLOR_BLACK);
  152. init_pair(5, COLOR_MAGENTA, COLOR_BLACK);
  153. init_pair(6, COLOR_CYAN, COLOR_BLACK);
  154. init_pair(7, COLOR_BLUE, COLOR_WHITE);
  155. init_pair(8, COLOR_WHITE, COLOR_RED);
  156. init_pair(9, COLOR_BLACK, COLOR_GREEN);
  157. init_pair(10, COLOR_BLUE, COLOR_YELLOW);
  158. init_pair(11, COLOR_WHITE, COLOR_BLUE);
  159. init_pair(12, COLOR_WHITE, COLOR_MAGENTA);
  160. init_pair(13, COLOR_BLACK, COLOR_CYAN);
  161. }
  162.  
  163. pthread_t r;
  164.  
  165. pthread_create(&r, NULL, ref, NULL );
  166. pthread_t ab;
  167. // while(true)
  168. //{
  169. for (int i=0; i<100;i++)
  170. {
  171. Ball a;
  172. tab.push_back(a);
  173. int args=i;
  174. pthread_create(&threads1[i], NULL, p_work, (void *) args);
  175. sleep(3);
  176. }
  177. //pthread_join(r, NULL);
  178.  
  179. //sleep(1);
  180. //}
  181. while(true) sleep(1);
  182. /* Clean up */
  183.  
  184. delwin(mainwin);
  185. endwin();
  186. refresh();
  187.  
  188. return EXIT_SUCCESS;
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement