Select Git revision
README.CMake.txt
-
Albrecht Schlosser authored
Cherry-pick the essential changes from FLTK 1.3 since this change had not been ported to 1.4 yet. To do: my current plan is to consolidate 1.3 and 1.4 CMake files as far as possible (with the exceptions of source files, obviously) and to redesign / refactor CMake files later in this process or maybe only for 1.4 if it turns out to be too much to backport.
Albrecht Schlosser authoredCherry-pick the essential changes from FLTK 1.3 since this change had not been ported to 1.4 yet. To do: my current plan is to consolidate 1.3 and 1.4 CMake files as far as possible (with the exceptions of source files, obviously) and to redesign / refactor CMake files later in this process or maybe only for 1.4 if it turns out to be too much to backport.
snake.c 1.10 KiB
#include "snake.h"
// center position
void InitSnake(Snake *snake) {
snake->length = 1;
snake->segments[0].x = (SCREEN_WIDTH / CELL_SIZE) / 2;
snake->segments[0].y = (SCREEN_HEIGHT / CELL_SIZE) / 2;
snake->direction.x = 1;
snake->direction.y = 0;
}
// direction
void UpdateSnake(Snake *snake) {
// Move body segments behind head so the snake is in one piece
for (int i = snake->length - 1; i > 0; i--) {
snake->segments[i] = snake->segments[i - 1];
}
// current direction
snake->segments[0].x += snake->direction.x;
snake->segments[0].y += snake->direction.y;
}
// collision
bool CheckCollision(Snake *snake) {
// Wall
if (snake->segments[0].x < 0 ||
snake->segments[0].x >= SCREEN_WIDTH / CELL_SIZE ||
snake->segments[0].y < 0 ||
snake->segments[0].y >= SCREEN_HEIGHT / CELL_SIZE)
return true;
// Self
for (int i = 1; i < snake->length; i++) {
if (snake->segments[i].x == snake->segments[0].x &&
snake->segments[i].y == snake->segments[0].y)
return true;
}
return false;
}