#include #include #include #include #include #ifdef __linux__ #include #include #include #elif _WIN32 #include #include #endif // #include // #include #ifdef __linux__ #include "kbhit_linux.h" #elif _WIN32 #include #include #endif #define GRID_GROESSE_X 200 #define GRID_GROESSE_Y 60 #define SLEEP_TIME 0 #define RED "\033[31m" #define GREEN "\033[32m" #define YELLOW "\033[33m" #define BLUE "\033[34m" #define MAGENTA "\033[35m" #define CYAN "\033[36m" #define WHITE "\033[37m" const char *colors3[] = {RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE}; #ifdef __linux__ int getch_pipese(){ int ch; struct termios oldattr, newattr; tcgetattr(STDIN_FILENO, &oldattr); newattr = oldattr; newattr.c_lflag &= ~ICANON; newattr.c_lflag &= ~ECHO; newattr.c_cc[VMIN] = 1; newattr.c_cc[VTIME] = 0; tcsetattr(STDIN_FILENO, TCSANOW, &newattr); ch = getchar(); tcsetattr(STDIN_FILENO, TCSANOW, &oldattr); return ch; } #endif int randomee(int max){ return rand() % max; } void print_ate(int x, int y, char c[4], char color) { printf("%s\033[%d;%dH%s", colors3[color], y, x, c); } char print_pipee(int *x, int *y, int x_max, int y_max, int length, char direction, char direction_old, char *color){ if ((direction == 0 && direction_old == 1) || (direction == 1 && direction_old == 0)){ return direction_old; } else if ((direction == 2 && direction_old == 3) || (direction == 3 && direction_old == 2)){ return direction_old; } bool start = true; for(int i = 0; i < length; i++){ if (start && direction != direction_old){ switch (direction_old){ case 0: *y -= 1; if (*y <= 0){ *y = y_max; *color = randomee(7); } break; // up case 1: *y += 1; if (*y > y_max){ *y = 0; *color = randomee(7); } break; // down case 2: *x -= 1; if (*x <= 0){ *x = x_max; *color = randomee(7); } break; // left case 3: *x += 1; if (*x >= x_max){ *x = 0; *color = randomee(7); } break; // rigth } if ((direction == 2 && direction_old == 0) || (direction == 1 && direction_old == 3)){ print_ate(*x, *y, "┓", *color); } else if ((direction == 0 && direction_old == 2) || (direction == 3 && direction_old == 1)) { print_ate(*x, *y, "┗", *color); } else if ((direction == 3 && direction_old == 0) || (direction == 1 && direction_old == 2)) { print_ate(*x, *y, "┏", *color); } else if ((direction == 0 && direction_old == 3) || (direction == 2 && direction_old == 1)) { print_ate(*x, *y, "┛", *color); } // else { printf("\nd = %i | d_o = %i\n", direction, direction_old); } start = false; } else { switch (direction){ case 0: *y -= 1; if (*y <= 0){ *y = y_max; *color = randomee(7); } break; // up case 1: *y += 1; if (*y > y_max){ *y = 0; *color = randomee(7); } break; // down case 2: *x -= 1; if (*x <= 0){ *x = x_max; *color = randomee(7); } break; // left case 3: *x += 1; if (*x >= x_max){ *x = 0; *color = randomee(7); } break; // rigth } if (direction <= 1) { print_ate(*x, *y, "┃", *color); } else { print_ate(*x, *y, "━", *color); } } fflush(stdout); usleep(SLEEP_TIME); } return direction; } int pipes_2_game(int width, int height){ int x_max = width; int y_max =height; int x = width / 2; int y = height / 2; char direction_old = 0; char color = 6; int start_time = 0; srand(time(0)); while(!kbhit()){ int richtung = 0; int length = 4; if (start_time + 20 <= time(0)){ printf("\e[1;1H\e[2J"); // clear terminal start_time = time(0); } #ifdef __linux__ char key_input = (char)getch_pipese(); #elif _WIN32 char key_input = (char)getch(); #endif switch (key_input) { case 'h': richtung = 2; break; case 'a': richtung = 2; break; case 'l': richtung = 3; break; case 'd': richtung = 3; break; case 'j': richtung = 1; break; case 's': richtung = 1; break; case 'k': richtung = 0; break; case 'w': richtung = 0; break; case 'q': return 0; default: continue; } direction_old = print_pipee(&x, &y, x_max, y_max, length, richtung, direction_old, &color); } // ist dazu da um "\n" aus dem buffer zu entfernen! getchar(); printf("\033[00m"); // Farbe zurücksetzen return 0; }