[C Entry, c Entry
This article is about setting the attributes of a snake, followed by the previous article (V ).
It is easy to set the speed of a snake, as long as it is not a negative number.
void SNK_SetSnakeSpeed(Snake *snake, int speed){ if (snake != 0) snake->speed = SDL_abs(speed);}
It is complicated to set the direction of a snake. Anyone who has ever played the Snake knows that when the snake moves forward, it cannot turn backward; when it moves left, it cannot turn to the right. So I have to make such judgments.
void SNK_SetSnakeDirection(Snake *snake, int direction){ if (snake != 0 && (direction & (SNAKE_UP | SNAKE_DOWN | SNAKE_LEFT | SNAKE_RIGHT))) { if (snake->direction & (SNAKE_UP | SNAKE_DOWN)) { if (direction & (SNAKE_LEFT | SNAKE_RIGHT)) snake->direction = direction; } else { if (direction & (SNAKE_UP | SNAKE_DOWN)) snake->direction = direction; } }}
This ensures that the direction is correct and the turns are correct.
Finally, set the snake color, so there is nothing to say.
void SNK_SetSnakeColor(Snake *snake, Uint8 r, Uint8 g, Uint8 b, Uint8 a){ if (snake != 0) { snake->color.r = r; snake->color.g = g; snake->color.b = b; snake->color.a = a; }}
At this point, the implementation code of the snake is finished. This is the core of the entire game. Next, we will create a beautiful interface.
The complete source code for the snk-snake.c file is as follows:
#include "snk-snake.h"#define INIT_SNAKE(world, size, x, y) \ snake->world = (world); \ snake->x = (SDL_abs(x) > (world)->w) ? 0 : SDL_abs(x); \ snake->x = (size) ? ((snake->x / SDL_abs(size)) * SDL_abs(size)) : 0; \ snake->y = (SDL_abs(y) > (world)->h) ? 0 : SDL_abs(y); \ snake->y = (size) ? ((snake->y / SDL_abs(size)) * SDL_abs(size)) : 0; \ snake->size = (size) ? SDL_abs(size) : 0; \ snake->color.r = snake->color.g = snake->color.b = snake->color.a = 0; \ snake->speed = 0; \ snake->length = 1; \ snake->direction = SNAKE_UP; \ snake->body = 0;#define MOVE_SNAKE(body) do { \ switch ((body)->direction) \ { \ case SNAKE_UP: (body)->y -= snake->size; break; \ case SNAKE_DOWN: (body)->y += snake->size; break; \ case SNAKE_LEFT: (body)->x -= snake->size; break; \ case SNAKE_RIGHT:(body)->x += snake->size; break; \ } \} while (0)#define APPEND_BODY(last, body) do { \ if ((last)->direction & (SNAKE_UP | SNAKE_DOWN)) { \ (body)->x = (last)->x; \ if ((last)->direction & SNAKE_UP) \ (body)->y = (last)->y + snake->size; \ else \ (body)->y = (last)->y - snake->size; \ } else { \ if ((last)->direction & SNAKE_LEFT) \ (body)->x = (last)->x + snake->size; \ else \ (body)->x = (last)->x - snake->size; \ (body)->y = (last)->y; \ } \ (body)->direction = (last)->direction; \ (body)->next = (snake->body != 0) ? snake->body : 0; \ snake->body = (body); \ ++snake->length; \} while (0)#define REMOVE_BODY(body) do { \ snake->body = (body)->next; \ SDL_free(body); \ (body) = snake->body; \} while (body)Snake * SNK_CreateSnake(World *world, int size, int x, int y){ Snake *snake; if (world == 0) return 0; if ((snake = (Snake *)SDL_malloc(sizeof(Snake))) == 0) return 0; INIT_SNAKE(world, size, x, y); SNK_GrowSnake(snake); return snake;}void SNK_DestroySnake(Snake *snake){ struct Body *body; if (snake != 0) { if ((body = snake->body)) REMOVE_BODY(body); SDL_free(snake); snake = 0; }}void SNK_MoveSnake(Snake *snake){ struct Body *body; if (snake != 0) { MOVE_SNAKE(snake); for (body = snake->body; body; body = body->next) { MOVE_SNAKE(body); body->direction = (body->next != 0) ? body->next->direction : snake->direction; } }}void SNK_DrawSnake(Snake *snake){ SDL_Rect rect; struct Body *body; if (snake != 0) { rect.x = snake->x; rect.y = snake->y; rect.w = rect.h = snake->size; if (((snake->world != 0) ? (snake->world->render != 0) : 0)) { SDL_SetRenderDrawColor(snake->world->render, snake->color.r, snake->color.g, snake->color.b, snake->color.a); SDL_RenderDrawRect(snake->world->render, &rect); for (body = snake->body; body; body = body->next) { rect.x = body->x; rect.y = body->y; SDL_RenderDrawRect(snake->world->render, &rect); } } }}void SNK_GrowSnake(Snake *snake){ struct Body *body; if (snake != 0) { if ((body = (struct Body *)SDL_malloc(sizeof(struct Body))) == 0) return; if (snake->body == 0) { APPEND_BODY(snake, body); } else { APPEND_BODY(snake->body, body); } }}int SNK_HasIntersection(Snake *snake, SDL_Rect rect){ SDL_Rect bodyrect; struct Body *body; if (snake != 0) { bodyrect.w = bodyrect.h = snake->size; for (body = snake->body; body; body = body->next) { bodyrect.x = body->x; bodyrect.y = body->y; if (SDL_HasIntersection(&bodyrect, &rect) != 0) return 1; } } return 0;}int SNK_GetSnakeStatus(Snake *snake){ SDL_Rect headrect; if (((snake != 0) ? (snake->world != 0) : 0)) { headrect.w = (snake->x > 0 && snake->x < snake->world->w); headrect.h = (snake->y > 0 && snake->y < snake->world->h); if (headrect.w && headrect.h) { headrect.x = snake->x; headrect.y = snake->y; headrect.w = headrect.h = snake->size; if (SNK_HasIntersection(snake, headrect) != 0) return SNAKE_DIED; return SNAKE_MOVABLE; } else { switch (snake->direction) { case SNAKE_UP: headrect.x = (snake->y > 0); break; case SNAKE_DOWN: headrect.x = ((snake->y + snake->size) < snake->world->h); break; case SNAKE_LEFT: headrect.x = (snake->x > 0); break; case SNAKE_RIGHT: headrect.x = ((snake->x + snake->size) < snake->world->w); break; } return ((headrect.x != 0) ? SNAKE_MOVABLE : 0); } } return 0;}void SNK_SetSnakeSpeed(Snake *snake, int speed){ if (snake != 0) snake->speed = SDL_abs(speed);}void SNK_SetSnakeDirection(Snake *snake, int direction){ if (snake != 0 && (direction & (SNAKE_UP | SNAKE_DOWN | SNAKE_LEFT | SNAKE_RIGHT))) { if (snake->direction & (SNAKE_UP | SNAKE_DOWN)) { if (direction & (SNAKE_LEFT | SNAKE_RIGHT)) snake->direction = direction; } else { if (direction & (SNAKE_UP | SNAKE_DOWN)) snake->direction = direction; } }}void SNK_SetSnakeColor(Snake *snake, Uint8 r, Uint8 g, Uint8 b, Uint8 a){ if (snake != 0) { snake->color.r = r; snake->color.g = g; snake->color.b = b; snake->color.a = a; }}
In the macro INIT_SNAKE, snake-> x and snake-> y Repeat twice, mainly to align the location of the snake and the size of the snake.
APPEND_BODY is used to append a linked list node, and sets snake-> body to always point to the end of the snake to improve the efficiency of appending nodes.