Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <curses.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <pthread.h>
- #include <time.h>
- #define SOPRA 65
- #define SOTTO 66
- #define SINISTRA 68
- #define DESTRA 67
- #define MAXX 80
- #define MAXY 24
- #define DELAY 50000
- struct position {
- int x;
- int y;
- };
- struct position nemico1;
- struct position nemico2;
- struct position amico;
- struct position balloon;
- int collision;
- pthread_mutex_t mtx;
- void AreaGioco(){
- int i=0;
- int scudi=3;
- while (collision==1){
- pthread_mutex_lock(&mtx);
- mvprintw(0,0,"SCUDI %d", scudi);
- pthread_mutex_unlock(&mtx);
- if (!(i++%100)){
- pthread_mutex_lock(&mtx);
- mvaddch(balloon.y,balloon.x,' ');
- balloon.x = random()%MAXX;
- balloon.y = random()%MAXY;
- mvaddch(balloon.y,balloon.x,'O');
- refresh();
- pthread_mutex_unlock(&mtx);
- }
- if (amico.x == balloon.x && amico.y == balloon.y){
- if (scudi<6){
- scudi++;
- }
- pthread_mutex_lock(&mtx);
- balloon.x = random()%MAXX;
- balloon.y = random()%MAXY;
- pthread_mutex_unlock(&mtx);
- }
- if (((amico.x == nemico1.x && amico.y == nemico1.y) || (amico.x == nemico1.x && amico.y == nemico1.y))||
- ((amico.x == nemico2.x && amico.y == nemico2.y) || (amico.x == nemico2.x && amico.y == nemico2.y))){
- if (scudi>0){
- scudi--;
- } else {
- pthread_mutex_lock(&mtx);
- collision=0;
- pthread_mutex_unlock(&mtx);
- }
- }
- pthread_mutex_lock(&mtx);
- refresh();
- pthread_mutex_unlock(&mtx);
- usleep(DELAY);
- }
- }
- void *aereonemico1(){
- int rand_Y;
- int i=0;
- //struct position old1;
- nemico1.x = 0;
- srand(time(NULL));
- while (collision=1){
- nemico1.y = 1+random()%MAXY-1;
- for (i=1;i<MAXX+1;i++){
- pthread_mutex_lock(&mtx);
- mvaddch(nemico1.y,nemico1.x,' ');
- pthread_mutex_unlock(&mtx);
- nemico1.x=i;
- pthread_mutex_lock(&mtx);
- mvaddch(nemico1.y,nemico1.x,'+');
- refresh();
- pthread_mutex_unlock(&mtx);
- usleep(DELAY);
- }
- pthread_mutex_lock(&mtx);
- mvaddch(nemico1.y,nemico1.x,' ');
- pthread_mutex_unlock(&mtx);
- }
- }
- void *aereonemico2(){
- int rand_Y;
- int i=0;
- //struct position old1;
- nemico2.y = 0;
- srand(time(NULL));
- while (collision=1){
- nemico2.x = 1+random()%MAXX-1;
- for (i=1;i<MAXY+1;i++){
- pthread_mutex_lock(&mtx);
- mvaddch(nemico2.y,nemico2.x,' ');
- pthread_mutex_unlock(&mtx);
- nemico2.y=i;
- pthread_mutex_lock(&mtx);
- mvaddch(nemico2.y,nemico2.x,'X');
- refresh();
- pthread_mutex_unlock(&mtx);
- usleep(DELAY);
- }
- pthread_mutex_lock(&mtx);
- mvaddch(nemico2.y,nemico2.x,' ');
- pthread_mutex_unlock(&mtx);
- }
- }
- void *aereoamico(){
- char key;
- amico.x = MAXX/2;
- amico.y = MAXY/2;
- pthread_mutex_lock(&mtx);
- mvaddch(amico.y,amico.x,'#');
- refresh();
- pthread_mutex_unlock(&mtx);
- while (collision=1){
- key = getch();
- pthread_mutex_lock(&mtx);
- mvaddch(amico.y,amico.x,' ');
- pthread_mutex_unlock(&mtx);
- pthread_mutex_lock(&mtx);
- switch(key){
- case SOPRA:
- if (amico.y>0)
- amico.y--;
- //mvaddch(amico.y,amico.x,'#');
- break;
- case SOTTO:
- if (amico.y<MAXY)
- amico.y++;
- //mvaddch(amico.y,amico.x,'#');
- break;
- case SINISTRA:
- if (amico.x>0)
- amico.x--;
- //mvaddch(amico.y,amico.x,'#');
- break;
- case DESTRA:
- if (amico.x<MAXX-1)
- amico.x++;
- //mvaddch(amico.y,amico.x,'#');
- break;
- default:
- break;
- }
- mvaddch(amico.y,amico.x,'#');
- refresh();
- pthread_mutex_unlock(&mtx);
- }
- }
- int main (){
- pthread_t nemico1;
- pthread_t nemico2;
- pthread_t amico;
- pthread_mutex_init(&mtx, NULL);
- initscr();
- noecho();
- curs_set(0);
- collision=1;
- pthread_create(&nemico1, NULL, aereonemico1, NULL);
- pthread_create(&nemico2, NULL, aereonemico2, NULL);
- pthread_create(&amico, NULL, aereoamico, NULL);
- AreaGioco();
- pthread_join(nemico1,NULL);
- pthread_join(nemico2,NULL);
- pthread_join(amico,NULL);
- pthread_mutex_destroy(&mtx);
- endwin();
- printf("-- GAME OVER --");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement