贪吃蛇
- 一实现贪吃蛇:
- 1.绘制网格:
- 1.绘制蛇:
- 3.控制蛇的默认移动向右:
- 4.控制蛇的移动方向:
- 5.生成食物
- 6.判断蛇吃到食物并且长大。
- 7.判断游戏结束:
- 8.重置函数:
- 二整体代码:
一实现贪吃蛇:
1.绘制网格:
#define wide 800
#define high 600#define Frame 20//1.绘制边框
void DrawFrame()
{setlinecolor(BLACK);setlinestyle(PS_SOLID, 1);for (int i = 1; i < wide / Frame; i++){line(Frame * i, 0, Frame * i, 600);}for (int i = 1; i < high / Frame; i++){line(0, Frame * i,800 , Frame * i);}
}
1.绘制蛇:
int main()
{initgraph(wide,high);setbkcolor(RGB(188, 227, 245));cleardevice();//初始化蛇的长度int length = 5;//初始化蛇的坐标:sn snake[5] = { {7,6} ,{6,6}, {5,6},{4,6},{3,6} };while (1){cleardevice();BeginBatchDraw();//1.绘制边框DrawFrame();//2.绘制蛇DrawSnake(snake,length);FlushBatchDraw();Sleep(40);}EndBatchDraw();getchar();closegraph();return 0;
}
//2.绘制蛇
void DrawSnake(sn* snake,int length)
{setfillcolor(GREEN);for (int i = 0; i < length; i++){//先绘制头int x1 = snake[i].x;int y1 = snake[i].y;solidrectangle(x1 * Frame, y1 * Frame, (x1 + 1) * Frame, (y1 + 1) * Frame);}
}
3.控制蛇的默认移动向右:
void SnakeMove(sn* snake, int length)
{//默认蛇一开始是一直向右移动的;sn newsnake = { (snake->x)+1,(snake->y)};//2.绘制蛇DrawSnake(snake, length);FlushBatchDraw();//进行蛇的移动;for (int i = length - 1; i >= 1; i--){snake[i] = snake[i - 1];}snake[0] = newsnake;
}
4.控制蛇的移动方向:
void SnakeMove(sn* snake, int length,int* vx,int* vy)
{//默认蛇一开始是一直向右移动的;//2.绘制蛇DrawSnake(snake, length);FlushBatchDraw();//进行蛇的移动;if (_kbhit()){char ch = _getch();//蛇是不可以直接走回头路的:switch (ch){case'A':case'a':if (*vx == 1 && vy == 0)break;*vx = -1;*vy = 0;break;case'W':case'w':if (*vx == 0 && *vy == 1)break;*vx = 0;*vy = -1;break;case'D':case'd':if (*vx == -1 && *vy == 0)break;*vx = 1;*vy = 0;break;case'S':case's':if (*vx == 0 && *vy == -1)break;*vx = 0;*vy = 1;break;}}//控制蛇节点变化sn newsnake = { (snake->x)+(*vx) ,(snake->y)+(*vy)};for (int i = length - 1; i >= 1; i--){snake[i] = snake[i - 1];}snake[0] = newsnake;
}
5.生成食物
1.食物不可以生成到画布的外面;
2.不可以生成在蛇的身体上面;
3.食物是随机生成的;
//4.生成食物:
void DrawFeeFood(sn* snake, int length, FD* food)
{ int fx = 0;int fy = 0;int flag = 0;while (1){//食物坐标的随机生成:food->x = rand() % (wide / Frame);food->y= rand() % (high / Frame);//2.不可以生成在蛇的身体上面,遍历蛇的身体。for (int i = 0; i < length; i++){if (((food->x) == (snake[i].x)) && ((food->y) == (snake[i].y))){flag++;}}if (flag == 0){break;}}
}
//3.控制蛇的移动
void SnakeMove(sn* snake, int length,FD* food,int* vx,int* vy)
{//默认蛇一开始是一直向右移动的;//2.绘制蛇DrawSnake(snake, length);//2.绘制食物:if (food->x == 0 && food->y == 0){DrawFeeFood(snake, length, food);}setfillcolor(YELLOW);solidrectangle((food->x) * Frame, (food->y) * Frame, (food->x+1) * Frame, (food->y+1) * Frame);FlushBatchDraw();//进行蛇的移动;if (_kbhit()){char ch = _getch();//蛇是不可以直接走回头路的:switch (ch){case'A':case'a':if (*vx == 1 && vy == 0)break;*vx = -1;*vy = 0;break;case'W':case'w':if (*vx == 0 && *vy == 1)break;*vx = 0;*vy = -1;break;case'D':case'd':if (*vx == -1 && *vy == 0)break;*vx = 1;*vy = 0;break;case'S':case's':if (*vx == 0 && *vy == -1)break;*vx = 0;*vy = 1;break;}}//控制蛇节点变化sn newsnake = { (snake->x)+(*vx) ,(snake->y)+(*vy)};for (int i = length - 1; i >= 1; i--){snake[i] = snake[i - 1];}snake[0] = newsnake;
}
6.判断蛇吃到食物并且长大。
//3.控制蛇的移动
void SnakeMove(sn* snake, int* length,FD* food,int* vx,int* vy)
{//默认蛇一开始是一直向右移动的;//2.绘制蛇DrawSnake(snake, *length);//3.绘制食物:if (food->x == 0 && food->y == 0){DrawFeeFood(snake, length, food);}setfillcolor(YELLOW);solidrectangle((food->x) * Frame, (food->y) * Frame, (food->x+1) * Frame, (food->y+1) * Frame);FlushBatchDraw();//进行蛇的移动;if (_kbhit()){char ch = _getch();//蛇是不可以直接走回头路的:switch (ch){case'A':case'a':if (*vx == 1 && vy == 0)break;*vx = -1;*vy = 0;break;case'W':case'w':if (*vx == 0 && *vy == 1)break;*vx = 0;*vy = -1;break;case'D':case'd':if (*vx == -1 && *vy == 0)break;*vx = 1;*vy = 0;break;case'S':case's':if (*vx == 0 && *vy == -1)break;*vx = 0;*vy = 1;break;}}//控制蛇节点变化sn newsnake = { (snake->x)+(*vx) ,(snake->y)+(*vy)};for (int i = *length - 1; i >= 1; i--){snake[i] = snake[i - 1];}snake[0] = newsnake;//判断蛇是否吃到了食物和生长:Grow(snake, length, food);
}
void Grow(sn* snake, int* length, FD* food)
{//保存一下尾的数值sn end = snake[(*length) - 1];if ((snake[0].x == food->x) && (snake[0].y == food->y)){//重置食物数值food->x = 0;food->y = 0;//添加尾节点;*length = (*length) + 1;snake[*length] = end;}
}
7.判断游戏结束:
1.蛇头碰到墙壁:
2.蛇头碰到蛇身体:
//5.判断游戏结束
bool Gameover(sn* snake, int* length)
{//1.碰到墙壁if ((snake[0].x >= wide / Frame) || (snake[0].x < 0) || (snake[0].y >= high / Frame) || (snake[0].y < 0)){return true;}else{//2.碰到身体for (int i = 1; i < (*length); i++){if (((snake[0].x)==(snake[i].x))&&((snake[0].y) == (snake[i].y)))return true;}}return false;
}
8.重置函数:
//初始化函数
void initgame(int* length,int* vx,int* vy,sn* snake,FD* food)
{//初始化蛇的长度*length = 5;//初始化蛇的水平初始速度*vx = 1;*vy = 0;//初始化蛇的坐标:snake[0] = { 7,6 };snake[0] = { 6,6 };snake[0] = { 5,6 };snake[0] = { 4,6 };snake[0] = { 3,6 };//初始化食物的坐标;*food = { 0,0 };
}
二整体代码:
#define _CRT_SECURE_NO_WARNINGS 1#include<stdio.h>
#include<easyx.h>
#include<math.h>
#include<stdbool.h>
#include<conio.h>
#include<time.h>#define wide 800
#define high 600
#define Frame 20typedef struct snake {int x;int y;
}sn;
typedef struct Food {int x;int y;
}FD;
//1.绘制边框
void DrawFrame()
{setlinecolor(BLACK);setlinestyle(PS_SOLID, 1);for (int i = 1; i < wide / Frame; i++){line(Frame * i, 0, Frame * i, 600);}for (int i = 1; i < high / Frame; i++){line(0, Frame * i,800 , Frame * i);}
}//2.绘制蛇
void DrawSnake(sn* snake,int length)
{setfillcolor(GREEN);for (int i = 0; i < length; i++){//先绘制头int x1 = snake[i].x;int y1 = snake[i].y;solidrectangle(x1 * Frame, y1 * Frame, (x1 + 1) * Frame, (y1 + 1) * Frame);}
}//3.生成食物:
void DrawFeeFood(sn* snake, int *length, FD* food)
{ int fx = 0;int fy = 0;int flag = 0;while (1){//食物坐标的随机生成:food->x = rand() % (wide / Frame);food->y= rand() % (high / Frame);//2.不可以生成在蛇的身体上面,遍历蛇的身体。for (int i = 0; i < *length; i++){if (((food->x) == (snake[i].x)) && ((food->y) == (snake[i].y))){flag++;}}if (flag == 0){break;}}
}//4判断蛇吃到食物并且长大。
void Grow(sn* snake, int* length, FD* food)
{//保存一下尾的数值sn end = snake[(*length) - 1];if ((snake[0].x == food->x) && (snake[0].y == food->y)){//重置食物数值food->x = 0;food->y = 0;//添加尾节点;*length = (*length) + 1;snake[*length] = end;}
}//初始化函数
void initgame(int* length,int* vx,int* vy,sn* snake,FD* food)
{//初始化蛇的长度*length = 5;//初始化蛇的水平初始速度*vx = 1;*vy = 0;//初始化蛇的坐标:snake[0] = { 7,6 };snake[0] = { 6,6 };snake[0] = { 5,6 };snake[0] = { 4,6 };snake[0] = { 3,6 };//初始化食物的坐标;*food = { 0,0 };
}
//5.判断游戏结束
bool Gameover(sn* snake, int* length)
{//1.碰到墙壁if ((snake[0].x >= wide / Frame) || (snake[0].x < 0) || (snake[0].y >= high / Frame) || (snake[0].y < 0)){return true;}else{//2.碰到身体for (int i = 1; i < (*length); i++){if (((snake[0].x)==(snake[i].x))&&((snake[0].y) == (snake[i].y)))return true;}}return false;
}//6.控制蛇的移动
void SnakeMove(sn* snake, int* length,FD* food,int* vx,int* vy)
{//默认蛇一开始是一直向右移动的;//2.绘制蛇DrawSnake(snake, *length);//3.绘制食物:if (food->x == 0 && food->y == 0){DrawFeeFood(snake, length, food);}setfillcolor(YELLOW);solidrectangle((food->x) * Frame, (food->y) * Frame, (food->x+1) * Frame, (food->y+1) * Frame);FlushBatchDraw();//进行蛇的移动;if (_kbhit()){char ch = _getch();//蛇是不可以直接走回头路的:switch (ch){case'A':case'a':if (*vx == 1 && vy == 0)break;*vx = -1;*vy = 0;break;case'W':case'w':if (*vx == 0 && *vy == 1)break;*vx = 0;*vy = -1;break;case'D':case'd':if (*vx == -1 && *vy == 0)break;*vx = 1;*vy = 0;break;case'S':case's':if (*vx == 0 && *vy == -1)break;*vx = 0;*vy = 1;break;}}//控制蛇节点变化sn newsnake = { (snake->x)+(*vx) ,(snake->y)+(*vy)};for (int i = *length - 1; i >= 1; i--){snake[i] = snake[i - 1];}snake[0] = newsnake;//判断蛇是否吃到了食物和生长:Grow(snake, length, food);//判断蛇是否碰到了墙壁和自己if (Gameover(snake, length)){initgame(length, vx, vy, snake, food);//Sleep(100);}}int main()
{initgraph(wide,high);setbkcolor(RGB(188, 227, 245));//获取当前时间作为随机数种子:srand((unsigned int)time(NULL));cleardevice();//初始化蛇的长度int length = 5;//初始化蛇的水平初始速度int vx = 1;int vy = 0;//初始化蛇的坐标:sn snake[100] = { {7,6} ,{6,6}, {5,6},{4,6},{3,6} };//初始化食物的坐标;FD food = {0,0};while (1){cleardevice();BeginBatchDraw();//1.绘制边框DrawFrame();//2.控制蛇的移动和节点的变化;SnakeMove(snake, &length,&food,&vx,&vy);Sleep(150);}EndBatchDraw();getchar();closegraph();return 0;
}