Add functions in snake.c

snakeInit(), snakeGrow(), snakeMove(), snakeSetDir(), snakeIsAlive(),
snakeSetHeadPos()
This commit is contained in:
Hanse-14 2023-12-02 13:51:06 +01:00
parent b7bbd22f71
commit dd918c9dec
2 changed files with 99 additions and 6 deletions

View File

@ -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

View File

@ -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;
}