Files
snake/src/main.c
T
2026-03-03 15:53:21 -05:00

44 lines
868 B
C

#include <fcntl.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
#include <time.h>
#include "engine.h"
int
main()
{
Engine engine;
engine_init(&engine);
char status[64] = { 0 };
static struct termios oldt, newt;
struct timespec req;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
req.tv_nsec = 1.5e8;
req.tv_sec = 0;
fcntl(0, F_SETFL, fcntl(0, F_GETFL) | O_NONBLOCK);
while (is_open(status, 64, &engine) && process_input(&engine.dir))
{
if(equals(&engine.fruit, &engine.head)) {
eat(&engine);
}
move(&engine);
engine_display(&engine);
nanosleep(&req, NULL);
}
// for(int i = 0; i < engine.tail_size; i++) {
// free(engine.tail[i]);
// }
write(1, status, sizeof(status));
write(1, "\033[?25h", 6);
tcsetattr(0, TCSANOW, &oldt);
return 0;
}