/* Per poter compilare correttamente il programma va aggiunta l'opzione del compilatore -lcurses al fine di linkare la libreria libcurses.a al programma eseguibile. Ad esempio, il comando da shell per la compilazione risulterebbe essere: > gcc.exe -c esempio.c -o pacman -I"C:\Dev-cpp\include" -lcurses */ #include /* ncurses.h includes stdio.h */ #include #include #define PACMANDELAY 5 clock_t delay; /* move delay */ float deltatime() { clock_t now; static float delta=0; static clock_t last = 0; now = clock(); delta += now-last; // if (last != now) // printf("delta %f\n",delta); // if (delta > 0) last = now; if ((100*delta)/CLOCKS_PER_SEC > delay) { delta = 0; return delay; } return 0; } int main() { int row,col; /* to store the number of rows and the number of colums of the screen */ int fine=0; /* to store end-of-game flag */ int x=0; /* to store current horizontal position on the screen */ int y=0; /* to store current vertical position on the screen */ int key; /* to store current key pressed by user */ int lastkey = 0; /* to store last useful key pressed */ float delta; char pacman = '<'; /* Init screen & keyboard input */ initscr(); /* start the curses mode */ clear(); if(has_colors() == FALSE) { endwin(); printf("Your terminal does not support color\n"); fine=1; } start_color(); /* Start color */ init_pair(1, COLOR_RED, COLOR_WHITE); init_pair(2, COLOR_BLACK, COLOR_BLACK); attron(COLOR_PAIR(1)); keypad(stdscr, TRUE); /* Allow control keys to be read */ //ESCDELAY = 20; /* Set delay for ESC reaction (default value is too long), Linux only */ curs_set(0); /* Hide cursor */ noecho(); /* Do not echo pressed key on the screen */ cbreak(); /* Line buffering disabled. pass on everything */ delay = PACMANDELAY; getmaxyx(stdscr,row,col); /* get the number of rows and columns */ /* Main loop: moves the character on the screen */ while (!fine) { delta = deltatime(); /* Delay between two iterations in the game */ if (delta > 0) { timeout(0); key = getch(); /* Try to read a key, do not block (unless "timeout(-1)") */ flushinp(); move(x,y); /* Move cursor to current position on the screen */ addch(pacman | A_BOLD | A_REVERSE); /* Write character to current position on the screen */ refresh(); /* Show effects on schreen */ // mvprintw(row-1,0,"Tasto premuto = %d", key); /* Print a string at a given position on the screen */ if (key != KEY_LEFT && key != KEY_RIGHT && key != KEY_UP && key != KEY_DOWN && key != 27) { // mvprintw(row-1,0,"Tasto premuto = %d ", key); key = lastkey; /* If no useful key has been pressed, use last key pressed instead */ } move(x,y); /* Erase current position */ attron(COLOR_PAIR(2)); addch(' '); /* Erase current position */ attron(COLOR_PAIR(1)); switch(key) { /* Process key pressed */ case 27 : fine = 1; break; case KEY_UP : /* Move character up in the toroidal map */ x=((x+row)-1)%row; pacman = 'V'; lastkey = key; /* Store the key pressed */ break; case KEY_DOWN : /* Move character down in the toroidal map */ x=((x+row)+1)%row; pacman = '^'; lastkey = key; /* Store the key pressed */ break; case KEY_RIGHT : /* Move character right in the toroidal map */ y=((y+col)+1)%col; pacman = '<'; lastkey = key; /* Store the key pressed */ break; case KEY_LEFT : /* Move character left in the toroidal map */ y=((y+col)-1)%col; pacman = '>'; lastkey = key; /* Store the key pressed */ break; } } } attroff(COLOR_PAIR(1)); /* Restore screen colors */ endwin(); /* Close the window */ return 0; }