From 6e262115dc576aad05ab383c3d62d1da0e9a082b Mon Sep 17 00:00:00 2001 From: lshprung Date: Fri, 18 Dec 2020 12:23:54 -0800 Subject: Added fruit and scoring functionality --- body.c | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) (limited to 'body.c') diff --git a/body.c b/body.c index 4c44fab..ad06411 100644 --- a/body.c +++ b/body.c @@ -19,6 +19,7 @@ typedef struct snake{ //private prototypes NODE *makeNode(int x, int y); void deleteNode(NODE *np); +void increaseSnake(SNAKE *sp); //public functions SNAKE *makeSnake(int startx, int starty, int len){ @@ -59,7 +60,7 @@ void deleteSnake(SNAKE *sp){ return; } -void updateSnake(SNAKE *sp, int dir, int maxx, int maxy, bool *collision){ +void updateSnake(SNAKE *sp, int dir, int maxx, int maxy, bool *collision, int *fruit_loc, bool *fruit, int *score){ NODE *np; if(sp == NULL) return; @@ -95,7 +96,16 @@ void updateSnake(SNAKE *sp, int dir, int maxx, int maxy, bool *collision){ //update tail pointer sp->tail = np; - //TODO write checkSnake() which will check to see if the snake had a collision + //check if eating fruit + if(*fruit && sp->tail->x == *fruit_loc && sp->tail->y == *(fruit_loc + 1)){ + *fruit = false; + (*score)++; + + //need to increase snake size + increaseSnake(sp); + } + + //check collision *collision = checkSnake(sp, maxx, maxy); return; @@ -165,3 +175,15 @@ void deleteNode(NODE *np){ return; } + +void increaseSnake(SNAKE *sp){ + int xdiff = sp->head->next->x - sp->head->x; + int ydiff = sp->head->next->y - sp->head->y; + NODE *new = makeNode(sp->head->x - xdiff, sp->head->y - ydiff); + + new->next = sp->head; + sp->head = new; + sp->length++; + + return; +} -- cgit