Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <unistd.h> /* for sleep() */
- #include <curses.h>
- #include <time.h>
- #include <thread>
- #include <string>
- #include <vector>
- #include <math.h>
- using namespace std;
- bool ramka=true;
- pthread_t threads1[100];
- //v=s/t s=vt t=s/v
- class Ball
- {
- public:
- double x;
- double y;
- double velx;
- double vely;
- int time;
- Ball()
- {
- x=40;
- y=20;
- velx=(rand()%5) - 2;
- vely=((rand()%3)-2);
- if(velx==0 && vely==0) vely=-1;
- time = 50000;
- }
- void Wyp();
- };
- void Ball::Wyp()
- {
- mvprintw(round(y), round(x), "o");
- }
- vector <Ball> tab(100);
- void *ref(void *args)
- {
- while(true)
- {
- clear();
- for(Ball &b : tab)
- {
- b.Wyp();
- }
- if(ramka)
- {
- for(int i=1;i<41;i++)
- {
- mvprintw(i, 0, "|");
- mvprintw(i, 80, "|");
- }
- for(int i=1;i<81;i++)
- {
- mvprintw(0, i, "_");
- mvprintw(40, i, "_");
- }
- }
- refresh();
- usleep(30000);
- }
- }
- void *p_work(void *args){
- int k=(int)args;
- Ball& b = tab.at(k);
- int maxx = 79;
- int maxy = 39;
- while(true)
- {
- b.x+=b.velx;
- b.y-=b.vely;
- if(b.x>=maxx)
- {
- b.velx=b.velx*(-1);
- b.x+=b.velx;
- }
- if(b.x<=1)
- {
- b.velx=b.velx*(-1);
- b.x+=b.velx;
- }
- if(b.y>maxy)
- {
- b.vely=b.vely*(-1);
- b.y=maxy;
- }
- if(b.y<1)
- {
- b.vely=b.vely*(-1);
- b.y=1;
- }
- usleep(300000);//sqrt(pow(b.velx,2)+pow(b.vely,2)));
- b.vely-=0.2;
- }
- }
- void *addBalls(void *args)
- {
- while(true)
- {
- //sleep(3);
- Ball a;
- tab.push_back(a);
- pthread_t t;
- pthread_create(&t,NULL, p_work,&a);
- //threads.push_back(t);
- sleep(1);
- }
- }
- int main(void) {
- srand(time(NULL));
- WINDOW * mainwin;
- /* Initialize ncurses */
- if ( (mainwin = initscr()) == NULL ) {
- fprintf(stderr, "Error initialising ncurses.\n");
- exit(EXIT_FAILURE);
- }
- start_color(); /* Initialize colours */
- if ( has_colors() && COLOR_PAIRS >= 13 ) {
- int n = 1;
- init_pair(1, COLOR_RED, COLOR_BLACK);
- init_pair(2, COLOR_GREEN, COLOR_BLACK);
- init_pair(3, COLOR_YELLOW, COLOR_BLACK);
- init_pair(4, COLOR_BLUE, COLOR_BLACK);
- init_pair(5, COLOR_MAGENTA, COLOR_BLACK);
- init_pair(6, COLOR_CYAN, COLOR_BLACK);
- init_pair(7, COLOR_BLUE, COLOR_WHITE);
- init_pair(8, COLOR_WHITE, COLOR_RED);
- init_pair(9, COLOR_BLACK, COLOR_GREEN);
- init_pair(10, COLOR_BLUE, COLOR_YELLOW);
- init_pair(11, COLOR_WHITE, COLOR_BLUE);
- init_pair(12, COLOR_WHITE, COLOR_MAGENTA);
- init_pair(13, COLOR_BLACK, COLOR_CYAN);
- }
- pthread_t r;
- pthread_create(&r, NULL, ref, NULL );
- pthread_t ab;
- // while(true)
- //{
- for (int i=0; i<100;i++)
- {
- Ball a;
- tab.push_back(a);
- int args=i;
- pthread_create(&threads1[i], NULL, p_work, (void *) args);
- sleep(3);
- }
- //pthread_join(r, NULL);
- //sleep(1);
- //}
- while(true) sleep(1);
- /* Clean up */
- delwin(mainwin);
- endwin();
- refresh();
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement