From dd918c9decb4dfe5f5d45a5b8a3d0d6cb40a873f Mon Sep 17 00:00:00 2001 From: Hanse-14 Date: Sat, 2 Dec 2023 13:51:06 +0100 Subject: [PATCH] Add functions in snake.c snakeInit(), snakeGrow(), snakeMove(), snakeSetDir(), snakeIsAlive(), snakeSetHeadPos() --- include/snake.h | 2 + src/snake.c | 103 +++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 99 insertions(+), 6 deletions(-) diff --git a/include/snake.h b/include/snake.h index 6f62e86..68cc199 100644 --- a/include/snake.h +++ b/include/snake.h @@ -21,6 +21,8 @@ typedef struct snake_t bool isAlive; // lebt die Schlange noch oder ist sie mit sich selbst kollidiert? } snake_t; +extern gameData_t game; + void snakeInit(); // Snake mit bestimmter Startlänge an Startposition erstellen diff --git a/src/snake.c b/src/snake.c index bd5d5eb..9b1a407 100644 --- a/src/snake.c +++ b/src/snake.c @@ -1,25 +1,116 @@ #include "snake.h" -void snakeInit(){ + +void snakeInit() +{ + + //----------------------------------- + //-------------Default--------------- + //----------------------------------- + + // length + config.snakeDefaultLength = 2; // inkl head + game.snake.length = config.snakeDefaultLength; + + // coordinates of head + game.snake.headX = 5; + game.snake.headY = 5; + + // movement direction + game.snake.direction = 3; // left + + // coordinates of tail + game.snake.tail[0][0] = 5; // X-coordinate + game.snake.tail[0][1] = 5; // Y-coordinate + game.snake.tail[1][0] = 4; // X-coordinate + game.snake.tail[1][1] = 4; // Y-coordinate + + // alive + game.snake.isAlive = true; return; } -void snakeGrow(){ +void snakeGrow() +{ + game.snake.length++; + // tail part is attached left of last tail part + // maybe problem while rendering --> MUST BE SOLVED THEN + game.snake.tail[game.snake.length][0] = game.snake.tail[game.snake.length - 1][0] - 1; + game.snake.tail[game.snake.length][1] = game.snake.tail[game.snake.length - 1][1]; return; } -void snakeMove(){ +void snakeMove() +{ + int i = game.snake.length - 1; // counter for snake moving + + // update head position + snakeSetHeadPos(); + + // tail part of[x,y][0,1] get coordinates of tail part before + while(i) + { + game.snake.tail[i][0] = game.snake.tail[i-1][0]; + game.snake.tail[i][1] = game.snake.tail[i-1][1]; + i--; + } + game.snake.tail[0][0] = game.snake.headX; + game.snake.tail[0][1] = game.snake.headY; + return; } -void snakeSetDir(snakeDirection_t dir){ +void snakeSetDir(snakeDirection_t dir) +{ + game.snake.direction = dir; return; } -bool snakeIsAlive(){ +bool snakeIsAlive() +{ + for(int i = 1; i < game.snake.length; i++) + { + if(game.snake.tail[i][0] == game.snake.headX && game.snake.tail[i][1] == game.snake.headY) + return false; + } return true; } -void snakeSetHeadPos(){ +void snakeSetHeadPos() +{ + switch(game.snake.direction) + { + // DOWN + case DOWN: + game.snake.headX = game.snake.tail[0][0]; + game.snake.headY = game.snake.tail[0][1] + 1; + if(game.snake.headY >= game.snake.height) + game.snake.headY = 0; + break; + + // UP + case UP: + game.snake.headX = game.snake.tail[0][0]; + game.snake.headY = game.snake.tail[0][1] - 1; + if(game.snake.headY <= 0) + game.snake.headY = game.snake.height - 1; + break; + + // LEFT + case LEFT: + game.snake.headX = game.snake.tail[0][0] - 1; + game.snake.headY = game.snake.tail[0][1]; + if(game.snake.headX <= 0) + game.snake.headX = game.snake.weight - 1; + break; + + // RIGHT + case RIGHT: + game.snake.headX = game.snake.tail[0][0] + 1; + game.snake.headY = game.snake.tail[0][1]; + if(game.snake.headX >= game.snake.weight) + game.snake.headX = 0; + break; + } return; } \ No newline at end of file