void put_food()
{
srand(time(0));
foodimage.x = 300;
foodimage.y = 133;
foodimage.w = 100;
foodimage.h = 133;
putfood.w = 100;
putfood.h = 133;
if (eat == false)//检测食物是不是被吃了
{
fx = rand() % 20 * 100;
fy = rand() % 11 * 100;
for (int i = 0; i < len; i++)
{
if (check_collision(node[0].rect, node[i+1].rect))
{
//蛇头和蛇身碰撞检测当然还有over函数没写完
}//如果食物投到墙上或者身体上重投
while (fx < 80 || fx > 1840 || fy < 80 || fy > 940
|| check_collision(node[i].rect, putfood))
{
fx = rand() % 20 * 100;
fy = rand() % 11 * 100;
}
}
}
putfood.x = fx;
putfood.y = fy;
eat = true;
SDL_BlitSurface(food, &foodimage, screen, &putfood);
}
只要蛇头碰到食物就卡住了也不退出

{
srand(time(0));
foodimage.x = 300;
foodimage.y = 133;
foodimage.w = 100;
foodimage.h = 133;
putfood.w = 100;
putfood.h = 133;
if (eat == false)//检测食物是不是被吃了
{
fx = rand() % 20 * 100;
fy = rand() % 11 * 100;
for (int i = 0; i < len; i++)
{
if (check_collision(node[0].rect, node[i+1].rect))
{
//蛇头和蛇身碰撞检测当然还有over函数没写完
}//如果食物投到墙上或者身体上重投
while (fx < 80 || fx > 1840 || fy < 80 || fy > 940
|| check_collision(node[i].rect, putfood))
{
fx = rand() % 20 * 100;
fy = rand() % 11 * 100;
}
}
}
putfood.x = fx;
putfood.y = fy;
eat = true;
SDL_BlitSurface(food, &foodimage, screen, &putfood);
}
只要蛇头碰到食物就卡住了也不退出
