由于食物是贪吃蛇游戏中最简单的一部分,而且和其他部分关联性不强,基本上是一个独立的部分,所以我打算先实现它。
我的想法是食物必须在世界中才能被创造出来,也就是说,先有世界再有食物,所以我得先判断世界是否存在,存在的话才可以创建食物。
Food * SNK_CreateFood(World *world, int size)
{
Food *food; if (world == ) return ; if ((food = (Food *)SDL_malloc(sizeof(Food))) == ) return ; INIT_FOOD(world, size); return food;
}
对于指针变量,我总是显式地将它和数值比较,这有助于避免某些隐藏的错误。宏INIT_FOOD用来初始化Food结构体,在最后的完整源码中可以看到定义。
由于food是用SDL_malloc函数分配的,所以销毁食物只要释放内存就可以了。在C语言中,释放后的指针并不为零, 一个好的习惯是:每次释放指针后,显式的将指针置为零。我总是会遵循这个习惯,除非释放指针后程序也立刻退出,否则我总是将释放后的指针置为零。
void SNK_DestroyFood(Food *food)
{
SDL_free(food);
food = ;
}
我对食物的美或丑没有任何概念,所以我这里所谓的食物仅仅只是一个矩形。在画食物这里可以有很多创意,画一个奇葩的食物可以令人耳目一新。不过我只力求简洁,意思到了就可以了。
void SNK_DrawFood(Food *food)
{
SDL_Rect rect; if (food != )
{
rect.x = food->x;
rect.y = food->y;
rect.w = rect.h = food->size; if (((food->world != ) ? (food->world->render != ) : ))
{
SDL_SetRenderDrawColor(food->world->render,
food->color.r, food->color.g,
food->color.b, food->color.a);
SDL_RenderDrawRect(food->world->render, &rect);
}
}
}
设置食物位置的函数定义的很简单,给一个食物和坐标就可以了。但是内部实现起来我还是做了一点判断和处理,为了保证两方面的可行性:1. 确保坐标是正数且在世界范围内。2. 食物坐标要为食物大小的整数倍。尽管不是整数倍也能行,不过我是打算将食物和蛇设置为相同大小的,如果不是整数倍,就会发生蛇还没吃完食物,食物就消失了的情况。
void SNK_SetFoodPosition(Food *food, int x, int y)
{
if (((food != ) ? (food->world != ) : ))
{
if (SDL_abs(x) > food->world->w)
x = food->world->w; if (SDL_abs(y) > food->world->h)
y = food->world->h; if (food->size != )
{
food->x = (SDL_abs(x) / food->size) * food->size;
food->y = (SDL_abs(y) / food->size) * food->size;
}
}
}
最后就是设置食物颜色了,这个很简单,依次赋值就行了。
void SNK_SetFoodColor(Food *food, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
{
if (food != )
{
food->color.r = r;
food->color.g = g;
food->color.b = b;
food->color.a = a;
}
}
以下是snk-food.c文件的完整内容:
#include "snk-food.h" #define INIT_FOOD(world, size) \
food->world = (world); \
food->x = food->y = ; \
food->size = (size) ? SDL_abs(size) : ; \
food->color.r = food->color.g = food->color.b = food->color.a = ; Food * SNK_CreateFood(World *world, int size)
{
Food *food; if (world == ) return ; if ((food = (Food *)SDL_malloc(sizeof(Food))) == ) return ; INIT_FOOD(world, size); return food;
} void SNK_DestroyFood(Food *food)
{
SDL_free(food);
food = ;
} void SNK_DrawFood(Food *food)
{
SDL_Rect rect; if (food != )
{
rect.x = food->x;
rect.y = food->y;
rect.w = rect.h = food->size; if (((food->world != ) ? (food->world->render != ) : ))
{
SDL_SetRenderDrawColor(food->world->render,
food->color.r, food->color.g,
food->color.b, food->color.a);
SDL_RenderDrawRect(food->world->render, &rect);
}
}
} void SNK_SetFoodPosition(Food *food, int x, int y)
{
if (((food != ) ? (food->world != ) : ))
{
if (SDL_abs(x) > food->world->w)
x = food->world->w; if (SDL_abs(y) > food->world->h)
y = food->world->h; if (food->size != )
{
food->x = (SDL_abs(x) / food->size) * food->size;
food->y = (SDL_abs(y) / food->size) * food->size;
}
}
} void SNK_SetFoodColor(Food *food, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
{
if (food != )
{
food->color.r = r;
food->color.g = g;
food->color.b = b;
food->color.a = a;
}
}