您的位置:首页 > 数码常识数码常识
c语言的贪吃蛇源代码(c语言编写贪吃蛇程序代码)
2025-05-10人已围观
c语言的贪吃蛇源代码(c语言编写贪吃蛇程序代码)
序言很多学习了C语言的小伙伴,虽然说学完了C语言入门,但是实际能力还是停留在一个很低的水平,基本上就是套几个for循环,暴力解一下排列组合问题的水平,很多人基本上不能够独立写一个小程序,今天就给大家我很久以前做的一个简单的贪吃蛇案例。
c语言编写贪吃蛇程序代码序言
这次的编写控制台贪吃蛇程序对学完C语言没做过项目的小伙伴来说可能是一个不小的挑战。
本文的贪吃蛇案例用的东西也并不是很多,游戏的实现主要是对一个二维数组按一定逻辑进行修改、变换(实际操作时,为了减少闪烁,我用的是字符串)。这里不对编写过程进行赘述,主要说一下最基本功能的逻辑、和一些之前较少用的函数等。
一、 基本功能逻辑
1、游戏的背景、打印
定义一个二维字符串,用“”和空格表示边界、蛇身、空白等。打印是用for循环遍历整个字符串,并以一定频率刷新,就可以达到游戏效果。
2、建立蛇数组
考虑到没用链表做过东西,不太熟练,我采用了数组来做蛇。数组主要有容量有限,最长长度需要先定义(只要我定的足够长hhhh),以及很多地方需要取地址(N次打掉了”&“)等缺点。数组存储蛇的节数、XY坐标、移动方向等参数。主要需要注意“”占两个字节,在写坐标时很多地方要乘二。
3、生成蛇的随机坐标
首先种随机种子,采用系统时间做种子。定义x、y两个变量作为坐标值,用rand()函数搭配取余来获得想要的坐标值范围。然后初始生成两三节就可以了。
4、把蛇画到地图上
建立for循环遍历整条蛇,利用strncpy()函数将空白部分复制为“”就行了。
5、蛇的运动
这里卡了比较久,期间去玩了玩贪吃蛇,发现蛇的运动方式不是很复杂,可以说就是蛇尾去一个,蛇头加一个。我采用了整个蛇身向前移,蛇头单独处理的方法,这样也便于以后控制方向。
6、擦除运动轨迹
写到上一步运行会发现蛇越来越长。。。。就像死机了以后的鼠标光标一样。。。。是因为虽然前一节点的属性赋给了后一个节点,但是这个节点并没有变。所以在每次运动前把之前的蛇擦掉,方法同第四步,只是把“”换成两个空格。
7、蛇改变方向
由于蛇运动方式的特殊性,只需要对蛇头处理。用GetAsyncKeyState()函数读取键盘输入,并需要注意通过附加条件防止蛇掉头。
8、生成食物
随机坐标、复制、打印。
9、蛇吃食物长长
蛇运动到食物的地方会把食物覆盖掉,所以吃掉食物的效果不用写。只用判断蛇头坐标和食物坐标重合,然后判断运动方向来确定在哪里加一节就行了。然后用一个布尔值判断场上是否还有食物,来生成新的食物。计分也可以在此处写。
网络效果图
代码如下:
#define _CRT_SECURE_NO_WARNINGS 1#include <stdio.h>#include <stdlib.h>#include <math.h>#include <conio.h>#include <time.h>#include <windows.h>#define MAXWIDTH 30#define MAXHEIGHT 30#define INITLEN 3 //贪吃蛇的初始长度 struct{
char *ch; int color; char type;
}
charBorder={ "", 4, 1 }, //边框charBg={ "", 2, 2 }, //背景charSnake={ "", 0xe, 3 }, //贪吃蛇节点charFood={ "", 0xc, 4 }; //食物//用一个结构体数组保存地图中的各个点struct{
char type; int index;
}globalMap[MAXWIDTH][MAXHEIGHT];struct{
int x; int y;
} snakeMap[(MAXWIDTH - 2)*(MAXHEIGHT - 2)], scoresPostion;int scores=0; //得分int snakeMapLen=(MAXWIDTH - 2)*(MAXHEIGHT - 2);int headerIndex, tailIndex;
HANDLE hStdin;
// 设置光标位置,x为行,y为列void setPosition(int x, int y){
COORD coord;
coord.X=2 * y;
coord.Y=x;
SetConsoleCursorPosition(hStdin, coord);
}// 设置颜色void setColor(int color){
SetConsoleTextAttribute(hStdin, color);
}//创建食物void createFood(){ int index, rang, x, y;
srand((unsigned)time(NULL)); if (tailIndex<headerIndex){
rang=headerIndex - tailIndex - 1;
index=rand() % rang + tailIndex + 1;
} else{
rang=snakeMapLen - (tailIndex - headerIndex + 1);
index=rand() % rang; if (index >=headerIndex){
index +=(tailIndex - headerIndex + 1);
}
}
x=snakeMap[index].x;
y=snakeMap[index].y;
setPosition(x, y);
setColor(charFood.color); printf("%s", charFood.ch);
globalMap[x][y].type=charFood.type;
}//死了void die(){ int xCenter=MAXHEIGHT % 2==0 ? MAXHEIGHT / 2 : MAXHEIGHT / 2 + 1; int yCenter=MAXWIDTH % 2==0 ? MAXWIDTH / 2 : MAXWIDTH / 2 + 1;
setPosition(xCenter, yCenter - 5);
setColor(0xC); exit(1);
_getch(); exit(0);
}// 蛇移动void move(char direction){ int newHeaderX, newHeaderY; //新蛇头的坐标
int newHeaderPreIndex; //新蛇头坐标以前对应的索引
int newHeaderPreX, newHeaderPreY; //新蛇头的索引以前对应的坐标
int newHeaderPreType; //新蛇头以前的类型
int oldTailX, oldTailY; //老蛇尾坐标
switch (direction){ case 'w':
newHeaderX=snakeMap[headerIndex].x - 1;
newHeaderY=snakeMap[headerIndex].y; break; case 's':
newHeaderX=snakeMap[headerIndex].x + 1;
newHeaderY=snakeMap[headerIndex].y; break; case 'a':
newHeaderX=snakeMap[headerIndex].x;
newHeaderY=snakeMap[headerIndex].y - 1; break; case 'd':
newHeaderX=snakeMap[headerIndex].x;
newHeaderY=snakeMap[headerIndex].y + 1; break;
}
headerIndex=headerIndex==0 ? snakeMapLen - 1 : headerIndex - 1;
newHeaderPreIndex=globalMap[newHeaderX][newHeaderY].index;
newHeaderPreX=snakeMap[headerIndex].x;
newHeaderPreY=snakeMap[headerIndex].y;
snakeMap[headerIndex].x=newHeaderX;
snakeMap[headerIndex].y=newHeaderY;
globalMap[newHeaderX][newHeaderY].index=headerIndex;
snakeMap[newHeaderPreIndex].x=newHeaderPreX;
snakeMap[newHeaderPreIndex].y=newHeaderPreY;
globalMap[newHeaderPreX][newHeaderPreY].index=newHeaderPreIndex; //新蛇头以前的类型
newHeaderPreType=globalMap[newHeaderX][newHeaderY].type; //设置新蛇头类型
globalMap[newHeaderX][newHeaderY].type=charSnake.type; // 判断是否出界或撞到自己
if (newHeaderPreType==charBorder.type || newHeaderPreType==charSnake.type){
die();
} //输出新蛇头
setPosition(newHeaderX, newHeaderY);
setColor(charSnake.color); printf("%s", charSnake.ch); //判断是否吃到食物
if (newHeaderPreType==charFood.type){ //吃到食物
createFood(); //更改分数
setPosition(scoresPostion.x, scoresPostion.y); printf("%d", ++scores);
} else{ //老蛇尾坐标
oldTailX=snakeMap[tailIndex].x;
oldTailY=snakeMap[tailIndex].y; //删除蛇尾
setPosition(oldTailX, oldTailY);
setColor(charBg.color); printf("%s", charBg.ch);
globalMap[oldTailX][oldTailY].type=charBg.type;
tailIndex=(tailIndex==0) ? snakeMapLen - 1 : tailIndex - 1;
}
}//下次移动的方向char nextDirection(char ch, char directionOld){ int sum=ch + directionOld;
ch=tolower(ch); if ((ch=='w' || ch=='a' || ch=='s' || ch=='d') && sum !=197 && sum !=234){ return ch;
} else{ return directionOld;
}
}//暂停char pause(){ return _getch();
}// 初始化void init(){ // 设置相关变量
int x, y, index; int xCenter=MAXHEIGHT % 2==0 ? MAXHEIGHT / 2 : MAXHEIGHT / 2 + 1; int yCenter=MAXWIDTH % 2==0 ? MAXWIDTH / 2 : MAXWIDTH / 2 + 1;
CONSOLE_CURSOR_INFO cci; //控制台光标信息
//判断相关设置是否合理
if (MAXWIDTH<16){ printf("'MAXWIDTH' is too small!");
_getch(); exit(0);
} //设置窗口大小
system("mode con: cols=96 lines=32"); //隐藏光标
hStdin=GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleCursorInfo(hStdin, &cci);
cci.bVisible=0;
SetConsoleCursorInfo(hStdin, &cci); //打印背景
for (x=0; x<MAXHEIGHT; x++){ for (y=0; y<MAXWIDTH; y++){ if (y==0 || y==MAXWIDTH - 1 || x==0 || x==MAXHEIGHT - 1){
globalMap[x][y].type=charBorder.type;
setColor(charBorder.color); printf("%s", charBorder.ch);
} else{
index=(x - 1)*(MAXWIDTH - 2) + (y - 1);
snakeMap[index].x=x;
snakeMap[index].y=y;
globalMap[x][y].type=charBg.type;
globalMap[x][y].index=index;
setColor(charBg.color); printf("%s", charBg.ch);
}
} printf("n");
} //初始化贪吃蛇
globalMap[xCenter][yCenter - 1].type=globalMap[xCenter][yCenter].type=globalMap[xCenter][yCenter + 1].type=charSnake.type;
headerIndex=(xCenter - 1)*(MAXWIDTH - 2) + (yCenter - 1) - 1;
tailIndex=headerIndex + 2;
setPosition(xCenter, yCenter - 1);
setColor(charSnake.color); for (y=yCenter - 1; y <=yCenter + 1; y++){ printf("%s", charSnake.ch);
} //生成食物
createFood(); //设置程序信息
setPosition(xCenter - 1, MAXWIDTH + 2); printf(" 得分 : 0");
setPosition(xCenter, MAXWIDTH + 2); printf(" 姓名班级 :33班杨超");
scoresPostion.x=xCenter - 1;
scoresPostion.y=MAXWIDTH + 8;
}int main(){ char charInput, direction='a';
init();
charInput=tolower(_getch());
direction=nextDirection(charInput, direction); while (1){ if (_kbhit()){
charInput=tolower(_getch()); if (charInput==' '){
charInput=pause();
}
direction=nextDirection(charInput, direction);
}
move(direction);
Sleep(500);
}
_getch(); return 0;
}
希望对大家有帮助~
上面就是小居数码小编今天给大家介绍的关于(c语言编写贪吃蛇程序代码)的全部内容,希望可以帮助到你,想了解更多关于数码知识的问题,欢迎关注我们,并收藏,转发,分享。
94%的朋友还想知道的:
c语言里面void和main(main函数前为什么有void)
热门的应用程序开发编程语言盘点(应用程序开发编程语言有哪些)
会c语言学java难吗(c语言与java难度)
哪种编程语言最赚钱(世界上最难的5种编程语言)
153045
序言很多学习了C语言的小伙伴,虽然说学完了C语言入门,但是实际能力还是停留在一个很低的水平,基本上就是套几个for循环,暴力解一下排列组合问题的水平,很多人基本上不能够独立写一个小程序,今天就给大家我很久以前做的一个简单的贪吃蛇案例。
c语言编写贪吃蛇程序代码序言
这次的编写控制台贪吃蛇程序对学完C语言没做过项目的小伙伴来说可能是一个不小的挑战。
本文的贪吃蛇案例用的东西也并不是很多,游戏的实现主要是对一个二维数组按一定逻辑进行修改、变换(实际操作时,为了减少闪烁,我用的是字符串)。这里不对编写过程进行赘述,主要说一下最基本功能的逻辑、和一些之前较少用的函数等。
一、 基本功能逻辑
1、游戏的背景、打印
定义一个二维字符串,用“”和空格表示边界、蛇身、空白等。打印是用for循环遍历整个字符串,并以一定频率刷新,就可以达到游戏效果。
2、建立蛇数组
考虑到没用链表做过东西,不太熟练,我采用了数组来做蛇。数组主要有容量有限,最长长度需要先定义(只要我定的足够长hhhh),以及很多地方需要取地址(N次打掉了”&“)等缺点。数组存储蛇的节数、XY坐标、移动方向等参数。主要需要注意“”占两个字节,在写坐标时很多地方要乘二。
3、生成蛇的随机坐标
首先种随机种子,采用系统时间做种子。定义x、y两个变量作为坐标值,用rand()函数搭配取余来获得想要的坐标值范围。然后初始生成两三节就可以了。
4、把蛇画到地图上
建立for循环遍历整条蛇,利用strncpy()函数将空白部分复制为“”就行了。
5、蛇的运动
这里卡了比较久,期间去玩了玩贪吃蛇,发现蛇的运动方式不是很复杂,可以说就是蛇尾去一个,蛇头加一个。我采用了整个蛇身向前移,蛇头单独处理的方法,这样也便于以后控制方向。
6、擦除运动轨迹
写到上一步运行会发现蛇越来越长。。。。就像死机了以后的鼠标光标一样。。。。是因为虽然前一节点的属性赋给了后一个节点,但是这个节点并没有变。所以在每次运动前把之前的蛇擦掉,方法同第四步,只是把“”换成两个空格。
7、蛇改变方向
由于蛇运动方式的特殊性,只需要对蛇头处理。用GetAsyncKeyState()函数读取键盘输入,并需要注意通过附加条件防止蛇掉头。
8、生成食物
随机坐标、复制、打印。
9、蛇吃食物长长
蛇运动到食物的地方会把食物覆盖掉,所以吃掉食物的效果不用写。只用判断蛇头坐标和食物坐标重合,然后判断运动方向来确定在哪里加一节就行了。然后用一个布尔值判断场上是否还有食物,来生成新的食物。计分也可以在此处写。
网络效果图
代码如下:
#define _CRT_SECURE_NO_WARNINGS 1#include <stdio.h>#include <stdlib.h>#include <math.h>#include <conio.h>#include <time.h>#include <windows.h>#define MAXWIDTH 30#define MAXHEIGHT 30#define INITLEN 3 //贪吃蛇的初始长度 struct{
char *ch; int color; char type;
}
charBorder={ "", 4, 1 }, //边框charBg={ "", 2, 2 }, //背景charSnake={ "", 0xe, 3 }, //贪吃蛇节点charFood={ "", 0xc, 4 }; //食物//用一个结构体数组保存地图中的各个点struct{
char type; int index;
}globalMap[MAXWIDTH][MAXHEIGHT];struct{
int x; int y;
} snakeMap[(MAXWIDTH - 2)*(MAXHEIGHT - 2)], scoresPostion;int scores=0; //得分int snakeMapLen=(MAXWIDTH - 2)*(MAXHEIGHT - 2);int headerIndex, tailIndex;
HANDLE hStdin;
// 设置光标位置,x为行,y为列void setPosition(int x, int y){
COORD coord;
coord.X=2 * y;
coord.Y=x;
SetConsoleCursorPosition(hStdin, coord);
}// 设置颜色void setColor(int color){
SetConsoleTextAttribute(hStdin, color);
}//创建食物void createFood(){ int index, rang, x, y;
srand((unsigned)time(NULL)); if (tailIndex<headerIndex){
rang=headerIndex - tailIndex - 1;
index=rand() % rang + tailIndex + 1;
} else{
rang=snakeMapLen - (tailIndex - headerIndex + 1);
index=rand() % rang; if (index >=headerIndex){
index +=(tailIndex - headerIndex + 1);
}
}
x=snakeMap[index].x;
y=snakeMap[index].y;
setPosition(x, y);
setColor(charFood.color); printf("%s", charFood.ch);
globalMap[x][y].type=charFood.type;
}//死了void die(){ int xCenter=MAXHEIGHT % 2==0 ? MAXHEIGHT / 2 : MAXHEIGHT / 2 + 1; int yCenter=MAXWIDTH % 2==0 ? MAXWIDTH / 2 : MAXWIDTH / 2 + 1;
setPosition(xCenter, yCenter - 5);
setColor(0xC); exit(1);
_getch(); exit(0);
}// 蛇移动void move(char direction){ int newHeaderX, newHeaderY; //新蛇头的坐标
int newHeaderPreIndex; //新蛇头坐标以前对应的索引
int newHeaderPreX, newHeaderPreY; //新蛇头的索引以前对应的坐标
int newHeaderPreType; //新蛇头以前的类型
int oldTailX, oldTailY; //老蛇尾坐标
switch (direction){ case 'w':
newHeaderX=snakeMap[headerIndex].x - 1;
newHeaderY=snakeMap[headerIndex].y; break; case 's':
newHeaderX=snakeMap[headerIndex].x + 1;
newHeaderY=snakeMap[headerIndex].y; break; case 'a':
newHeaderX=snakeMap[headerIndex].x;
newHeaderY=snakeMap[headerIndex].y - 1; break; case 'd':
newHeaderX=snakeMap[headerIndex].x;
newHeaderY=snakeMap[headerIndex].y + 1; break;
}
headerIndex=headerIndex==0 ? snakeMapLen - 1 : headerIndex - 1;
newHeaderPreIndex=globalMap[newHeaderX][newHeaderY].index;
newHeaderPreX=snakeMap[headerIndex].x;
newHeaderPreY=snakeMap[headerIndex].y;
snakeMap[headerIndex].x=newHeaderX;
snakeMap[headerIndex].y=newHeaderY;
globalMap[newHeaderX][newHeaderY].index=headerIndex;
snakeMap[newHeaderPreIndex].x=newHeaderPreX;
snakeMap[newHeaderPreIndex].y=newHeaderPreY;
globalMap[newHeaderPreX][newHeaderPreY].index=newHeaderPreIndex; //新蛇头以前的类型
newHeaderPreType=globalMap[newHeaderX][newHeaderY].type; //设置新蛇头类型
globalMap[newHeaderX][newHeaderY].type=charSnake.type; // 判断是否出界或撞到自己
if (newHeaderPreType==charBorder.type || newHeaderPreType==charSnake.type){
die();
} //输出新蛇头
setPosition(newHeaderX, newHeaderY);
setColor(charSnake.color); printf("%s", charSnake.ch); //判断是否吃到食物
if (newHeaderPreType==charFood.type){ //吃到食物
createFood(); //更改分数
setPosition(scoresPostion.x, scoresPostion.y); printf("%d", ++scores);
} else{ //老蛇尾坐标
oldTailX=snakeMap[tailIndex].x;
oldTailY=snakeMap[tailIndex].y; //删除蛇尾
setPosition(oldTailX, oldTailY);
setColor(charBg.color); printf("%s", charBg.ch);
globalMap[oldTailX][oldTailY].type=charBg.type;
tailIndex=(tailIndex==0) ? snakeMapLen - 1 : tailIndex - 1;
}
}//下次移动的方向char nextDirection(char ch, char directionOld){ int sum=ch + directionOld;
ch=tolower(ch); if ((ch=='w' || ch=='a' || ch=='s' || ch=='d') && sum !=197 && sum !=234){ return ch;
} else{ return directionOld;
}
}//暂停char pause(){ return _getch();
}// 初始化void init(){ // 设置相关变量
int x, y, index; int xCenter=MAXHEIGHT % 2==0 ? MAXHEIGHT / 2 : MAXHEIGHT / 2 + 1; int yCenter=MAXWIDTH % 2==0 ? MAXWIDTH / 2 : MAXWIDTH / 2 + 1;
CONSOLE_CURSOR_INFO cci; //控制台光标信息
//判断相关设置是否合理
if (MAXWIDTH<16){ printf("'MAXWIDTH' is too small!");
_getch(); exit(0);
} //设置窗口大小
system("mode con: cols=96 lines=32"); //隐藏光标
hStdin=GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleCursorInfo(hStdin, &cci);
cci.bVisible=0;
SetConsoleCursorInfo(hStdin, &cci); //打印背景
for (x=0; x<MAXHEIGHT; x++){ for (y=0; y<MAXWIDTH; y++){ if (y==0 || y==MAXWIDTH - 1 || x==0 || x==MAXHEIGHT - 1){
globalMap[x][y].type=charBorder.type;
setColor(charBorder.color); printf("%s", charBorder.ch);
} else{
index=(x - 1)*(MAXWIDTH - 2) + (y - 1);
snakeMap[index].x=x;
snakeMap[index].y=y;
globalMap[x][y].type=charBg.type;
globalMap[x][y].index=index;
setColor(charBg.color); printf("%s", charBg.ch);
}
} printf("n");
} //初始化贪吃蛇
globalMap[xCenter][yCenter - 1].type=globalMap[xCenter][yCenter].type=globalMap[xCenter][yCenter + 1].type=charSnake.type;
headerIndex=(xCenter - 1)*(MAXWIDTH - 2) + (yCenter - 1) - 1;
tailIndex=headerIndex + 2;
setPosition(xCenter, yCenter - 1);
setColor(charSnake.color); for (y=yCenter - 1; y <=yCenter + 1; y++){ printf("%s", charSnake.ch);
} //生成食物
createFood(); //设置程序信息
setPosition(xCenter - 1, MAXWIDTH + 2); printf(" 得分 : 0");
setPosition(xCenter, MAXWIDTH + 2); printf(" 姓名班级 :33班杨超");
scoresPostion.x=xCenter - 1;
scoresPostion.y=MAXWIDTH + 8;
}int main(){ char charInput, direction='a';
init();
charInput=tolower(_getch());
direction=nextDirection(charInput, direction); while (1){ if (_kbhit()){
charInput=tolower(_getch()); if (charInput==' '){
charInput=pause();
}
direction=nextDirection(charInput, direction);
}
move(direction);
Sleep(500);
}
_getch(); return 0;
}
希望对大家有帮助~
上面就是小居数码小编今天给大家介绍的关于(c语言编写贪吃蛇程序代码)的全部内容,希望可以帮助到你,想了解更多关于数码知识的问题,欢迎关注我们,并收藏,转发,分享。
94%的朋友还想知道的:
c语言里面void和main(main函数前为什么有void)
热门的应用程序开发编程语言盘点(应用程序开发编程语言有哪些)
会c语言学java难吗(c语言与java难度)
哪种编程语言最赚钱(世界上最难的5种编程语言)
153045
很赞哦! ()
上一篇:华为笔记本电脑怎么样(华为擎云S540商用笔记本体验)
下一篇:返回列表