diff options
author | lshprung <lshprung@yahoo.com> | 2020-12-17 16:06:12 -0800 |
---|---|---|
committer | lshprung <lshprung@yahoo.com> | 2020-12-17 16:06:12 -0800 |
commit | bc5885c888f0a4df03a455e1b5c70c2307c676b1 (patch) | |
tree | c8946cf625212048f8ed68c869d326bb5a2ab723 /body.c | |
parent | e67207d672cf3164ee392a6834dcd8ed20510d28 (diff) |
Working collision
Diffstat (limited to 'body.c')
-rw-r--r-- | body.c | 22 |
1 files changed, 21 insertions, 1 deletions
@@ -1,4 +1,5 @@ #include <assert.h> +#include <stdbool.h> #include <stdlib.h> #include "include/body.h" @@ -58,7 +59,7 @@ void deleteSnake(SNAKE *sp){ return; } -void updateSnake(SNAKE *sp, int dir){ +void updateSnake(SNAKE *sp, int dir, int maxx, int maxy, bool *collision){ NODE *np; if(sp == NULL) return; @@ -95,10 +96,29 @@ void updateSnake(SNAKE *sp, int dir){ sp->tail = np; //TODO write checkSnake() which will check to see if the snake had a collision + *collision = checkSnake(sp, maxx, maxy); return; } +//true = collision, false = no collision +bool checkSnake(SNAKE *sp, int maxx, int maxy){ + NODE *np; + + if(sp == NULL) return true; + + //check if oob + if(sp->tail->x < 0 || sp->tail->x >= maxx || sp->tail->y < 0 || sp->tail->y >= maxy) return true; + + //check if collision with body (check each cell) + np = sp->head; + while(np != sp->tail && np != NULL){ + if(sp->tail->x == np->x && sp->tail->y == np->y) return true; + np = np->next; + } + + return false; +} int getSnakeTailx(SNAKE *sp){ |