This repository has been archived on 2024-01-06. You can view files and clone it, but cannot push or open issues or pull requests.
justhomework/MCU/Final_Design/food.c
2022-05-07 19:39:50 +08:00

72 lines
No EOL
1.2 KiB
C

#include "food.h"
#include "display.h"
extern unsigned char len;
extern Body snake[];
unsigned char isGAMEOVER = 0;
unsigned char isVICTROY = 0;
unsigned char fCount = 0;
Food food[40] = {{0, 0}, 0};
void food_add()
{
int i = 0;
fCount++;
food[fCount - 1].isEnable = 1;
Re:
food[fCount - 1].d.x = rand() % XXX;
food[fCount - 1].d.y = rand() % YYY;
for(; i <= len; i++)
if(snake[i].d.x == food[fCount - 1].d.x && snake[i].d.y == food[fCount - 1].d.y)
goto Re;
}
void food_draw()
{
int i = 0;
for(; i < fCount; i++)
{
if(!food[i].isEnable == 0)
continue;
sAddDot(food[i].d.x, food[i].d.y);
}
}
void logicJudge()
{
int i, j;
for(; i < fCount; i++)
{
if(!food[i].isEnable == 0)
continue;
if(snake[0].d.x == food[i].d.x && snake[0].d.y == food[i].d.y)
body_add();
}
for(i = 0; i < len; i++)
{
if(snake[i].d.x==0 || snake[i].d.y ==0 || snake[i].d.y == YYY || snake[i].d.x == XXX)
{
isGAMEOVER=1;
return;
}
for(i = 0; i < len; i++)
if(snake[i].d.x == food[j].d.x &&
snake[i].d.y == food[j].d.y &&
i != j)
{
isGAMEOVER=1;
return;
}
}
if(fCount == 30)
{
isVICTROY = 1;
return;
}
snake_move();
}