该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
#define UP 1
#define DN -1
#define RT 2
#define LT -2
#define DW 20
#define SPEED 150//速度
#include
#include
#include
typedef struct Body
{
int x;
int y;
int d;
int BodyColor;
struct Body * next;
}BODY;//定义蛇身结构体
typedef struct
{
int x;
int y;
}FOOD;//定义食物结构体
bool findover();//判断是否死亡
void findlong();//判断是否吃到食物
void snkmove();//移动
void drawsnk();//绘图
void setnewsed();//放置新食物
bool rep();//判断食物是否与蛇身重复
FOOD food;
BODY head;
BODY last;
void main()
{
srand((unsigned)time(NULL));
char mv='d';
char mm='a';
int a;
BODY *p;
initgraph(640, 480);
while(mm!='k')
{
if (_kbhit())
{
mm=getch();
}
if(mm!='k')
{
loadimage(NULL,"E:\\素材\\素材\\3\\11.jpg");Sleep(50);
loadimage(NULL,"E:\\素材\\素材\\3\\22.jpg");Sleep(50);
loadimage(NULL,"E:\\素材\\素材\\3\\33.jpg");Sleep(50);
loadimage(NULL,"E:\\素材\\素材\\3\\44.jpg");Sleep(50);
loadimage(NULL,"E:\\素材\\素材\\3\\55.jpg");Sleep(50);
loadimage(NULL,"E:\\素材\\素材\\3\\66.jpg");Sleep(50);
loadimage(NULL,"E:\\素材\\素材\\3\\77.jpg");Sleep(20);
loadimage(NULL,"E:\\素材\\素材\\3\\88.jpg");Sleep(20);
loadimage(NULL,"E:\\素材\\素材\\3\\99.jpg");Sleep(20);
loadimage(NULL,"E:\\素材\\素材\\3\\1010.jpg");Sleep(20);
}
}
//loadimage(NULL, _T("E:\\素材\\背景\\2.jpg"));//设置背景
do//食物初始化
{
food.x = rand() % (640/DW-1) * DW;
food.y = rand() % (480/DW) * DW;
} while (food.x < DW || food.y < DW || food.x == 320 || food.y == 240);//不与蛇身重合
head.x = 320; head.y = 240; head.d = 0; head.next = NULL; head.BodyColor = RGB(rand() % 256, rand() % 256, rand() % 256);//初始化蛇身
drawsnk();
while (true)
{
p =&head;
if (_kbhit())
{
a = p->d;
mv = getch();
switch (mv)//获取方向
{
case 'W':
case 'w':p->d = UP; break;
case 'S':
case 's':p->d = DN; break;
case 'A':
case 'a':p->d = LT; break;
case 'D':
case 'd':p->d = RT; break;
//case 'k':loadimg(NULL,);
case ' ':getch(); break;//暂停
}
if (p->next!=NULL)
if (a == -p->d)
p->d = -p ->d;
}
//loadimage(NULL,"E:\\图片\\2.jpg");
snkmove();
if ((findover())== true)//判断结果
{
mm='a';
while(mm!='k')
{
if (_kbhit())
{
mm=getch();
}
if(mm!='k')
{
loadimage(NULL, _T("E:\\素材\\素材\\4\\11.jpg"));Sleep(50);
loadimage(NULL, _T("E:\\素材\\素材\\4\\22.jpg"));Sleep(50);
loadimage(NULL, _T("E:\\素材\\素材\\4\\33.jpg"));Sleep(50);
loadimage(NULL, _T("E:\\素材\\素材\\4\\44.jpg"));Sleep(50);
loadimage(NULL, _T("E:\\素材\\素材\\4\\55.jpg"));Sleep(50);
loadimage(NULL, _T("E:\\素材\\素材\\4\\66.jpg"));Sleep(50);
loadimage(NULL, _T("E:\\素材\\素材\\4\\77.jpg"));Sleep(50);
loadimage(NULL, _T("E:\\素材\\素材\\4\\88.jpg"));Sleep(50);
}
}
return;
}
findlong();
cleardevice();
drawsnk();//贴图
Sleep(SPEED);//蛇的速度
}
getch();
}
bool findover()
{
bool flag = false;
BODY *p=&head;
if (p->x > (640-DW) || p->xy>(480-DW) || p->y < DW)//判断是否出界
return true;
else
{
while (p->next != NULL) //判断是否撞到身体
{
if (p!=&head)
if (p->x == head.x&&p->y == head.y)
{
flag = true;
break;
}
p = p->next;
}
if (head.next!=NULL)
if (p->x == head.x&&p->y == head.y)
flag = true;
return flag;
}
}
void findlong()
{
BODY *p;
p = &head;
if (head.x == food.x&&head.y == food.y)
{
while (p->next != NULL)
p = p->next;
p->next = (BODY *)malloc(sizeof(BODY));
p->next->next = NULL;
p->next->BodyColor = RGB(rand() % 256, rand() % 222, rand() % 111);
p->next->x = last.x;
p->next->y = last.y;
setnewsed();
}
}
void snkmove()
{
BODY *p,*m;
int x,x1, y,y1;
p = m = &head;
while (m->next != NULL)
m = m->next;
last.x = m->x;
last.y = m->y;
x1 = x = p->x;
y1 = y = p->y;
if (head.d == UP || head.d == DN)
{
if (head.d == UP)
head.y = head.y - DW;
else
head.y = head.y + DW;
}
else
{
if (head.d == RT)
head.x = head.x + DW;
if (head.d==LT)
head.x = head.x - DW;
}
if (head.next != NULL)
{
while (p->next->next != NULL)
{
x = p->next->x;
y = p->next->y;
p->next->x = x1;
p->next->y = y1;
p = p->next;
x1 = x;
y1 = y;
}
p->next->x =x1;
p->next->y = y1;
}
}
void drawsnk()
{
//IMAGE img;
BODY *p = &head;
setfillcolor(RGB(rand() % 50, rand() % 200, rand() % 100));//食物颜色
//solidrectangle(sed.x - (DW / 2), sed.y - (DW / 2), sed.x + (DW / 2), sed.y + (DW / 2));
fillcircle(food.x, food.y, (DW / 2));//食物
//loadimage(&img, _T("E:\\图片"), _T("1.jpg"));
//putimage(food.x, food.y, &img);
//loadimage(NULL, _T("E:\\图片\\1.jpg"));//有bug
while (p->next != NULL)
{
setfillcolor(RGB(rand() % 80, rand() % 166, rand() % 250));//蛇身子颜色
//fillcircle(p->x, p->y, (DW / 2));
solidrectangle(p->x - (DW / 2)+3, p->y - (DW / 2)+3, p->x + (DW / 2)-3, p->y + (DW / 2)-3);
p = p->next;
}
setfillcolor(RGB(rand() % 30, rand() %135, rand() % 220));
//fillcircle(p->x, p->y, (DW / 2));
solidrectangle(p->x - (DW / 2) + 3, p->y - (DW / 2) + 3, p->x + (DW / 2) - 3, p->y + (DW / 2) - 3);
}
void setnewsed()
{
do
{
food.x = rand() % (640 / DW - 1) * DW;//放置食物(/DW是为了对其坐标)
food.y = rand() % (480 / DW) * DW;
} while (food.x < DW || food.y
}
bool rep()
{
BODY *p;
p = &head;
do
{
if (p->x == food.x&&p->y == food.y)
return true;
p = p->next;
} while (p->next != NULL);
return false;
}