From 42fe495bb67854732ec98e4a82cd80c7252943b7 Mon Sep 17 00:00:00 2001 From: David Moeller Date: Sun, 21 Jan 2024 12:21:17 +0100 Subject: [PATCH 01/22] Snake rough structure --- src/main/c/Snake/snake_start.c | 28 ++++++++++++++++++++++++++++ src/main/c/Snake/snake_start.h | 0 src/main/c/main.c | 5 +++-- 3 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 src/main/c/Snake/snake_start.c create mode 100644 src/main/c/Snake/snake_start.h diff --git a/src/main/c/Snake/snake_start.c b/src/main/c/Snake/snake_start.c new file mode 100644 index 0000000..186f773 --- /dev/null +++ b/src/main/c/Snake/snake_start.c @@ -0,0 +1,28 @@ +#include + + +#define HEIGHT 16 +#define WIDTH 16 +#define AREA HEIGHT * WIDTH + + +typedef struct +{ + char direction; + char segments[AREA]; +}Snake; + + +void main_menu(); + + +void snake_start(){ + system("clear"); + printf("Playing Snake"); + getchar(); + main_menu(); +} + +void main_menu(){ + +} \ No newline at end of file diff --git a/src/main/c/Snake/snake_start.h b/src/main/c/Snake/snake_start.h new file mode 100644 index 0000000..e69de29 diff --git a/src/main/c/main.c b/src/main/c/main.c index eaf0b17..27cfcae 100644 --- a/src/main/c/main.c +++ b/src/main/c/main.c @@ -3,6 +3,7 @@ #include #include "Template/game100.h" +#include "Snake/snake_start.h" int main(){ bool running = true; @@ -13,7 +14,7 @@ int main(){ system("clear"); printf("Waehlen Sie eine Option:\n"); - printf("\t1.Spiel1 starten\n"); + printf("\t1.Snake starten\n"); printf("\t2.Spiel2 starten\n"); printf("\t3.Spiel3 starten\n"); printf("\t4.Spiel4 starten\n"); @@ -25,7 +26,7 @@ int main(){ switch (option){ case 1: - //start_game1(); + snake_start(); break; case 2: //start_game2(); From 37820a27b28cef00a97f02420205cda31c8ccda6 Mon Sep 17 00:00:00 2001 From: David Moeller Date: Sun, 21 Jan 2024 13:03:36 +0100 Subject: [PATCH 02/22] Initializing Game --- src/main/c/Snake/snake_start.c | 77 ++++++++++++++++++++++++++++++---- 1 file changed, 69 insertions(+), 8 deletions(-) diff --git a/src/main/c/Snake/snake_start.c b/src/main/c/Snake/snake_start.c index 186f773..4e21045 100644 --- a/src/main/c/Snake/snake_start.c +++ b/src/main/c/Snake/snake_start.c @@ -1,28 +1,89 @@ #include +#include -#define HEIGHT 16 -#define WIDTH 16 +#define HEIGHT 15 +#define WIDTH 15 #define AREA HEIGHT * WIDTH - -typedef struct -{ - char direction; +typedef struct{ + signed char direction[2]; + char length; char segments[AREA]; }Snake; void main_menu(); +void game(); +Snake initialize_game(); +void draw(Snake *snake); void snake_start(){ system("clear"); - printf("Playing Snake"); - getchar(); main_menu(); } void main_menu(){ + bool running = true; + while (running){ + int option = 0; + + system("clear"); + printf("Waehlen Sie eine Option:\n"); + printf("\t1.Start\n"); + printf("\t2.Exit\n"); + + scanf("%d", &option); + getchar(); + + system("clear"); + + switch (option){ + case 1: + game(); + break; + case 2: + running = false; + break; + + default: + break; + } + } +} + +void game(){ + Snake snake = initialize_game(); + bool running = true; + while (running) + { + draw(&snake); + } + +} + +Snake initialize_game(){ + Snake snake = {{0, 1}, 3}; + for(int i = 0; i < AREA; i++){ + snake.segments[i] = 0; + } + snake.segments[0] = 8 + 16 * 8; + snake.segments[1] = 7 + 16 * 8; + snake.segments[2] = 6 + 16 * 8; + return snake; +} + +void draw(Snake *snake){ + printf("+"); + for(int i = 0; i < WIDTH; i++){printf("-");} + printf("+\n"); + for(int i = 0; i < HEIGHT; i++){ + for(int j = 0; j < WIDTH; j++){ + } + } + printf("+"); + for(int i = 0; i < WIDTH; i++){printf("_");} + printf("\n"); } \ No newline at end of file From 189eb9570481baba1b7013139167647bc854634d Mon Sep 17 00:00:00 2001 From: David Moeller Date: Sun, 21 Jan 2024 13:22:57 +0100 Subject: [PATCH 03/22] draw Funktion --- src/main/c/Snake/snake_start.c | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/src/main/c/Snake/snake_start.c b/src/main/c/Snake/snake_start.c index 4e21045..57d332f 100644 --- a/src/main/c/Snake/snake_start.c +++ b/src/main/c/Snake/snake_start.c @@ -9,7 +9,7 @@ typedef struct{ signed char direction[2]; char length; - char segments[AREA]; + unsigned char segments[AREA]; }Snake; @@ -58,9 +58,11 @@ void game(){ bool running = true; while (running) { + system("clear"); draw(&snake); + running = false; } - + getchar(); } Snake initialize_game(){ @@ -78,12 +80,27 @@ void draw(Snake *snake){ printf("+"); for(int i = 0; i < WIDTH; i++){printf("-");} printf("+\n"); - for(int i = 0; i < HEIGHT; i++){ - for(int j = 0; j < WIDTH; j++){ - + for(int i = 1; i <= HEIGHT; i++){ + printf("|"); + for(int j = 1; j <= WIDTH; j++){ + //printf("%d:", i * 16 + j); + for(int pos = 0; pos < snake->length; pos++){ + //printf(" %d", snake->segments[pos]); + if(i * 16 + j == snake->segments[pos]){ + if(pos == 0){ + printf("0"); + }else{ + printf("O"); + } + break; + }else if(pos == snake->length - 1){ + printf(" "); + } + } } + printf("|\n"); } printf("+"); - for(int i = 0; i < WIDTH; i++){printf("_");} - printf("\n"); + for(int i = 0; i < WIDTH; i++){printf("-");} + printf("+\n"); } \ No newline at end of file From d1418a1344a3cb955d9a4bd7922e666b65ee7b92 Mon Sep 17 00:00:00 2001 From: David Moeller Date: Sun, 21 Jan 2024 13:33:27 +0100 Subject: [PATCH 04/22] refactoring: drawing snake --- src/main/c/Snake/snake_start.c | 29 +++++++++++++++-------------- src/main/c/Snake/snake_start.h | 6 ++++++ 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/main/c/Snake/snake_start.c b/src/main/c/Snake/snake_start.c index 57d332f..efcd08b 100644 --- a/src/main/c/Snake/snake_start.c +++ b/src/main/c/Snake/snake_start.c @@ -17,6 +17,7 @@ void main_menu(); void game(); Snake initialize_game(); void draw(Snake *snake); +int part_of_snake(Snake *snake, unsigned char tile); void snake_start(){ @@ -83,24 +84,24 @@ void draw(Snake *snake){ for(int i = 1; i <= HEIGHT; i++){ printf("|"); for(int j = 1; j <= WIDTH; j++){ - //printf("%d:", i * 16 + j); - for(int pos = 0; pos < snake->length; pos++){ - //printf(" %d", snake->segments[pos]); - if(i * 16 + j == snake->segments[pos]){ - if(pos == 0){ - printf("0"); - }else{ - printf("O"); - } - break; - }else if(pos == snake->length - 1){ - printf(" "); - } - } + int index = part_of_snake(snake, i * 16 + j); + if(index == 0){printf("0");} + else if(index > 0){printf("O");} + else{printf(" ");} } printf("|\n"); } printf("+"); for(int i = 0; i < WIDTH; i++){printf("-");} printf("+\n"); +} + +//returns index of segments which is identical to tile; -1 if not found +int part_of_snake(Snake *snake, unsigned char tile){ + for(int i = 0; i < snake->length; i++){ + if(snake->segments[i] == tile){ + return i; + } + } + return -1; } \ No newline at end of file diff --git a/src/main/c/Snake/snake_start.h b/src/main/c/Snake/snake_start.h index e69de29..423f324 100644 --- a/src/main/c/Snake/snake_start.h +++ b/src/main/c/Snake/snake_start.h @@ -0,0 +1,6 @@ +#ifndef SNAKE_START_H +#define SNAKE_START_H + +void snake_start(); + +#endif // SNAKE_START_H \ No newline at end of file From d285bcc4135350edbcaa31a3674fef23e04ce4f3 Mon Sep 17 00:00:00 2001 From: David Moeller Date: Sun, 21 Jan 2024 13:48:58 +0100 Subject: [PATCH 05/22] Test part_of_snake --- src/main/c/Snake/snake_start.c | 18 ++++------------ src/main/c/Snake/snake_start.h | 14 ++++++++++++ test/Snake/test_part_of_snake.c | 38 +++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 14 deletions(-) create mode 100644 test/Snake/test_part_of_snake.c diff --git a/src/main/c/Snake/snake_start.c b/src/main/c/Snake/snake_start.c index efcd08b..86f0204 100644 --- a/src/main/c/Snake/snake_start.c +++ b/src/main/c/Snake/snake_start.c @@ -1,21 +1,11 @@ #include #include - - -#define HEIGHT 15 -#define WIDTH 15 -#define AREA HEIGHT * WIDTH - -typedef struct{ - signed char direction[2]; - char length; - unsigned char segments[AREA]; -}Snake; +#include "snake_start.h" void main_menu(); void game(); -Snake initialize_game(); +Snake initialize_snake(); void draw(Snake *snake); int part_of_snake(Snake *snake, unsigned char tile); @@ -55,7 +45,7 @@ void main_menu(){ } void game(){ - Snake snake = initialize_game(); + Snake snake = initialize_snake(); bool running = true; while (running) { @@ -66,7 +56,7 @@ void game(){ getchar(); } -Snake initialize_game(){ +Snake initialize_snake(){ Snake snake = {{0, 1}, 3}; for(int i = 0; i < AREA; i++){ snake.segments[i] = 0; diff --git a/src/main/c/Snake/snake_start.h b/src/main/c/Snake/snake_start.h index 423f324..87a4e1c 100644 --- a/src/main/c/Snake/snake_start.h +++ b/src/main/c/Snake/snake_start.h @@ -1,6 +1,20 @@ #ifndef SNAKE_START_H #define SNAKE_START_H +#define HEIGHT 15 +#define WIDTH 15 +#define AREA HEIGHT * WIDTH + +typedef struct{ + signed char direction[2]; + char length; + unsigned char segments[AREA]; +}Snake; + + void snake_start(); +Snake initialize_snake(); +int part_of_snake(Snake *snake, unsigned char tile); + #endif // SNAKE_START_H \ No newline at end of file diff --git a/test/Snake/test_part_of_snake.c b/test/Snake/test_part_of_snake.c new file mode 100644 index 0000000..5dfad8b --- /dev/null +++ b/test/Snake/test_part_of_snake.c @@ -0,0 +1,38 @@ +#ifdef TEST +#include "unity.h" +#include "../../src/main/c/Snake/snake_start.h" + + +void setUp(void){ + //Wenn Funktion Vorraussetzungen braucht +} +void tearDown(void){ +} + + +void test_find_head(void){ + /* arrange */ + int result; + Snake snake = initialize_snake(); + + /* act */ + result = part_of_snake(&snake, 8 * 16 + 8); + + /* assert */ + TEST_ASSERT_EQUAL_INT(0, result);//head is at 8/8 +} + + +void test_get_correct_index(void){ + /* arrange */ + int result; + Snake snake = initialize_snake(); + + /* act */ + result = part_of_snake(&snake, 8 * 16 + 6); + + /* assert */ + TEST_ASSERT_EQUAL_INT(2, result);//2. part ist at 6/8 +} + +#endif // TEST \ No newline at end of file From af57eab9d66875554fdf4459f344d3e653efcdb3 Mon Sep 17 00:00:00 2001 From: David Moeller Date: Tue, 23 Jan 2024 12:53:46 +0100 Subject: [PATCH 06/22] added get_Chara --- src/main/c/Snake/get_character.c | 59 ++++++++++++++++++++++++++++++++ src/main/c/Snake/get_character.h | 6 ++++ src/main/c/Snake/snake_start.c | 54 +++++++++++++++++++++++++++++ test/Snake/test_part_of_snake.c | 13 +++++++ 4 files changed, 132 insertions(+) create mode 100644 src/main/c/Snake/get_character.c create mode 100644 src/main/c/Snake/get_character.h diff --git a/src/main/c/Snake/get_character.c b/src/main/c/Snake/get_character.c new file mode 100644 index 0000000..a800c01 --- /dev/null +++ b/src/main/c/Snake/get_character.c @@ -0,0 +1,59 @@ +#include +#include +#include +#include +#include +#include +#include + +struct termios orig_termios; + +void reset_terminal_mode(){ + tcsetattr(0, TCSANOW, &orig_termios); +} + +void set_conio_terminal_mode(){ + struct termios new_termios; + + /* take two copies - one for now, one for later */ + tcgetattr(0, &orig_termios); + memcpy(&new_termios, &orig_termios, sizeof(new_termios)); + + /* register cleanup handler, and set the new terminal mode */ + atexit(reset_terminal_mode); + cfmakeraw(&new_termios); + tcsetattr(0, TCSANOW, &new_termios); +} + +int kbhit(){ + struct timeval tv = { 0L, 0L }; + fd_set fds; + FD_ZERO(&fds); + FD_SET(0, &fds); + return select(1, &fds, NULL, NULL, &tv) > 0; +} + +int getch(){ + int r; + unsigned char c; + if ((r = read(0, &c, sizeof(c))) < 0) { + return r; + } else { + return c; + } +} + +char get_character(double limit){ + set_conio_terminal_mode(); + + clock_t t = clock(); + char c = 0; + + while ((double)(clock() - t) / CLOCKS_PER_SEC < limit){ + if(kbhit()){c = getch();} + } + + reset_terminal_mode(); + + return c; +} \ No newline at end of file diff --git a/src/main/c/Snake/get_character.h b/src/main/c/Snake/get_character.h new file mode 100644 index 0000000..a955266 --- /dev/null +++ b/src/main/c/Snake/get_character.h @@ -0,0 +1,6 @@ +#ifndef GET_CHARACTER_H +#define GET_CHARACTER_H + +char get_character(double limit); + +#endif // GET_CHARACTER_H \ No newline at end of file diff --git a/src/main/c/Snake/snake_start.c b/src/main/c/Snake/snake_start.c index 86f0204..18d92e8 100644 --- a/src/main/c/Snake/snake_start.c +++ b/src/main/c/Snake/snake_start.c @@ -1,11 +1,18 @@ #include #include +#include +#include +#include #include "snake_start.h" +#include "get_character.h" +#define TIME_TURN 0.5; void main_menu(); void game(); Snake initialize_snake(); +int *get_next_move(double limit); +void move_snake(Snake *snake); void draw(Snake *snake); int part_of_snake(Snake *snake, unsigned char tile); @@ -47,13 +54,23 @@ void main_menu(){ void game(){ Snake snake = initialize_snake(); bool running = true; + clock_t t = clock(); + while (running) { system("clear"); + + t = clock() - t; + int *mv = get_next_move((double)t / CLOCKS_PER_SEC); + t = clock(); + + move_snake(&snake); draw(&snake); running = false; + } getchar(); + getchar(); } Snake initialize_snake(){ @@ -67,6 +84,43 @@ Snake initialize_snake(){ return snake; } +int *get_next_move(double limit){ + limit = TIME_TURN - limit; + int *ptr = (int*) malloc(2 * sizeof(int)); + clock_t t = clock(); + char c; + + c = get_character(limit); + + switch (c){ + case 'w': + case 'A': + printf("Hoch\n"); + break; + case 'a': + case 'D': + printf("Links\n"); + break; + case 's': + case 'B': + printf("Runter\n"); + break; + case 'd': + case 'C': + printf("Rechts\n"); + break; + + default: + break; + } + + return ptr; +} + +void move_snake(Snake *snake){ + +} + void draw(Snake *snake){ printf("+"); for(int i = 0; i < WIDTH; i++){printf("-");} diff --git a/test/Snake/test_part_of_snake.c b/test/Snake/test_part_of_snake.c index 5dfad8b..fcc45e9 100644 --- a/test/Snake/test_part_of_snake.c +++ b/test/Snake/test_part_of_snake.c @@ -35,4 +35,17 @@ void test_get_correct_index(void){ TEST_ASSERT_EQUAL_INT(2, result);//2. part ist at 6/8 } + +void test_snake_not_on_tile(void){ + /* arrange */ + int result; + Snake snake = initialize_snake(); + + /* act */ + result = part_of_snake(&snake, 6 * 16 + 6); + + /* assert */ + TEST_ASSERT_EQUAL_INT(-1, result);//-1 snake is not on 6/6 +} + #endif // TEST \ No newline at end of file From 1c7dfb82378e0d4d863bb7c4b30f511e871f53c1 Mon Sep 17 00:00:00 2001 From: David Moeller Date: Tue, 23 Jan 2024 13:29:33 +0100 Subject: [PATCH 07/22] refactoring: get_next_move --- src/main/c/Snake/snake_start.c | 36 +++++++++++++++++---------------- test/Snake/test_part_of_snake.c | 1 + 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/main/c/Snake/snake_start.c b/src/main/c/Snake/snake_start.c index 18d92e8..e4c826b 100644 --- a/src/main/c/Snake/snake_start.c +++ b/src/main/c/Snake/snake_start.c @@ -5,13 +5,13 @@ #include #include "snake_start.h" #include "get_character.h" -#define TIME_TURN 0.5; +#define TIME_TURN 0.5 void main_menu(); void game(); Snake initialize_snake(); -int *get_next_move(double limit); +void get_next_move(double limit, Snake *snake); void move_snake(Snake *snake); void draw(Snake *snake); int part_of_snake(Snake *snake, unsigned char tile); @@ -56,16 +56,16 @@ void game(){ bool running = true; clock_t t = clock(); - while (running) - { + //while (running){ + for(int i = 0; i < 100; i++){ system("clear"); + draw(&snake); t = clock() - t; - int *mv = get_next_move((double)t / CLOCKS_PER_SEC); + get_next_move(TIME_TURN - (double)t / CLOCKS_PER_SEC, &snake); t = clock(); - move_snake(&snake); - draw(&snake); + running = false; } @@ -81,13 +81,13 @@ Snake initialize_snake(){ snake.segments[0] = 8 + 16 * 8; snake.segments[1] = 7 + 16 * 8; snake.segments[2] = 6 + 16 * 8; + + snake.direction[0] = 1; + snake.direction[1] = 0; return snake; } -int *get_next_move(double limit){ - limit = TIME_TURN - limit; - int *ptr = (int*) malloc(2 * sizeof(int)); - clock_t t = clock(); +void get_next_move(double limit, Snake *snake){ char c; c = get_character(limit); @@ -95,26 +95,28 @@ int *get_next_move(double limit){ switch (c){ case 'w': case 'A': - printf("Hoch\n"); + snake->direction[0] = 0; + snake->direction[1] = -1; break; case 'a': case 'D': - printf("Links\n"); + snake->direction[0] = -1; + snake->direction[1] = 0; break; case 's': case 'B': - printf("Runter\n"); + snake->direction[0] = 0; + snake->direction[1] = 1; break; case 'd': case 'C': - printf("Rechts\n"); + snake->direction[0] = 1; + snake->direction[1] = 0; break; default: break; } - - return ptr; } void move_snake(Snake *snake){ diff --git a/test/Snake/test_part_of_snake.c b/test/Snake/test_part_of_snake.c index fcc45e9..c349867 100644 --- a/test/Snake/test_part_of_snake.c +++ b/test/Snake/test_part_of_snake.c @@ -1,6 +1,7 @@ #ifdef TEST #include "unity.h" #include "../../src/main/c/Snake/snake_start.h" +#include "../../src/main/c/Snake/get_character.h" void setUp(void){ From 60076577cc7913fcc36b69942b530b1f9473fd78 Mon Sep 17 00:00:00 2001 From: David Moeller Date: Tue, 23 Jan 2024 13:47:17 +0100 Subject: [PATCH 08/22] moving snake --- src/main/c/Snake/snake_start.c | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/src/main/c/Snake/snake_start.c b/src/main/c/Snake/snake_start.c index e4c826b..23418cf 100644 --- a/src/main/c/Snake/snake_start.c +++ b/src/main/c/Snake/snake_start.c @@ -11,7 +11,7 @@ void main_menu(); void game(); Snake initialize_snake(); -void get_next_move(double limit, Snake *snake); +void get_next_move(double limit, Snake *snake, bool *running); void move_snake(Snake *snake); void draw(Snake *snake); int part_of_snake(Snake *snake, unsigned char tile); @@ -56,25 +56,19 @@ void game(){ bool running = true; clock_t t = clock(); - //while (running){ - for(int i = 0; i < 100; i++){ + while (running){ system("clear"); draw(&snake); t = clock() - t; - get_next_move(TIME_TURN - (double)t / CLOCKS_PER_SEC, &snake); + get_next_move(TIME_TURN - (double)t / CLOCKS_PER_SEC, &snake, &running); t = clock(); move_snake(&snake); - - running = false; - } - getchar(); - getchar(); } Snake initialize_snake(){ - Snake snake = {{0, 1}, 3}; + Snake snake = {{1, 0}, 3}; for(int i = 0; i < AREA; i++){ snake.segments[i] = 0; } @@ -82,12 +76,10 @@ Snake initialize_snake(){ snake.segments[1] = 7 + 16 * 8; snake.segments[2] = 6 + 16 * 8; - snake.direction[0] = 1; - snake.direction[1] = 0; return snake; } -void get_next_move(double limit, Snake *snake){ +void get_next_move(double limit, Snake *snake, bool *running){ char c; c = get_character(limit); @@ -113,6 +105,8 @@ void get_next_move(double limit, Snake *snake){ snake->direction[0] = 1; snake->direction[1] = 0; break; + case 'q': + *running = false; default: break; @@ -120,7 +114,10 @@ void get_next_move(double limit, Snake *snake){ } void move_snake(Snake *snake){ - + for(int i = snake->length - 1; i > 0; i--){ + snake->segments[i] = snake->segments[i - 1]; + } + snake->segments[0] += snake->direction[0] + snake->direction[1] * 16; } void draw(Snake *snake){ From d96d6594cdf5029867c8754f8dfe3b301912700f Mon Sep 17 00:00:00 2001 From: David Moeller Date: Tue, 23 Jan 2024 14:01:43 +0100 Subject: [PATCH 09/22] killing snake if collision --- src/main/c/Snake/snake_start.c | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/main/c/Snake/snake_start.c b/src/main/c/Snake/snake_start.c index 23418cf..ce54919 100644 --- a/src/main/c/Snake/snake_start.c +++ b/src/main/c/Snake/snake_start.c @@ -15,6 +15,7 @@ void get_next_move(double limit, Snake *snake, bool *running); void move_snake(Snake *snake); void draw(Snake *snake); int part_of_snake(Snake *snake, unsigned char tile); +bool check_if_dead(Snake *snake); void snake_start(){ @@ -59,7 +60,11 @@ void game(){ while (running){ system("clear"); draw(&snake); - + + if(check_if_dead(&snake)){ + break; + } + t = clock() - t; get_next_move(TIME_TURN - (double)t / CLOCKS_PER_SEC, &snake, &running); t = clock(); @@ -147,4 +152,21 @@ int part_of_snake(Snake *snake, unsigned char tile){ } } return -1; +} + +bool check_if_dead(Snake *snake){ + //Self + for(int i = 0; i < snake->length; i++){ + int part = part_of_snake(snake, snake->segments[i]); + if(part != -1 && part != i){ + return true; + } + } + + //Wall + if(snake->segments[0] % 16 == 0 || (snake->segments[0] / 16) % 16 == 0){ + return true; + } + + return false; } \ No newline at end of file From ca67976e794d7906d2d3721dd097f1b1e27a8d85 Mon Sep 17 00:00:00 2001 From: David Moeller Date: Tue, 23 Jan 2024 14:49:21 +0100 Subject: [PATCH 10/22] Added fruit --- src/main/c/Snake/snake_start.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/main/c/Snake/snake_start.c b/src/main/c/Snake/snake_start.c index ce54919..0584deb 100644 --- a/src/main/c/Snake/snake_start.c +++ b/src/main/c/Snake/snake_start.c @@ -13,9 +13,10 @@ void game(); Snake initialize_snake(); void get_next_move(double limit, Snake *snake, bool *running); void move_snake(Snake *snake); -void draw(Snake *snake); +void draw(Snake *snake, unsigned char fruit); int part_of_snake(Snake *snake, unsigned char tile); bool check_if_dead(Snake *snake); +unsigned char spawn_fruit(Snake *snake); void snake_start(){ @@ -56,10 +57,11 @@ void game(){ Snake snake = initialize_snake(); bool running = true; clock_t t = clock(); + unsigned char fruit = spawn_fruit(&snake); while (running){ system("clear"); - draw(&snake); + draw(&snake, fruit); if(check_if_dead(&snake)){ break; @@ -125,7 +127,8 @@ void move_snake(Snake *snake){ snake->segments[0] += snake->direction[0] + snake->direction[1] * 16; } -void draw(Snake *snake){ +void draw(Snake *snake, unsigned char fruit){ + printf("%d\n", fruit); printf("+"); for(int i = 0; i < WIDTH; i++){printf("-");} printf("+\n"); @@ -135,6 +138,7 @@ void draw(Snake *snake){ int index = part_of_snake(snake, i * 16 + j); if(index == 0){printf("0");} else if(index > 0){printf("O");} + else if(i * 16 + j == fruit){printf("X");} else{printf(" ");} } printf("|\n"); @@ -169,4 +173,14 @@ bool check_if_dead(Snake *snake){ } return false; +} + +unsigned char spawn_fruit(Snake *snake){ + srand(time(NULL)); + int r = 0; + while (part_of_snake(snake, r) != -1 || r % 16 == 0 || (r / 16) % 16 == 0){ + r = rand() % 256; + printf("%d\n", r); + } + return r; } \ No newline at end of file From bd65725af8c62621723d926b788e13e6d6e17951 Mon Sep 17 00:00:00 2001 From: David Moeller Date: Tue, 23 Jan 2024 14:54:10 +0100 Subject: [PATCH 11/22] refactoring: check_if_dead --- src/main/c/Snake/snake_start.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/main/c/Snake/snake_start.c b/src/main/c/Snake/snake_start.c index 0584deb..a56e34c 100644 --- a/src/main/c/Snake/snake_start.c +++ b/src/main/c/Snake/snake_start.c @@ -150,7 +150,7 @@ void draw(Snake *snake, unsigned char fruit){ //returns index of segments which is identical to tile; -1 if not found int part_of_snake(Snake *snake, unsigned char tile){ - for(int i = 0; i < snake->length; i++){ + for(int i = snake->length - 1; i >= 0; i--){ if(snake->segments[i] == tile){ return i; } @@ -160,11 +160,8 @@ int part_of_snake(Snake *snake, unsigned char tile){ bool check_if_dead(Snake *snake){ //Self - for(int i = 0; i < snake->length; i++){ - int part = part_of_snake(snake, snake->segments[i]); - if(part != -1 && part != i){ - return true; - } + if(part_of_snake(snake, snake->segments[0]) > 0){ + return true; } //Wall From ce57e7533ce5a1f6485777f5d8004978086f9f4c Mon Sep 17 00:00:00 2001 From: David Moeller Date: Thu, 25 Jan 2024 10:00:07 +0100 Subject: [PATCH 12/22] Test self_collision --- src/main/c/Snake/get_character.c | 1 - test/Snake/test_collision.c | 24 ++++++++++++++++++++++++ test/Snake/test_part_of_snake.c | 7 ++----- 3 files changed, 26 insertions(+), 6 deletions(-) create mode 100644 test/Snake/test_collision.c diff --git a/src/main/c/Snake/get_character.c b/src/main/c/Snake/get_character.c index a800c01..9c180ef 100644 --- a/src/main/c/Snake/get_character.c +++ b/src/main/c/Snake/get_character.c @@ -3,7 +3,6 @@ #include #include #include -#include #include struct termios orig_termios; diff --git a/test/Snake/test_collision.c b/test/Snake/test_collision.c new file mode 100644 index 0000000..81d1a76 --- /dev/null +++ b/test/Snake/test_collision.c @@ -0,0 +1,24 @@ +#ifdef TEST +#include "unity.h" +#include +#include "../../src/main/c/Snake/snake_start.h" +#include "../../src/main/c/Snake/get_character.h" + + +void setUp(void){} +void tearDown(void){} + + +void test_self_collision(void){ + /* arrange */ + bool result; + Snake snake = {{1, 0}, 5, {6 + 16 * 6, 6 + 16 * 7, 7 + 16 * 7, 7 + 16 * 6, 6 + 16 * 6}}; + + /* act */ + result = check_if_dead(&snake); + + /* assert */ + TEST_ASSERT_TRUE(result);//head collides with body +} + +#endif // TEST \ No newline at end of file diff --git a/test/Snake/test_part_of_snake.c b/test/Snake/test_part_of_snake.c index c349867..95ce3bd 100644 --- a/test/Snake/test_part_of_snake.c +++ b/test/Snake/test_part_of_snake.c @@ -4,11 +4,8 @@ #include "../../src/main/c/Snake/get_character.h" -void setUp(void){ - //Wenn Funktion Vorraussetzungen braucht -} -void tearDown(void){ -} +void setUp(void){} +void tearDown(void){} void test_find_head(void){ From 5e744b90792d9a25be9e8bd17d115710b3d63dce Mon Sep 17 00:00:00 2001 From: David Moeller Date: Thu, 25 Jan 2024 10:01:55 +0100 Subject: [PATCH 13/22] Test no_collision --- test/Snake/test_collision.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/Snake/test_collision.c b/test/Snake/test_collision.c index 81d1a76..d0edd61 100644 --- a/test/Snake/test_collision.c +++ b/test/Snake/test_collision.c @@ -21,4 +21,17 @@ void test_self_collision(void){ TEST_ASSERT_TRUE(result);//head collides with body } + +void test_no_collision(void){ + /* arrange */ + bool result; + Snake snake = initialize_snake(); + + /* act */ + result = check_if_dead(&snake); + + /* assert */ + TEST_ASSERT_FALSE(result);//head collides with body +} + #endif // TEST \ No newline at end of file From 819208c1e918231b110f60009eee50262b58889e Mon Sep 17 00:00:00 2001 From: David Moeller Date: Thu, 25 Jan 2024 10:04:07 +0100 Subject: [PATCH 14/22] Test wall_collision --- test/Snake/test_collision.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/Snake/test_collision.c b/test/Snake/test_collision.c index d0edd61..b70676d 100644 --- a/test/Snake/test_collision.c +++ b/test/Snake/test_collision.c @@ -34,4 +34,17 @@ void test_no_collision(void){ TEST_ASSERT_FALSE(result);//head collides with body } + +void test_wall_collision(void){ + /* arrange */ + bool result; + Snake snake = {{1, 0}, 4, {0 + 16 * 6, 1 + 16 * 6, 2 + 16 * 6, 3 + 16 * 6}}; + + /* act */ + result = check_if_dead(&snake); + + /* assert */ + TEST_ASSERT_TRUE(result);//head collides with body +} + #endif // TEST \ No newline at end of file From 46d38f2857e468fdf8dd9853ca2189323cab467b Mon Sep 17 00:00:00 2001 From: David Moeller Date: Thu, 25 Jan 2024 10:16:08 +0100 Subject: [PATCH 15/22] Snake gets longer when eating fruit --- src/main/c/Snake/snake_start.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/main/c/Snake/snake_start.c b/src/main/c/Snake/snake_start.c index a56e34c..068eca7 100644 --- a/src/main/c/Snake/snake_start.c +++ b/src/main/c/Snake/snake_start.c @@ -5,7 +5,7 @@ #include #include "snake_start.h" #include "get_character.h" -#define TIME_TURN 0.5 +#define TIME_TURN 0.3 void main_menu(); @@ -17,6 +17,7 @@ void draw(Snake *snake, unsigned char fruit); int part_of_snake(Snake *snake, unsigned char tile); bool check_if_dead(Snake *snake); unsigned char spawn_fruit(Snake *snake); +unsigned char eating_fruit(Snake *snake); void snake_start(){ @@ -71,6 +72,7 @@ void game(){ get_next_move(TIME_TURN - (double)t / CLOCKS_PER_SEC, &snake, &running); t = clock(); move_snake(&snake); + if(part_of_snake(&snake, fruit) == 0){fruit = eating_fruit(&snake);} } } @@ -180,4 +182,9 @@ unsigned char spawn_fruit(Snake *snake){ printf("%d\n", r); } return r; +} + +unsigned char eating_fruit(Snake *snake){ + snake->length++; + return spawn_fruit(snake); } \ No newline at end of file From 9a3247bf684857c1cf33b4990a87e14897f62bd8 Mon Sep 17 00:00:00 2001 From: David Moeller Date: Thu, 25 Jan 2024 10:22:33 +0100 Subject: [PATCH 16/22] refactoring: movement of snake --- src/main/c/Snake/snake_start.c | 16 ++++++---------- src/main/c/Snake/snake_start.h | 2 +- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/src/main/c/Snake/snake_start.c b/src/main/c/Snake/snake_start.c index 068eca7..70bc96e 100644 --- a/src/main/c/Snake/snake_start.c +++ b/src/main/c/Snake/snake_start.c @@ -77,7 +77,7 @@ void game(){ } Snake initialize_snake(){ - Snake snake = {{1, 0}, 3}; + Snake snake = {1, 3}; for(int i = 0; i < AREA; i++){ snake.segments[i] = 0; } @@ -96,23 +96,19 @@ void get_next_move(double limit, Snake *snake, bool *running){ switch (c){ case 'w': case 'A': - snake->direction[0] = 0; - snake->direction[1] = -1; + snake->direction = -16; break; case 'a': case 'D': - snake->direction[0] = -1; - snake->direction[1] = 0; + snake->direction = -1; break; case 's': case 'B': - snake->direction[0] = 0; - snake->direction[1] = 1; + snake->direction = 16; break; case 'd': case 'C': - snake->direction[0] = 1; - snake->direction[1] = 0; + snake->direction = 1; break; case 'q': *running = false; @@ -126,7 +122,7 @@ void move_snake(Snake *snake){ for(int i = snake->length - 1; i > 0; i--){ snake->segments[i] = snake->segments[i - 1]; } - snake->segments[0] += snake->direction[0] + snake->direction[1] * 16; + snake->segments[0] += snake->direction; } void draw(Snake *snake, unsigned char fruit){ diff --git a/src/main/c/Snake/snake_start.h b/src/main/c/Snake/snake_start.h index 87a4e1c..9240626 100644 --- a/src/main/c/Snake/snake_start.h +++ b/src/main/c/Snake/snake_start.h @@ -6,7 +6,7 @@ #define AREA HEIGHT * WIDTH typedef struct{ - signed char direction[2]; + signed char direction; char length; unsigned char segments[AREA]; }Snake; From 82360823017c7513101c9e85b4da0f04c034d83a Mon Sep 17 00:00:00 2001 From: David Moeller Date: Thu, 25 Jan 2024 10:44:08 +0100 Subject: [PATCH 17/22] Test moving right --- src/main/c/Snake/snake_start.c | 4 ++-- src/main/c/Snake/snake_start.h | 2 ++ test/Snake/test_collision.c | 4 ++-- test/Snake/test_moving_snake.c | 27 +++++++++++++++++++++++++++ 4 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 test/Snake/test_moving_snake.c diff --git a/src/main/c/Snake/snake_start.c b/src/main/c/Snake/snake_start.c index 70bc96e..12fb3d5 100644 --- a/src/main/c/Snake/snake_start.c +++ b/src/main/c/Snake/snake_start.c @@ -7,7 +7,7 @@ #include "get_character.h" #define TIME_TURN 0.3 - +#pragma region Funktion_heads void main_menu(); void game(); Snake initialize_snake(); @@ -18,7 +18,7 @@ int part_of_snake(Snake *snake, unsigned char tile); bool check_if_dead(Snake *snake); unsigned char spawn_fruit(Snake *snake); unsigned char eating_fruit(Snake *snake); - +#pragma endregion void snake_start(){ system("clear"); diff --git a/src/main/c/Snake/snake_start.h b/src/main/c/Snake/snake_start.h index 9240626..1634146 100644 --- a/src/main/c/Snake/snake_start.h +++ b/src/main/c/Snake/snake_start.h @@ -15,6 +15,8 @@ typedef struct{ void snake_start(); Snake initialize_snake(); int part_of_snake(Snake *snake, unsigned char tile); +void move_snake(Snake *snake); +bool check_if_dead(Snake *snake); #endif // SNAKE_START_H \ No newline at end of file diff --git a/test/Snake/test_collision.c b/test/Snake/test_collision.c index b70676d..e5d5a63 100644 --- a/test/Snake/test_collision.c +++ b/test/Snake/test_collision.c @@ -12,7 +12,7 @@ void tearDown(void){} void test_self_collision(void){ /* arrange */ bool result; - Snake snake = {{1, 0}, 5, {6 + 16 * 6, 6 + 16 * 7, 7 + 16 * 7, 7 + 16 * 6, 6 + 16 * 6}}; + Snake snake = {1, 5, {6 + 16 * 6, 6 + 16 * 7, 7 + 16 * 7, 7 + 16 * 6, 6 + 16 * 6}}; /* act */ result = check_if_dead(&snake); @@ -38,7 +38,7 @@ void test_no_collision(void){ void test_wall_collision(void){ /* arrange */ bool result; - Snake snake = {{1, 0}, 4, {0 + 16 * 6, 1 + 16 * 6, 2 + 16 * 6, 3 + 16 * 6}}; + Snake snake = {-1, 4, {0 + 16 * 6, 1 + 16 * 6, 2 + 16 * 6, 3 + 16 * 6}}; /* act */ result = check_if_dead(&snake); diff --git a/test/Snake/test_moving_snake.c b/test/Snake/test_moving_snake.c new file mode 100644 index 0000000..b098f9e --- /dev/null +++ b/test/Snake/test_moving_snake.c @@ -0,0 +1,27 @@ +#ifdef TEST +#include "unity.h" +#include +#include +#include "../../src/main/c/Snake/snake_start.h" +#include "../../src/main/c/Snake/get_character.h" + + +void setUp(void){} +void tearDown(void){} + + +void test_moving_right(void){ + /* arrange */ + bool result; + Snake snake = {1, 3, {8 + 16 * 6, 7 + 16 * 6, 6 + 16 * 6}}; + Snake expected = {1, 3, {9 + 16 * 6, 8 + 16 * 6, 7 + 16 * 6}}; + + /* act */ + move_snake(&snake); + result = memcmp(&snake, &expected, sizeof(Snake)) == 0; + + /* assert */ + TEST_ASSERT_TRUE(result);//head collides with body +} + +#endif // TEST \ No newline at end of file From 1612d95af9c776e7c7844f69d998b09d730b051b Mon Sep 17 00:00:00 2001 From: David Moeller Date: Thu, 25 Jan 2024 10:47:13 +0100 Subject: [PATCH 18/22] Test moving down --- test/Snake/test_moving_snake.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/test/Snake/test_moving_snake.c b/test/Snake/test_moving_snake.c index b098f9e..6c400b8 100644 --- a/test/Snake/test_moving_snake.c +++ b/test/Snake/test_moving_snake.c @@ -21,7 +21,21 @@ void test_moving_right(void){ result = memcmp(&snake, &expected, sizeof(Snake)) == 0; /* assert */ - TEST_ASSERT_TRUE(result);//head collides with body + TEST_ASSERT_TRUE(result); +} + +void test_moving_down(void){ + /* arrange */ + bool result; + Snake snake = {16, 3, {8 + 16 * 6, 7 + 16 * 6, 6 + 16 * 6}}; + Snake expected = {16, 3, {8 + 16 * 7, 8 + 16 * 6, 7 + 16 * 6}}; + + /* act */ + move_snake(&snake); + result = memcmp(&snake, &expected, sizeof(Snake)) == 0; + + /* assert */ + TEST_ASSERT_TRUE(result); } #endif // TEST \ No newline at end of file From 890f363d88434969743bc3dc5c6d3f1e7bd848c4 Mon Sep 17 00:00:00 2001 From: David Moeller Date: Thu, 25 Jan 2024 10:48:18 +0100 Subject: [PATCH 19/22] Test moving left --- test/Snake/test_moving_snake.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/Snake/test_moving_snake.c b/test/Snake/test_moving_snake.c index 6c400b8..3bd9b6a 100644 --- a/test/Snake/test_moving_snake.c +++ b/test/Snake/test_moving_snake.c @@ -38,4 +38,18 @@ void test_moving_down(void){ TEST_ASSERT_TRUE(result); } +void test_moving_left(void){ + /* arrange */ + bool result; + Snake snake = {-1, 3, {8 + 16 * 6, 7 + 16 * 6, 6 + 16 * 6}}; + Snake expected = {-1, 3, {7 + 16 * 6, 8 + 16 * 6, 7 + 16 * 6}}; + + /* act */ + move_snake(&snake); + result = memcmp(&snake, &expected, sizeof(Snake)) == 0; + + /* assert */ + TEST_ASSERT_TRUE(result); +} + #endif // TEST \ No newline at end of file From 0a40d39849709aad2c3089c0d218a1cd67bf0079 Mon Sep 17 00:00:00 2001 From: David Moeller Date: Thu, 25 Jan 2024 10:49:00 +0100 Subject: [PATCH 20/22] Test moving up --- test/Snake/test_moving_snake.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/Snake/test_moving_snake.c b/test/Snake/test_moving_snake.c index 3bd9b6a..115d55f 100644 --- a/test/Snake/test_moving_snake.c +++ b/test/Snake/test_moving_snake.c @@ -52,4 +52,18 @@ void test_moving_left(void){ TEST_ASSERT_TRUE(result); } +void test_moving_up(void){ + /* arrange */ + bool result; + Snake snake = {-16, 3, {8 + 16 * 6, 7 + 16 * 6, 6 + 16 * 6}}; + Snake expected = {-16, 3, {8 + 16 * 5, 8 + 16 * 6, 7 + 16 * 6}}; + + /* act */ + move_snake(&snake); + result = memcmp(&snake, &expected, sizeof(Snake)) == 0; + + /* assert */ + TEST_ASSERT_TRUE(result); +} + #endif // TEST \ No newline at end of file From 3122822b72b2f0330e22e8482fea0a85f582cfa9 Mon Sep 17 00:00:00 2001 From: David Moeller Date: Thu, 25 Jan 2024 10:55:57 +0100 Subject: [PATCH 21/22] refactoring: initializing snake --- src/main/c/Snake/snake_start.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/main/c/Snake/snake_start.c b/src/main/c/Snake/snake_start.c index 12fb3d5..68de781 100644 --- a/src/main/c/Snake/snake_start.c +++ b/src/main/c/Snake/snake_start.c @@ -6,6 +6,9 @@ #include "snake_start.h" #include "get_character.h" #define TIME_TURN 0.3 +#define START_LENGTH 3 +#define START_DIRECTION 1 +#define START_TILE 8 * 16 + 8 #pragma region Funktion_heads void main_menu(); @@ -63,11 +66,7 @@ void game(){ while (running){ system("clear"); draw(&snake, fruit); - - if(check_if_dead(&snake)){ - break; - } - + if(check_if_dead(&snake)){break;} t = clock() - t; get_next_move(TIME_TURN - (double)t / CLOCKS_PER_SEC, &snake, &running); t = clock(); @@ -77,14 +76,12 @@ void game(){ } Snake initialize_snake(){ - Snake snake = {1, 3}; - for(int i = 0; i < AREA; i++){ - snake.segments[i] = 0; + Snake snake; + snake.direction = START_DIRECTION; + snake.length = START_LENGTH; + for(int i = 0; i < START_LENGTH; i++){ + snake.segments[i] = START_TILE - START_DIRECTION * i; } - snake.segments[0] = 8 + 16 * 8; - snake.segments[1] = 7 + 16 * 8; - snake.segments[2] = 6 + 16 * 8; - return snake; } From 5c4520f7b03a1544b186ad9e01db4c9506c8c508 Mon Sep 17 00:00:00 2001 From: David Moeller Date: Thu, 25 Jan 2024 11:38:19 +0100 Subject: [PATCH 22/22] adding difficulty options --- src/main/c/Snake/snake_start.c | 24 +++++++++++++++++++++--- src/main/c/main.c | 8 ++++---- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/main/c/Snake/snake_start.c b/src/main/c/Snake/snake_start.c index 68de781..f6b7996 100644 --- a/src/main/c/Snake/snake_start.c +++ b/src/main/c/Snake/snake_start.c @@ -5,7 +5,7 @@ #include #include "snake_start.h" #include "get_character.h" -#define TIME_TURN 0.3 +#define TURN 0.5 #define START_LENGTH 3 #define START_DIRECTION 1 #define START_TILE 8 * 16 + 8 @@ -13,6 +13,7 @@ #pragma region Funktion_heads void main_menu(); void game(); +void options(); Snake initialize_snake(); void get_next_move(double limit, Snake *snake, bool *running); void move_snake(Snake *snake); @@ -23,6 +24,10 @@ unsigned char spawn_fruit(Snake *snake); unsigned char eating_fruit(Snake *snake); #pragma endregion +#pragma region Global +double TIME_TURN = 0.5; +#pragma endregion //Global + void snake_start(){ system("clear"); main_menu(); @@ -36,7 +41,8 @@ void main_menu(){ system("clear"); printf("Waehlen Sie eine Option:\n"); printf("\t1.Start\n"); - printf("\t2.Exit\n"); + printf("\t2.Options\n"); + printf("\t3.Exit\n"); scanf("%d", &option); getchar(); @@ -48,6 +54,9 @@ void main_menu(){ game(); break; case 2: + options(); + break; + case 3: running = false; break; @@ -75,6 +84,14 @@ void game(){ } } +void options(){ + int difficulty = 1; + system("clear"); + printf("Please select a difficulty(1 - 10): "); + scanf("%d", &difficulty); + TIME_TURN = TURN / difficulty;//(11 - difficulty) * TURN / 10; +} + Snake initialize_snake(){ Snake snake; snake.direction = START_DIRECTION; @@ -123,7 +140,7 @@ void move_snake(Snake *snake){ } void draw(Snake *snake, unsigned char fruit){ - printf("%d\n", fruit); + printf("Score:%d Speed:%f\n", snake->length - START_LENGTH, TIME_TURN); printf("+"); for(int i = 0; i < WIDTH; i++){printf("-");} printf("+\n"); @@ -141,6 +158,7 @@ void draw(Snake *snake, unsigned char fruit){ printf("+"); for(int i = 0; i < WIDTH; i++){printf("-");} printf("+\n"); + printf("(Press q to Quit)\n"); } //returns index of segments which is identical to tile; -1 if not found diff --git a/src/main/c/main.c b/src/main/c/main.c index 27cfcae..922c8d4 100644 --- a/src/main/c/main.c +++ b/src/main/c/main.c @@ -14,9 +14,9 @@ int main(){ system("clear"); printf("Waehlen Sie eine Option:\n"); - printf("\t1.Snake starten\n"); + printf("\t1.Spiel1 starten\n"); printf("\t2.Spiel2 starten\n"); - printf("\t3.Spiel3 starten\n"); + printf("\t3.Snake starten\n"); printf("\t4.Spiel4 starten\n"); printf("\t100.Template starten\n"); printf("\t6.Exit\n"); @@ -26,13 +26,13 @@ int main(){ switch (option){ case 1: - snake_start(); + //start_game1(); break; case 2: //start_game2(); break; case 3: - //start_game3(); + snake_start(); break; case 4: //start_game4();