Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <unistd.h>
- #include <pthread.h>
- #include <ncurses.h>
- #define UP 65 /* Cursore sopra */
- #define DW 66 /* Cursore sotto */
- #define SX 68 /* Cursore sinistra */
- #define DX 67 /* Cursore destra */
- #define MAXX 80 /* Dimensione dello schermo di output (colonne) */
- #define MAXY 20 /* Dimensione dello schermo di output (righe) */
- #define DELAY 50000 /* Ritardo nel movimento degli aerei nemici (da adattare) */
- /* Prototipi delle funzioni adoperate */
- void* Nemico(void* args);
- void* Amico(void* args);
- void Area(void);
- int collision;
- pthread_mutex_t muta=PTHREAD_MUTEX_INITIALIZER;
- struct position {
- int x; /* Coordinata X */
- int y; /* Coordinata Y */
- };
- struct position nemico;
- struct position amico;
- int main() {
- pthread_t thread_nemico;
- pthread_t thread_amico;
- srand(time(NULL)); /* Inizializza generatore di numeri casuali */
- nemico.x=0;
- nemico.y=0;
- amico.x = MAXX/2;
- amico.y = MAXY/2;
- initscr(); /* Inizializza schermo di output */
- noecho(); /* Imposta modalità della tastiera */
- curs_set(0); /* Nasconde il cursore */
- pthread_create (&thread_nemico, NULL, &Nemico, NULL);
- pthread_create (&thread_amico, NULL, &Amico, NULL);
- Area();
- endwin(); /* Ripristino la modalità di funzionamento usuale */
- printf("\n\n\nGAME OVER\n\n\n"); /* Termino il gioco ed esco dal programma */
- //pthread_join(thread_nemico, NULL);
- //pthread_join(thread_amico, NULL);
- return 0;
- }
- void* Nemico(void* args) {
- int YX=0; /* Variabile per direzione movimento aereo */
- struct position prev;
- prev.x = 1;
- prev.y = 1;
- while(1) {
- if(random()<RAND_MAX/2)
- YX=0;
- else
- YX=1;
- if(YX==0) {
- nemico.y = random()%MAXY;
- for(nemico.x=2;nemico.x<MAXX;nemico.x++) { /* Effettuo il percorso da x=0 a x=MAXX */
- pthread_mutex_lock(&muta);
- mvaddch(prev.y, prev.x, ' ');
- mvaddch(nemico.y, nemico.x, '+');
- refresh();
- pthread_mutex_unlock(&muta);
- prev.x=nemico.x;
- prev.y=nemico.y;
- usleep(DELAY); /* Inserisco una pausa per rallentare il movimento */
- }
- }
- else {
- nemico.x = random()%MAXX; /* Genero casualmente una posizione x di partenza */
- for(nemico.y=2;nemico.y<MAXY;nemico.y++) { /* Effettuo il percorso da y=0 a y=MAXY */
- pthread_mutex_lock(&muta);
- mvaddch(prev.y, prev.x, ' ');
- mvaddch(nemico.y, nemico.x, '+');
- refresh();
- pthread_mutex_unlock(&muta);
- prev.x=nemico.x;
- prev.y=nemico.y;
- usleep(DELAY); /* Inserisco una pausa per rallentare il movimento */
- }
- }
- }
- }
- /*
- ----------------------------------------------------------------------
- Funzione 'Aereo Amico' - Movimento tramite i tasti cursore
- ----------------------------------------------------------------------
- */
- void* Amico(void* args) {
- char c;
- struct position prev;
- prev.x = amico.x;
- prev.y = amico.y;
- pthread_mutex_lock(&muta);
- mvaddch(amico.y, amico.x, '#');
- refresh();
- pthread_mutex_unlock(&muta);
- while(1) { /* Lettura dei tasti cursore */
- c = getch();
- pthread_mutex_lock(&muta);
- if(c==UP && amico.y > 0)
- amico.y-=1;
- if(c==DW && amico.y < MAXY-1)
- amico.y+=1;
- if(c==SX && amico.x > 0)
- amico.x-=1;
- if(c==DX && amico.x < MAXX-1)
- amico.x+=1;
- pthread_mutex_unlock(&muta);
- pthread_mutex_lock(&muta);
- mvaddch(prev.y, prev.x, ' ');
- mvaddch(amico.y, amico.x, '#');
- refresh();
- pthread_mutex_unlock(&muta);
- prev.y=amico.y;
- prev.x=amico.x;
- }
- }
- void Area(void) {
- struct position bonus1;
- struct position bonus2;
- struct position bonus3;
- int i=0;
- int collision=0;
- int max_collision = 3;
- do {
- mvprintw(0,1,"SCUDO %3d", max_collision);
- if(!(i++%100)) {
- pthread_mutex_lock(&muta);
- mvaddch(bonus1.y, bonus1.x, ' ');
- mvaddch(bonus2.y, bonus2.x, ' ');
- mvaddch(bonus3.y, bonus3.x, ' ');
- pthread_mutex_unlock(&muta);
- bonus1.x = rand()%MAXX;
- bonus1.y = rand()%MAXY;
- bonus2.x = rand()%MAXX;
- bonus2.y = rand()%MAXY;
- bonus3.x = rand()%MAXX;
- bonus3.y = rand()%MAXY;
- pthread_mutex_lock(&muta);
- mvaddch(bonus1.y,bonus1.x,'O');
- mvaddch(bonus2.y,bonus2.x,'O');
- mvaddch(bonus3.y,bonus3.x,'O');
- refresh();
- pthread_mutex_unlock(&muta);
- }
- pthread_mutex_lock(&muta);
- if ((nemico.x == amico.x) && (nemico.y == amico.y)){
- max_collision--;
- if (max_collision < 0){
- collision=1;
- }
- }
- pthread_mutex_unlock(&muta);
- pthread_mutex_lock(&muta);
- if ((nemico.x == bonus1.x && nemico.y == bonus1.y)||
- (nemico.x == bonus2.x && nemico.y == bonus2.y)||
- (nemico.x == bonus3.x && nemico.y == bonus3.y)) {
- max_collision = 3;
- collision = 0;
- }
- pthread_mutex_unlock(&muta);
- usleep(DELAY);
- } while(!collision);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement