一、需要掌握的知识:
C语言基础语法(结构体、指针、链表)、<windows.h>库、<stdlib.h>库、<time.h>库中的一些函数(不需要额外学习,本文后面会讲贪吃蛇需要用到的相关函数)
由于作者水平有限,我尽可能讲清楚,相关函数有不理解的地方还请大家发挥自习能力,查阅相关资料进行学习
下面就让我们一步步的实现贪吃蛇这个小游戏
二、具体实现
1.结构体
用来表示蛇与食物
typedef struct Node{int x;int y;Node* next;
}node;
2.头文件与全局变量
现在只需要大概浏览,下面讲函数不清楚的时候再回头看一下
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <time.h>node* head; //蛇头
node food; //食物
int reCreateFood; //判断食物是否被吃掉
int fail; //如果蛇头碰到蛇身或边界则失败
int score, add = 10; //定义分数和每个食物的分数
int dx[4] = {0, 0, -1, 1}, dy[4] = {-1, 1, 0, 0}; //(dx[0], dy[0])为(0, 1),即代表“上”
int d = -1; //表示蛇前进的方向,与上一行对应,0为上,1为下,2为左,3为右
//开局时初始化为-1,表示还未按方向键
3.Pos( )函数
这个函数的作用是改变控制台(黑框)光标位置
(别着急!看不懂的地方我都会解释!):
(看完解释还是比较模糊的只需要理解这个函数是用来改变控制台光标的即可)
Pos(10, 10);
printf("&");
上面的意思就是在控制台(黑框)的(10, 10)位置打印字符’&’
下面不想看的可以直接看下一个函数
函数定义如下
void Pos(int x, int y){COORD pos; //位置坐标 pos.X = x;pos.Y = y;HANDLE handleOutput = GetStdHandle(STD_OUTPUT_HANDLE); //获取输出句柄 SetConsoleCursorPosition(handleOutput, pos); //定位
}
先来看COORD
COORD是定义在<window.h>库里的结构体,定义如下:
typedef struct _COORD{short X;short Y;
}COORD;
大家只需要把COORD理解成**坐标(x, y)**即可
再看这句:
HANDLE handleOutput = GetStdHandle(STD_OUTPUT_HANDLE); //获取输出句柄
先来理解一下句柄:
句柄(Handle)是一种数据结构或对象,用于标识或引用其他对象或资源。它通常是一个整数或指针,用于在程序中唯一标识或操作某个对象。
句柄的作用是隐藏底层对象的具体细节,使得程序可以通过引用句柄来访问和操作对象,而无需了解对象的内部结构或实现方式。这样可以提高程序的模块性和灵活性,并且减少对对象的直接访问,从而提高系统的安全性和效率。
句柄有三种:输入句柄、输出句柄、错误句柄
这句话的作用就是获得一个输出句柄handleOutput
再看最后一句:
SetConsoleCursorPosition(handleOutput, pos); //定位
这个函数定义在<windows.h>库中,顾名思义,set(设置)console(控制台)cursor(光标)position(位置)
需要传递两个参数,一个是刚才获得的输出句柄,另一个是光标设置的坐标
4.border( )函数
这个函数的作用是打印边界,效果如下:

