diff options
author | lshprung <lshprung@yahoo.com> | 2020-12-17 14:26:34 -0800 |
---|---|---|
committer | lshprung <lshprung@yahoo.com> | 2020-12-17 14:26:34 -0800 |
commit | 9d05943cb413d4cb710f920b5875430f00496ab9 (patch) | |
tree | 538d86ef9f3bf7614493cb7be189f4e7d7ee94f3 /body.c | |
parent | 49d7a088a0768fdbd22624f28d4350469b8da421 (diff) |
Working updateScreen function
Diffstat (limited to 'body.c')
-rw-r--r-- | body.c | 147 |
1 files changed, 147 insertions, 0 deletions
@@ -0,0 +1,147 @@ +#include <assert.h> +#include <stdlib.h> + +#include "include/body.h" + +typedef struct node{ + int x; + int y; + struct node *next; +} NODE; + +typedef struct snake{ + NODE *head; //"head" will be the physical tail of the snake + NODE *tail; //"tail" will be the physical head of the snake + int length; +} SNAKE; + +//private prototypes +NODE *makeNode(int x, int y); +void deleteNode(NODE *np); + +//public functions +SNAKE *makeSnake(int startx, int starty, int len){ + SNAKE *new = malloc(sizeof(SNAKE)); + NODE *np; + + assert(new != NULL); + + new->length = len; + + new->head = makeNode(startx-len+1, starty); + np = new->head; + len--; + + while(len > 0){ + np->next = makeNode(startx-len+1, starty); + np = np->next; + len--; + } + + new->tail = np; + + return new; +} + +void deleteSnake(SNAKE *sp){ + NODE *hold; + + if(sp != NULL){ + while(sp->head != NULL){ + hold = sp->head; + sp->head = sp->head->next; + deleteNode(hold); + } + } + + free(sp); + return; +} + +void updateSnake(SNAKE *sp, int dir){ + NODE *np; + + if(sp == NULL) return; + + //move the "back" to the "front" (note that sp->tail will not be set until after np->x and np->y are set) + np = sp->head; + sp->head = sp->head->next; + sp->tail->next = np; + np->next = NULL; + + //dir decides direction the snake is moving + switch(dir){ + case(0): //snake is moving up + np->x = sp->tail->x; + np->y = sp->tail->y-1; + break; + + case(1): //snake is moving right + np->x = sp->tail->x+1; + np->y = sp->tail->y; + break; + + case(2): //snake is moving down + np->x = sp->tail->x; + np->y = sp->tail->y+1; + break; + + case(3): //snake is moving left + np->x = sp->tail->x-1; + np->y = sp->tail->y; + } + + //update tail pointer + sp->tail = np; + + //TODO write checkSnake() which will check to see if the snake had a collision + + return; +} + + +int getSnakeTailx(SNAKE *sp){ + + if(sp != NULL && sp->tail != NULL) return sp->tail->x; + return -1; + +} +int getSnakeTaily(SNAKE *sp){ + + if(sp != NULL && sp->tail != NULL) return sp->tail->y; + return -1; + +} + +int getSnakeHeadx(SNAKE *sp){ + + if(sp != NULL && sp->head != NULL) return sp->head->x; + return -1; + +} + +int getSnakeHeady(SNAKE *sp){ + + if(sp != NULL && sp->head != NULL) return sp->head->y; + return -1; + +} + +//private functions +NODE *makeNode(int x, int y){ + NODE *new = malloc(sizeof(NODE)); + assert(new != NULL); + + new->x = x; + new->y = y; + new->next = NULL; + + return new; +} + +void deleteNode(NODE *np){ + + if(np != NULL) free(np); + + return; +} |