Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <unistd.h>
- #include <pthread.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <ncurses.h>
- #include <time.h>
- struct gracz{
- int x;
- int y;
- int wynik;
- int sk_x;
- int sk_y;
- int dziala;
- };
- struct skarb{
- int x;
- int y;
- };
- struct ogolne{
- struct skarb sk;
- struct gracz gracze[4];
- int znalezione;
- int flag;
- };
- void* draw_treasure(void* arg)
- {
- struct ogolne* og=(struct ogolne*)arg;
- while(og->gracze[0].wynik+og->gracze[1].wynik+og->gracze[2].wynik+og->gracze[3].wynik<=10)
- {
- for(int i=0;i<4;i++)
- {
- if((og->gracze[i].x==og->sk.x&&og->gracze[i].y==og->sk.y))
- {
- og->gracze[i].wynik++;
- srand(time(NULL));
- og->sk.x=rand()%COLS;
- og->sk.y=rand()%(LINES-6);
- for(int k=0;k<4;k++)
- {
- og->gracze[i].sk_x=og->sk.x;
- og->gracze[i].sk_y=og->sk.y;
- }
- }
- }
- usleep(15000);
- }
- for(int i=0;i<4;i++)
- og->gracze[i].dziala=0;
- return NULL;
- }
- void* move_player(void*arg) {
- //pthread_mutex_lock(&nc);
- struct gracz* gr=(struct gracz*)arg;
- while(gr->dziala)
- {
- if(gr->x>gr->sk_x)
- gr->x--;
- else if(gr->x<gr->sk_x)
- gr->x++;
- if(gr->y>gr->sk_y)
- gr->y--;
- else if(gr->y<gr->sk_y)
- gr->y++;
- usleep(100000);
- }
- return NULL;
- }
- void* display(void* arg)
- {
- struct ogolne* og=(struct ogolne*)arg;
- int xp[4];
- int yp[4];
- for(int i=0;i<4;i++)
- {
- mvprintw(og->gracze[i].y,og->gracze[i].x,"%c",'x');
- xp[i]=og->gracze[i].x;
- yp[i]=og->gracze[i].y;
- }
- while(og->gracze[0].wynik+og->gracze[1].wynik+og->gracze[2].wynik+og->gracze[3].wynik<=10)
- {
- refresh();
- for(int i=0;i<4;i++)
- {
- mvprintw(yp[i],xp[i],"%c",' ');
- }
- mvprintw(og->sk.y,og->sk.x,"%c",'o');
- for(int i=0;i<4;i++)
- {
- mvprintw(og->gracze[i].y,og->gracze[i].x,"%c",'x');
- xp[i]=og->gracze[i].x;
- yp[i]=og->gracze[i].y;
- }
- for(int i=0;i<4;i++)
- {
- move(LINES-4+i,0);
- printw("Player %d: %d", i+1,og->gracze[i].wynik);
- }
- usleep(15000);
- }
- return NULL;
- }
- int main() {
- initscr();
- cbreak();
- noecho();
- curs_set(0);
- pthread_t gra[4];
- pthread_t skarb;
- pthread_t disp;
- srand(time(NULL));
- struct ogolne og;
- og.sk.x=rand()%COLS;
- og.sk.y=rand()%(LINES-6);
- og.flag=1;
- for(int i=0;i<4;i++)
- {
- og.gracze[i].x=rand()%COLS;
- og.gracze[i].y=rand()%(LINES-6);
- og.gracze[i].sk_x=og.sk.x;
- og.gracze[i].sk_y=og.sk.y;
- og.gracze[i].wynik=0;
- og.gracze[i].dziala=1;
- }
- for(int i=0;i<4;i++)
- {
- pthread_create(&gra[i],NULL,move_player,(void*)&og.gracze[i]);
- }
- pthread_create(&disp,NULL,display,(void*)&og);
- pthread_create(&skarb,NULL,draw_treasure,(void*)&og);
- pthread_join(skarb,NULL);
- for(int i=0;i<4;i++)
- pthread_join(gra[i],NULL);
- pthread_join(disp,NULL);
- //getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement