This commit is contained in:
2026-02-23 13:36:31 +02:00
parent da36cbe79c
commit 75e51178a1
3 changed files with 93 additions and 84 deletions

View File

@@ -108,13 +108,12 @@ void snake_move(Snake* snake, const int width, const int height) {
check_bounds(snake, width, height); check_bounds(snake, width, height);
} }
void snake_print_info(Snake* snake) { void snake_print_info(const Snake* snake) {
printf("snake: {\n"); printf("snake: {\n");
printf(" parts: {\n"); printf(" parts: {\n");
for (size_t i = 0; i < snake->length; i++) { for (size_t i = 0; i < snake->length; i++) {
BoardPiece part = snake->parts[i]; const BoardPiece part = snake->parts[i];
printf(" x: %d\n", part.x); printf(" { x: %d, y: %d }\n", part.x, part.y);
printf(" y: %d\n", part.y);
} }
printf(" }\n"); printf(" }\n");

View File

@@ -20,7 +20,7 @@ Snake snake_alloc(
); );
void snake_free(Snake* snake); void snake_free(Snake* snake);
void snake_move(Snake* snake, const int width, const int height); void snake_move(Snake* snake, const int width, const int height);
void snake_print_info(Snake* snake); void snake_print_info(const Snake* snake);
void snake_change_direction(Snake* snake, const char direction); void snake_change_direction(Snake* snake, const char direction);
bool snake_collides(const Snake* snake, const BoardPiece* piece); bool snake_collides(const Snake* snake, const BoardPiece* piece);
bool snake_collides_with_tail(const Snake* snake); bool snake_collides_with_tail(const Snake* snake);

View File

@@ -1,20 +1,22 @@
#include <stdio.h>
#include <unistd.h>
#include <termios.h>
#include <stdlib.h>
#include <ctype.h> #include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <time.h> #include <time.h>
#include <unistd.h>
#include "Board.h" #include "Board.h"
#include "Snake.h"
#include "utils.h"
#include "food.h"
#include "BoardPiece.h" #include "BoardPiece.h"
#include "Snake.h"
#include "args.h" #include "args.h"
#include "food.h"
#include "utils.h"
#define TERMIOS 1 #define TERMIOS 1
#define DEBUG 0 #define DEBUG 0
#define MIN_SLEEP_TIME_MS 60
const char snake_vis = '#'; const char snake_vis = '#';
struct termios set_termios(); struct termios set_termios();
@@ -37,6 +39,7 @@ int main(int argc, char** argv) {
BoardPiece food = { .vis_char = '$' }; BoardPiece food = { .vis_char = '$' };
food_new_location(&board, &food, &snake); food_new_location(&board, &food, &snake);
unsigned int score = 0; unsigned int score = 0;
unsigned int sleep_time = args.sleep_ms;
// Screen init // Screen init
system("clear"); system("clear");
@@ -57,8 +60,7 @@ int main(int argc, char** argv) {
(input == 'w' && snake.dir != 's') (input == 'w' && snake.dir != 's')
|| (input == 'a' && snake.dir != 'd') || (input == 'a' && snake.dir != 'd')
|| (input == 's' && snake.dir != 'w') || (input == 's' && snake.dir != 'w')
|| (input == 'd' && snake.dir != 'a') || (input == 'd' && snake.dir != 'a')) {
) {
snake_change_direction(&snake, input); snake_change_direction(&snake, input);
} }
} }
@@ -74,30 +76,38 @@ int main(int argc, char** argv) {
if (snake_collides_with_tail(&snake)) { if (snake_collides_with_tail(&snake)) {
printf("GAME OVER\n"); printf("GAME OVER\n");
exit(EXIT_SUCCESS); break;
} }
if (pieces_collide(&snake_head, &food)) { if (pieces_collide(&snake_head, &food)) {
score++; score++;
snake_add_part(&snake); snake_add_part(&snake);
food_new_location(&board, &food, &snake); food_new_location(&board, &food, &snake);
const unsigned int new_sleep_time = sleep_time - 5;
if (new_sleep_time >= MIN_SLEEP_TIME_MS) {
sleep_time = new_sleep_time;
}
} }
#if DEBUG #if DEBUG
printf("input: %c\n", input); printf("input: %c\n", input);
printf("Frame: %d\n", frame); printf("Frame: %lld\n", frame);
printf("sleep_ms: %u\n", sleep_time);
snake_print_info(&snake); snake_print_info(&snake);
board_piece_print_info(&food, "Food"); board_piece_print_info(&food, "Food");
frame++; frame++;
#endif // DEBUG #endif // DEBUG
sleep_ms(args.sleep_ms); sleep_ms(sleep_time);
} }
#if TERMIOS #if TERMIOS
tcsetattr(STDIN_FILENO, 0, &attr_orig); tcsetattr(STDIN_FILENO, 0, &attr_orig);
#endif // TERMIOS #endif // TERMIOS
board_free(&board);
snake_free(&snake);
return 0; return 0;
} }