add colors

pipes2game
MuedeHydra 2024-12-15 19:45:43 +01:00
parent 8373a04747
commit c09970c916
1 changed files with 46 additions and 33 deletions

View File

@ -16,46 +16,64 @@
#define GRID_GROESSE_X 200 #define GRID_GROESSE_X 200
#define GRID_GROESSE_Y 60 #define GRID_GROESSE_Y 60
#define SLEEP_TIME 10000 #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"
void print_at(int x, int y, char c[4]) const char *colors2[] = {RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE};
{
printf("\033[%d;%dH%s", y, x, c); int randome(int max){
return rand() % max;
} }
char print_pipe(int *x, int *y, int x_max, int y_max, int length, char direction, char direction_old){ 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; bool start = true;
for(int i = 0; i < length; i++){ for(int i = 0; i < length; i++){
if (start && direction != direction_old){ if (start && direction != direction_old){
switch (direction_old){ switch (direction_old){
case 0: *y -= 1; if (*y <= 0){ *y = y_max; } break; // up 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; } break; // down 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; } break; // left 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; } break; // rigth 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){ if ((direction == 2 && direction_old == 0) || (direction == 1 && direction_old == 3)){
print_at(*x, *y, ""); print_at(*x, *y, "", *color);
} else if (direction == 0 && direction_old == 2 || direction == 3 && direction_old == 1) { } else if ((direction == 0 && direction_old == 2) || (direction == 3 && direction_old == 1)) {
print_at(*x, *y, ""); print_at(*x, *y, "", *color);
} else if (direction == 3 && direction_old == 0 || direction == 1 && direction_old == 2) { } else if ((direction == 3 && direction_old == 0) || (direction == 1 && direction_old == 2)) {
print_at(*x, *y, ""); print_at(*x, *y, "", *color);
} else if (direction == 0 && direction_old == 3 || direction == 2 && direction_old == 1) { } else if ((direction == 0 && direction_old == 3) || (direction == 2 && direction_old == 1)) {
print_at(*x, *y, ""); print_at(*x, *y, "", *color);
} // else { printf("\nd = %i | d_o = %i\n", direction, direction_old); } } // else { printf("\nd = %i | d_o = %i\n", direction, direction_old); }
start = false; start = false;
} else { } else {
switch (direction){ switch (direction){
case 0: *y -= 1; if (*y <= 0){ *y = y_max; } break; // up 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; } break; // down 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; } break; // left 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; } break; // rigth case 3: *x += 1; if (*x >= x_max){ *x = 0; *color = randome(7); } break; // rigth
} }
if (direction <= 1) { if (direction <= 1) {
print_at(*x, *y, ""); print_at(*x, *y, "", *color);
} else { } else {
print_at(*x, *y, ""); print_at(*x, *y, "", *color);
} }
} }
fflush(stdout); fflush(stdout);
@ -64,33 +82,28 @@ char print_pipe(int *x, int *y, int x_max, int y_max, int length, char direction
return direction; return direction;
} }
int randome(int max){
return rand() % max;
}
void clear_terminal2(){
printf("\e[1;1H\e[2J");
}
int pipes_2(int width, int height){ int pipes_2(int width, int height){
int x_max = width; int x_max = width;
int y_max =height; int y_max =height;
int x = width / 2; int x = width / 2;
int y = height / 2; int y = height / 2;
char direction_old = 0; char direction_old = 0;
char color = 6;
int start_time = 0; int start_time = 0;
srand(time(0)); srand(time(0));
while(!kbhit()){ while(!kbhit()){
if (start_time + 20 <= time(0)){ if (start_time + 20 <= time(0)){
clear_terminal2(); printf("\e[1;1H\e[2J"); // clear terminal
start_time = time(0); start_time = time(0);
} }
direction_old = print_pipe(&x, &y, x_max, y_max, randome(height / 2), randome(4), direction_old); 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! // ist dazu da um "\n" aus dem buffer zu entfernen!
getchar(); getchar();
printf("\033[00m"); // Farbe zurücksetzen
return 0; return 0;
} }