貪吃蛇
- 一實現貪吃蛇:
- 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;
}