#include #include #include #include #include "input.h" #include "screen.h" #include "player.h" #include "food.h" #include "config.h" #include "platform.h" #include "sleep.h" void drawPlayer(Player *player, Screen *screen) { PlayerNode *node; for (node = player->tail; node != NULL; node = node->next) *screenGetPoint(screen, node->x, node->y) = '#'; } Food generateFood(Player *player) { Food food; do { food = (Food){rand() % SIZE, rand() % SIZE}; } while (playerCheckFoodCollision(player, food)); return food; } int main(int argc, char **argv) { srand((unsigned int)time(NULL)); Player *player = playerCreate(DOWN, DEFX, DEFY, 0); Screen *screen = screenCreate(SIZE, SIZE, ' '); int i; int head_x, head_y; Food food = generateFood(player); bool *running = malloc(sizeof(bool)); *running = true; int *key = malloc(sizeof(char)); *key = 0; bool stopped = false; InputArgs input_args = (InputArgs){ key, running }; threadCreate(input, &input_args); while (*running) { screenSet(screen, ' '); drawPlayer(player, screen); *screenGetPoint(screen, food.x, food.y) = '@'; resetCoordinates(); screenShow(screen); for (i = 0; i < SIZE*2; ++i) putchar('-'); printf("\nScore: %d\n", player->score); sleepMS(1000); switch (*key) { case 'q': *running = false; return 0; case 'p': stopped = !stopped; break; case 'w': if (player->direction == DOWN) break; player->direction = UP; break; case 'd': if (player->direction == LEFT) break; player->direction = RIGHT; break; case 's': if (player->direction == UP) break; player->direction = DOWN; break; case 'a': if (player->direction == RIGHT) break; player->direction = LEFT; break; } *key = 0; if (stopped) continue; if (playerDoTick(player, food) && player->score < SIZE*SIZE - 1) food = generateFood(player); head_x = player->head->x; head_y = player->head->y; if (head_x >= SIZE || head_x < 0 || head_y >= SIZE || head_y < 0 || playerCheckSelfCollision(player)) { *running = false; break; } } return 0; }