void border(){ //创建以(0, 0), (59, 25)为顶点的矩形边界for(int i = 0; i < 60; i ++ ){Pos(i, 0); //上行 printf("▇");Pos(i, 25); //下行 printf("▇"); } for(int i = 1; i < 25; i ++ ){Pos(0, i); //左列 printf("▇");Pos(59, i); //右列 printf("▇");}
}
5.getRandomPos( )函数
获得一个随机生成的坐标
void getRandomPos(int *x, int *y){srand((unsigned int)time(NULL)); //随着时间变化产生不同种子//保证坐标在边界内 *x = rand() % 58 + 1;*y = rand() % 24 + 1;
}
6.initSnake( )函数
实现蛇的初始化
void initSnake(){ head = (node*)malloc(sizeof(node));//保证在边界内head->x = rand() % 58 + 1; head->y = rand() % 24 + 1;head->next = NULL;Pos(head->x, head->y);//蛇用字符'*'表示printf("*");
}
7.inSnake( )函数
判断坐标(x, y)是否在蛇身上(食物不能创建在蛇身上/蛇头不能在蛇身上)
int inSnake(int x, int y){node* p = head->next;while(p){ //从头遍历蛇身 if(x == p->x && y == p->y)return 1;p = p->next;}return 0;
}
8.createFood( )函数
创建食物
void createFood(){int x, y;getRandomPos(&x, &y);if(inSnake(x, y)) createFood(); //重新生成food.x = x;food.y = y;Pos(x, y);printf("$");
}
9.getDirection( )函数
获取蛇前进的方向
int getDirection(){//不能往反方向走 if((GetAsyncKeyState(VK_UP) & 0x8000) && d != 1) d = 0;else if((GetAsyncKeyState(VK_DOWN) & 0x8000) && d != 0) d = 1;else if((GetAsyncKeyState(VK_LEFT) & 0x8000) && d != 3) d = 2;else if((GetAsyncKeyState(VK_RIGHT) & 0x8000) && d != 2) d = 3;return d;
}
GetAsyncKeyState(VK_UP) & 0x8000这个表达式的值为1的话表示按下了UP键
10.snakeMove( )函数
这是最核心的函数,用来控制蛇的移动
void snakeMove(){//刚开局未按方向键的情况 if(d == -1)while(d == -1) d = getDirection();//前进一格node* newHead = (node*)malloc(sizeof(node));newHead->x = head->x + dx[d];newHead->y = head->y + dy[d];newHead->next = head;head = newHead;//判断蛇头是否超出边界或者碰到蛇身node* p = head;if(inSnake(p->x, p->y) || (p->x == 0 || p->x == 59 || p->y == 0 || p->y == 25))fail = 1; //用fail记录蛇是否存活//没吃到食物的情况else if(p->x != food.x || p->y != food.y){//在蛇头打印字符'*'Pos(p->x, p->y);printf("*");//因为没吃到食物,蛇前进一个的话要把蛇尾的一格覆盖掉while(p->next->next) p = p->next;Pos(p->next->x, p->next->y);printf(" ");p->next = NULL;} //吃到食物的情况else{reCreateFood = 1; //食物被吃掉后需要再生成score += add; Pos(p->x, p->y);printf("*");}//这两行的作用除了打印得分,还是为了消除蛇尾闪烁的光标//读者可以把这两行注释掉进行对比Pos(61, 25);printf("您的得分为:%d", score);
}
11.welcome( )函数
创建欢迎界面
void welcome(){system("mode con cols=100 lines=30"); //设置控制台的大小为长100,宽30system("cls"); //清除控制台屏幕Pos(38,6);printf("welcome come to SnakeGame\n");Pos(38,8);printf("↑↓←→control direction\n");Pos(45,10);printf("ESC For Exit\n");Pos(42,12);printf("Enter For Begin\n");getchar(); //读取Enter字符进入游戏界面system("cls"); //清除控制台屏幕
}
效果如下:

12.主函数
终于到最后啦!加油!
int main(){welcome(); //欢迎界面 border(); //创建边界 initSnake(); //蛇的初始化 createFood(); //生成食物 //fail != 0就一直进行 while(1){if(fail) break;//食物被吃掉需要重新生成 if(reCreateFood){createFood();reCreateFood = 0;}snakeMove(); //蛇的移动 Sleep(500); //程序暂停500毫秒,注释掉运行一下就懂了//获取新的方向 d = getDirection(); }//这个也是注释掉对比一下就懂了 Pos(0, 24);printf("\n");return 0;
}
13.总代码
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <time.h>void welcome(); //欢迎界面
void border(); //创建边界
void Pos(int x, int y); //改变控制台光标位置
void getRandomPos(int *x, int *y); //获取随机坐标
void initSnake(); //蛇的初始化
void createFood(); //生成食物
int inSnake(int x, int y); //判断(x, y)是否在蛇上,1表示在,0表示不在
void snakeMove(); //蛇的移动
int getDirection(); //获取移动方向,0、1、2、3分别代表上、下、左、右 typedef struct Node{int x;int y;Node* next;
}node;//全局变量
node* head; //蛇头
node food; //食物
int reCreateFood; //判断食物是否被吃掉
int fail; //如果蛇头碰到蛇身或边界则失败
int score, add = 10; //定义分数和每个食物的分数
int dx[4] = {0, 0, -1, 1}, dy[4] = {-1, 1, 0, 0}; //(dx[0], dy[0])为(0, 1),即代表“上”
int d = -1; //表示蛇前进的方向,与上一行对应,0为上,1为下,2为左,3为右
//开局时初始化为-1,表示还未按方向键 int main(){welcome(); //欢迎界面 border(); //创建边界 initSnake(); //蛇的初始化 createFood(); //生成食物 //fail != 0就一直进行 while(1){if(fail) break;//食物被吃掉需要重新生成 if(reCreateFood){createFood();reCreateFood = 0;}snakeMove(); //蛇的移动 Sleep(500); //程序暂停500毫秒,注释掉运行一下就懂了//获取新的方向 d = getDirection(); }//这个也是注释掉对比一下就懂了 Pos(0, 24);printf("\n");return 0;
}void welcome(){system("mode con cols=100 lines=30");system("cls");Pos(38,6);printf("welcome come to SnakeGame\n");Pos(38,8);printf("↑↓←→control direction\n");Pos(45,10);printf("ESC For Exit\n");Pos(42,12);printf("Enter For Begin\n");getchar();system("cls");
}void border(){ //创建以(0, 0), (59, 25)为顶点的矩形边界for(int i = 0; i < 60; i ++ ){Pos(i, 0); //上行 printf("▇");Pos(i, 25); //下行 printf("▇"); } for(int i = 1; i < 25; i ++ ){Pos(0, i); //左列 printf("▇");Pos(59, i); //右列 printf("▇");}
}void Pos(int x, int y){COORD pos; //位置坐标 pos.X = x;pos.Y = y;HANDLE handleOutput = GetStdHandle(STD_OUTPUT_HANDLE); //获取输出句柄 SetConsoleCursorPosition(handleOutput, pos); //定位
}void getRandomPos(int *x, int *y){srand((unsigned int)time(NULL));*x = rand() % 58 + 1;*y = rand() % 24 + 1;
}void initSnake(){ head = (node*)malloc(sizeof(node));//保证在边界内head->x = rand() % 58 + 1; head->y = rand() % 24 + 1;head->next = NULL;Pos(head->x, head->y);printf("*");
}void createFood(){int x, y;getRandomPos(&x, &y);if(inSnake(x, y)) createFood(); //重新生成food.x = x;food.y = y;Pos(x, y);printf("$");
}int inSnake(int x, int y){node* p = head->next;while(p){ //从头遍历蛇身 if(x == p->x && y == p->y)return 1;p = p->next;}return 0;
} void snakeMove(){//刚开局未按方向键的情况 if(d == -1)while(d == -1) d = getDirection();node* newHead = (node*)malloc(sizeof(node));newHead->x = head->x + dx[d];newHead->y = head->y + dy[d];newHead->next = head;head = newHead;node* p = head;if(inSnake(p->x, p->y) || (p->x == 0 || p->x == 59 || p->y == 0 || p->y == 25))fail = 1;else if(p->x != food.x || p->y != food.y){Pos(p->x, p->y);printf("*");while(p->next->next) p = p->next;Pos(p->next->x, p->next->y);printf(" ");p->next = NULL;} else{reCreateFood = 1;score += add;Pos(p->x, p->y);printf("*");}Pos(61, 25);printf("您的得分为:%d", score);
}int getDirection(){//不能往反方向走 if((GetAsyncKeyState(VK_UP) & 0x8000) && d != 1) d = 0;else if((GetAsyncKeyState(VK_DOWN) & 0x8000) && d != 0) d = 1;else if((GetAsyncKeyState(VK_LEFT) & 0x8000) && d != 3) d = 2;else if((GetAsyncKeyState(VK_RIGHT) & 0x8000) && d != 2) d = 3; return d;
}
14.待改进的地方
- 改变蛇移动的快慢,相应改变食物的分数
- 没有把边界大小的参数写成宏
- 改变方向不灵敏
三.结语
以上就是贪吃蛇的基本内容啦,完结撒花~
下面有一些想说的话:
这篇文章的完成时间是2023.5.12,我现在是一名大一下的计科学生,其实C语言在去年十月份的中旬就看完了翁恺老师的网课,那个时候其实就有能力完成这个贪吃蛇的编写了,但是知道现在才写完,其实是走了很多弯路,耽误了不少时间
最后感谢北哥,在加入北哥的知识星球”编程指北“后才回正了自己的学习方向,也希望大家都能在正确的道路上越走越远~
















