Merge pull request 'pipes2game' (#7) from pipes2game into main

Reviewed-on: #7
pull/8/head
Noah Balsiger 2024-12-16 11:31:08 +01:00
commit be021d315f
3 changed files with 172 additions and 10 deletions

158
src/pipes2_game.c Normal file
View File

@ -0,0 +1,158 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <time.h>
#ifdef __linux__
#include <sys/ioctl.h>
#include <unistd.h>
#include <termio.h>
#elif _WIN32
#include <windows.h>
#include <conio.h>
#endif
// #include <termios.h>
// #include <fcntl.h>
#ifdef __linux__
#include "kbhit_linux.h"
#elif _WIN32
#include <windows.h>
#include <conio.h>
#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;
}

10
src/pipes2_game.h Normal file
View File

@ -0,0 +1,10 @@
#ifndef pipes2_game_H_
#define pipes2_game_H_
// #ifndef _FILE_NAME_H_
// #define _FILE_NAME_H_
// extern int pipes(int width, int height);
int pipes_2_game(int width, int height);
#endif

View File

@ -184,16 +184,10 @@ int print_grid(int x, int y, char **grid, int how_many_targets, int *lul) {
printf(" █ █████ █████ █ █ █████ █ ██\n"); printf(" █ █████ █████ █ █ █████ █ ██\n");
printf("\n"); printf("\n");
printf("Targets overritten: %i\n", 2*how_many_targets-target_count); printf("Targets overritten: %i\n", 2*how_many_targets-target_count);
if (target_count > 3) { *lul += 1;
printf("\nspecial achivement!!!!!!\n"); system("xdg-open https://bit.ly/3BlS71b");
printf("'q' for start menue"); printf("'q' for start menue");
return 1; return 1;
} else {
*lul += 1;
system("xdg-open https://bit.ly/3BlS71b");
printf("'q' for start menue");
return 1;
}
} }
return 0; return 0;