#include #include #include #include #include // #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 40000 #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 *colors2[] = {RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE}; int randome(int max){ return rand() % max; } void print_at(int x, int y, char c[4], char color) { printf("%s\033[%d;%dH%s", colors2[color], y, x, c); } char print_pipe(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 = randome(7); } break; // up case 1: *y += 1; if (*y > y_max){ *y = 0; *color = randome(7); } break; // down case 2: *x -= 1; if (*x <= 0){ *x = x_max; *color = randome(7); } break; // left case 3: *x += 1; if (*x >= x_max){ *x = 0; *color = randome(7); } break; // rigth } if ((direction == 2 && direction_old == 0) || (direction == 1 && direction_old == 3)){ print_at(*x, *y, "┓", *color); } else if ((direction == 0 && direction_old == 2) || (direction == 3 && direction_old == 1)) { print_at(*x, *y, "┗", *color); } else if ((direction == 3 && direction_old == 0) || (direction == 1 && direction_old == 2)) { print_at(*x, *y, "┏", *color); } else if ((direction == 0 && direction_old == 3) || (direction == 2 && direction_old == 1)) { print_at(*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 = randome(7); } break; // up case 1: *y += 1; if (*y > y_max){ *y = 0; *color = randome(7); } break; // down case 2: *x -= 1; if (*x <= 0){ *x = x_max; *color = randome(7); } break; // left case 3: *x += 1; if (*x >= x_max){ *x = 0; *color = randome(7); } break; // rigth } if (direction <= 1) { print_at(*x, *y, "┃", *color); } else { print_at(*x, *y, "━", *color); } } fflush(stdout); usleep(SLEEP_TIME); } return direction; } int pipes_2(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()){ if (start_time + 20 <= time(0)){ printf("\e[1;1H\e[2J"); // clear terminal start_time = time(0); } direction_old = print_pipe(&x, &y, x_max, y_max, randome(height / 3) + 1, randome(4), direction_old, &color); } // ist dazu da um "\n" aus dem buffer zu entfernen! getchar(); printf("\033[00m"); // Farbe zurücksetzen return 0; }