diff --git a/Makefile b/Makefile index 2163dbc..e93e1de 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ LD = ld RM = rm -f SRCDIR = src OBJDIR = obj -SRC = main.c screen.c input.c +SRC = main.c screen.c input.c player.c OBJ = $(addprefix $(OBJDIR)/,$(SRC:.c=.o)) default: $(OUT) diff --git a/include/player.h b/include/player.h new file mode 100644 index 0000000..f9a8a00 --- /dev/null +++ b/include/player.h @@ -0,0 +1,33 @@ +#ifndef __PLAYER_H__ +#define __PLAYER_H__ + +#include + +#define UP 0 +#define RIGHT 1 +#define DOWN 2 +#define LEFT 3 + +typedef int Direction; +typedef struct player_node_t PlayerNode; +typedef struct player_t Player; + +struct player_node_t +{ + int x, y; + PlayerNode *next; +}; + +struct player_t +{ + PlayerNode *tail, *head; + Direction direction; + int score; +}; + +Player *playerCreate(Direction direction, int x, int y, int score); +void playerFree(Player *player); + +void playerDoTick(Player *player, bool food_collision); + +#endif /* __PLAYER_H__ */ diff --git a/src/player.c b/src/player.c new file mode 100644 index 0000000..6461177 --- /dev/null +++ b/src/player.c @@ -0,0 +1,58 @@ +#include "player.h" +#include + +Player *playerCreate(Direction direction, int x, int y, int score) +{ + Player *player = (Player *)malloc(sizeof(Player)); + PlayerNode *head = (PlayerNode *)malloc(sizeof(PlayerNode)); + + player->tail = head; + player->head = head; + player->score = score; + player->direction = direction; + + head->x = x; + head->y = y; + head->next = NULL; +} + +void playerFree(Player *player) +{ +} + +void playerDoTick(Player *player, bool food_collision) +{ + PlayerNode *new_head = (PlayerNode *)malloc(sizeof(PlayerNode)); + int head_x = player->head->x; + int head_y = player->head->y; + + switch(player->direction) + { + case UP: + new_head->x = head_x; + new_head->y = head_y - 1; + break; + case DOWN: + new_head->x = head_x; + new_head->y = head_y + 1; + break; + case LEFT: + new_head->x = head_x - 1; + new_head->y = head_y; + break; + case RIGHT: + new_head->x = head_x + 1; + new_head->y = head_y; + break; + } + + player->head->next = new_head; + player->head = new_head; + + if (!food_collision) + { + PlayerNode *new_tail = player->tail->next; + free(player->tail); + player->tail = new_tail; + } +}