diff --git a/a3/main.c b/a3/main.c index 175c2f4..805d0c1 100644 --- a/a3/main.c +++ b/a3/main.c @@ -2,39 +2,66 @@ #include "snake.h" #include #include -#include +#include // usleep +#include // terminal config +#include // non-blocking input -int main(){ +// Enable non-blocking keyboard input +void enable_nonblocking_input() { + struct termios ttystate; + tcgetattr(STDIN_FILENO, &ttystate); + ttystate.c_lflag &= ~(ICANON | ECHO); // Disable buffering and echo + tcsetattr(STDIN_FILENO, TCSANOW, &ttystate); + fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK); // Non-blocking mode +} + +// Reset terminal settings to default +void disable_nonblocking_input() { + struct termios ttystate; + tcgetattr(STDIN_FILENO, &ttystate); + ttystate.c_lflag |= ICANON | ECHO; + tcsetattr(STDIN_FILENO, TCSANOW, &ttystate); +} + +int main() { struct state game_world; + init_world(&game_world, 30, 20); // Bigger field - // Initialize the game world with dimensions 20x20 - init_world(&game_world, 20, 20); + game_world.sx = 1; // Initial movement right + game_world.sy = 0; - // Run the game loop until the game ends - while(1){ + enable_nonblocking_input(); + + while (1) { + // Clear screen and print game state + printf("\033[H\033[J"); // Clear terminal screen print_world(&game_world); - // Update the game state based on snake movement - int game_status = step_state(&game_world); + // Read input + char ch = getchar(); + switch (ch) { + case 'w': if (game_world.sy != 1) { game_world.sx = 0; game_world.sy = -1; } break; + case 's': if (game_world.sy != -1) { game_world.sx = 0; game_world.sy = 1; } break; + case 'a': if (game_world.sx != 1) { game_world.sx = -1; game_world.sy = 0; } break; + case 'd': if (game_world.sx != -1) { game_world.sx = 1; game_world.sy = 0; } break; + case 'q': printf("Quit.\n"); goto end; + } - // Check the result of the move - if (game_status == END_WALL) { - printf("Game Over: Hit the Wall!\n"); + // Step game + int status = step_state(&game_world); + if (status == END_WALL) { + printf("Game Over: Hit the wall!\n"); break; - } - if (game_status == END_SNAKE) { - printf("Game Over: Snake hit itself!\n"); - break; - } - if (game_status == END_FOOD) { - printf("Congratulations! You ate all the food!\n"); + } else if (status == END_SNAKE) { + printf("Game Over: Ate itself!\n"); break; } - sleep(1); + usleep(200000); // Delay ~5 fps } +end: + disable_nonblocking_input(); free_snake(game_world.snake); - return 0; }