a3
This commit is contained in:
parent
673813d9d8
commit
35d4edf870
63
a3/main.c
63
a3/main.c
@ -2,39 +2,66 @@
|
|||||||
#include "snake.h"
|
#include "snake.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h> // usleep
|
||||||
|
#include <termios.h> // terminal config
|
||||||
|
#include <fcntl.h> // non-blocking input
|
||||||
|
|
||||||
|
// 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() {
|
int main() {
|
||||||
struct state game_world;
|
struct state game_world;
|
||||||
|
init_world(&game_world, 30, 20); // Bigger field
|
||||||
|
|
||||||
// Initialize the game world with dimensions 20x20
|
game_world.sx = 1; // Initial movement right
|
||||||
init_world(&game_world, 20, 20);
|
game_world.sy = 0;
|
||||||
|
|
||||||
|
enable_nonblocking_input();
|
||||||
|
|
||||||
// Run the game loop until the game ends
|
|
||||||
while (1) {
|
while (1) {
|
||||||
|
// Clear screen and print game state
|
||||||
|
printf("\033[H\033[J"); // Clear terminal screen
|
||||||
print_world(&game_world);
|
print_world(&game_world);
|
||||||
|
|
||||||
// Update the game state based on snake movement
|
// Read input
|
||||||
int game_status = step_state(&game_world);
|
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
|
// Step game
|
||||||
if (game_status == END_WALL) {
|
int status = step_state(&game_world);
|
||||||
printf("Game Over: Hit the Wall!\n");
|
if (status == END_WALL) {
|
||||||
|
printf("Game Over: Hit the wall!\n");
|
||||||
break;
|
break;
|
||||||
}
|
} else if (status == END_SNAKE) {
|
||||||
if (game_status == END_SNAKE) {
|
printf("Game Over: Ate itself!\n");
|
||||||
printf("Game Over: Snake hit itself!\n");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (game_status == END_FOOD) {
|
|
||||||
printf("Congratulations! You ate all the food!\n");
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
sleep(1);
|
usleep(200000); // Delay ~5 fps
|
||||||
}
|
}
|
||||||
|
|
||||||
|
end:
|
||||||
|
disable_nonblocking_input();
|
||||||
free_snake(game_world.snake);
|
free_snake(game_world.snake);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user