完整!贪吃蛇游戏c语言代码分享(包括界面,计数,提示)

article/2025/10/23 10:57:45

最近看到身边有些朋友抱怨,网上的贪吃蛇项目完整源码太少,或者说运行不了,甚至还有花钱购买的情况,作为你们的过来人,学长了解你们期末考试的苦,今天的博客我就不啰嗦,我直接放代码给你们。
所有功能都实现了,欢迎测试。

#include<stdio.h>#include<time.h>#include<windows.h>#include<stdlib.h>#define U 1#define D 2#define L 3 #define R 4 //蛇的状态,U:上 ;D:下;L:左 R:右typedef struct SNAKE //蛇身的一个节点{int x;int y;struct SNAKE *next;}snake;//全局变量//int score=0,add=10;//总得分与每次吃食物得分。int status,sleeptime=200;//每次运行的时间间隔snake *head, *food;//蛇头指针,食物指针snake *q;//遍历蛇的时候用到的指针int endgamestatus=0; //游戏结束的情况,1:撞到墙;2:咬到自己;3:主动退出游戏。//声明全部函数//void Pos();void creatMap();void initsnake();int biteself();void createfood();void cantcrosswall();void snakemove();void pause();void gamecircle();void welcometogame();void endgame();void gamestart();void Pos(int x,int y)//设置光标位置{COORD pos;HANDLE hOutput;pos.X=x;pos.Y=y;hOutput=GetStdHandle(STD_OUTPUT_HANDLE);SetConsoleCursorPosition(hOutput,pos);}void creatMap()//创建地图{int i;for(i=0;i<58;i+=2)//打印上下边框{Pos(i,0);printf("■");Pos(i,26);printf("■");}for(i=1;i<26;i++)//打印左右边框{Pos(0,i);printf("■"); Pos(56,i);printf("■"); }}void initsnake()//初始化蛇身{snake *tail;int i;tail=(snake*)malloc(sizeof(snake));//从蛇尾开始,头插法,以x,y设定开始的位置//tail->x=24;tail->y=5;tail->next=NULL;for(i=1;i<=4;i++){head=(snake*)malloc(sizeof(snake));head->next=tail;head->x=24+2*i;head->y=5;tail=head;}while(tail!=NULL)//从头到为,输出蛇身{Pos(tail->x,tail->y);printf("■");tail=tail->next;}}int biteself()//判断是否咬到了自己{snake *self;self=head->next;while(self!=NULL){if(self->x==head->x && self->y==head->y){return 1;}self=self->next;}return 0;}void createfood()//随机出现食物{snake *food_1;srand((unsigned)time(NULL));food_1=(snake*)malloc(sizeof(snake));while((food_1->x%2)!=0) //保证其为偶数,使得食物能与蛇头对其{food_1->x=rand()%52+2;}food_1->y=rand()%24+1;q=head;while(q->next==NULL){if(q->x==food_1->x && q->y==food_1->y) //判断蛇身是否与食物重合{free(food_1);createfood();}q=q->next;}Pos(food_1->x,food_1->y);food=food_1;printf("■");}void cantcrosswall()//不能穿墙{ if(head->x==0 || head->x==56 ||head->y==0 || head->y==26){endgamestatus=1;endgame();}}void snakemove()//蛇前进,上U,下D,左L,右R{snake * nexthead;cantcrosswall();nexthead=(snake*)malloc(sizeof(snake));if(status==U){nexthead->x=head->x;nexthead->y=head->y-1;if(nexthead->x==food->x && nexthead->y==food->y)//如果下一个有食物//{nexthead->next=head;head=nexthead;q=head;while(q!=NULL){Pos(q->x,q->y);printf("■");q=q->next;}score=score+add;createfood();}else //如果没有食物//{nexthead->next=head;head=nexthead;q=head;while(q->next->next!=NULL){Pos(q->x,q->y);printf("■");q=q->next; }Pos(q->next->x,q->next->y);printf(" ");free(q->next);q->next=NULL;}}if(status==D){nexthead->x=head->x;nexthead->y=head->y+1;if(nexthead->x==food->x && nexthead->y==food->y) //有食物{nexthead->next=head;head=nexthead;q=head;while(q!=NULL){Pos(q->x,q->y);printf("■");q=q->next;}score=score+add;createfood();}else //没有食物{nexthead->next=head;head=nexthead;q=head;while(q->next->next!=NULL){Pos(q->x,q->y);printf("■");q=q->next; }Pos(q->next->x,q->next->y);printf(" ");free(q->next);q->next=NULL;}}if(status==L){nexthead->x=head->x-2;nexthead->y=head->y;if(nexthead->x==food->x && nexthead->y==food->y)//有食物{nexthead->next=head;head=nexthead;q=head;while(q!=NULL){Pos(q->x,q->y);printf("■");q=q->next;}score=score+add;createfood();}else //没有食物{nexthead->next=head;head=nexthead;q=head;while(q->next->next!=NULL){Pos(q->x,q->y);printf("■");q=q->next; }Pos(q->next->x,q->next->y);printf(" ");free(q->next);q->next=NULL;}}if(status==R){nexthead->x=head->x+2;nexthead->y=head->y;if(nexthead->x==food->x && nexthead->y==food->y)//有食物{nexthead->next=head;head=nexthead;q=head;while(q!=NULL){Pos(q->x,q->y);printf("■");q=q->next;}score=score+add;createfood();}else //没有食物{nexthead->next=head;head=nexthead;q=head;while(q->next->next!=NULL){Pos(q->x,q->y);printf("■");q=q->next; }Pos(q->next->x,q->next->y);printf(" ");free(q->next);q->next=NULL;}}if(biteself()==1) //判断是否会咬到自己{endgamestatus=2;endgame();}}void pause()//暂停{while(1){Sleep(300);if(GetAsyncKeyState(VK_SPACE)){break;}}}void gamecircle()//控制游戏 {Pos(64,15);printf("不能穿墙,不能咬到自己\n");Pos(64,16);printf("用↑.↓.←.→分别控制蛇的移动.");Pos(64,17);printf("F1 为加速,F2 为减速\n");Pos(64,18);printf("ESC :退出游戏.space:暂停游戏.");Pos(64,20);printf("我的贪吃蛇游戏");status=R;while(1){Pos(64,10);printf("得分:%d ",score);Pos(64,11);printf("每个食物得分:%d分",add);if(GetAsyncKeyState(VK_UP) && status!=D){status=U;}else if(GetAsyncKeyState(VK_DOWN) && status!=U){status=D;}else if(GetAsyncKeyState(VK_LEFT)&& status!=R){status=L;}else if(GetAsyncKeyState(VK_RIGHT)&& status!=L){status=R;}else if(GetAsyncKeyState(VK_SPACE)){pause();}else if(GetAsyncKeyState(VK_ESCAPE)){endgamestatus=3;break;}else if(GetAsyncKeyState(VK_F1)){if(sleeptime>=50){sleeptime=sleeptime-30;add=add+2;if(sleeptime==320){add=2;//防止减到1之后再加回来有错}}}else if(GetAsyncKeyState(VK_F2)){if(sleeptime<350){sleeptime=sleeptime+30;add=add-2;if(sleeptime==350){add=1; //保证最低分为1}}}Sleep(sleeptime);snakemove();}}void welcometogame()//开始界面{Pos(40,12);system("title 贪吃蛇游戏");printf("欢迎来到贪食蛇游戏!");Pos(40,25);system("pause");system("cls");Pos(25,12);printf("用↑.↓.←.→分别控制蛇的移动, F1 为加速,2 为减速\n");Pos(25,13);printf("加速将能得到更高的分数。\n");system("pause");system("cls");}void endgame()//结束游戏{system("cls");Pos(24,12);if(endgamestatus==1){printf("对不起,您撞到墙了。游戏结束.");}else if(endgamestatus==2){printf("对不起,您咬到自己了。游戏结束.");}else if(endgamestatus==3){printf("您的已经结束了游戏。");}Pos(24,13);printf("您的得分是%d\n",score);exit(0);}void gamestart()//游戏初始化{system("mode con cols=100 lines=30");welcometogame();creatMap();initsnake();createfood();}int main(){gamestart();gamecircle();endgame();return 0;}

运行结果:
在这里插入图片描述


http://chatgpt.dhexx.cn/article/O09cMSoe.shtml

相关文章

C7-如何学好C语言

1&#xff1a;必须实践--敲代码 2&#xff1a;画图理解--内存布局 3&#xff1a;调试&#xff01;调试&#xff01; 课本推荐&#xff1a;

学好C语言从关键字开始

目录 1、C语言数据类型 2、变量的命名规则 3、最冤枉的关键字——sizeof 4、signed和unsigned 原、反、补 深入理解变量内容的存入和取出 大小端 1、C语言数据类型 在学习C语言数据类型之前&#xff0c;我们得先了解&#xff1a;为什么要有类型&#xff1f; 类型为什…

【c语言】如何学好c语言?c语言应该怎么去学?

大家好&#xff0c;我是想要慢慢变得优秀的向阳同学&#xff0c;谢谢大家的支持&#xff01;让我这个菜鸟博主可以有非常开心的收获1000的浏览量和200的粉丝&#xff01;我也会不断努力去持续分享一下高质量的博客内容的&#xff01;再次感谢大家的支持&#xff01;如果觉得我的…

python open函数编码_python中的open函数如何编码?

python中的open函数可以通过在打开文件时添加encoding参数来指定使用的编码方式&#xff0c;encoding表示的是返回的数据采用何种编码。 open()的函数原型&#xff1a;open(file, mode‘r, buffering-1, encodingNone, errorsNone, newlineNone, closefdTrue) 从官方文档中我…

open函数和 write函数

问题 Python内置多种函数与第三方库&#xff0c;本文对python中的open()函数和 write函数进行简单的讲解。 方法 open()函数 open()函数用于创建或打开指定文件&#xff0c;该函数的常用语法格式: open(name[,mode[,buffering]]) name : 要创建或打开文件的文件名称&#xff0c…

python open函数用法_Python使用open函数打开文件的常用模式

python 中open()的用法? open("/path/to/my/image.png", "rb") 中的‘rb’代表了什么意思?所有试图感动你而做的事,最终都只感动了小编自己。 r表示只读,b表示二进制 与此对应的是w表示可写,t表示文本方式打开。 再增加一些官方的解释: >>>…

linux中open函数详解

1、open函数 包含头文件 #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> open函数有两个参数和三个参数 int open(const char *pathname, int flags); int open(const char *pathname, int flags, mode_t mode); Pathname:要打开的文件名…

python open函数默认路径_Python open函数打开文件路径

要以读文件的模式打开一个文件对象,使用Python内置的open()函数,传入文件名和标示符,标示符r表示读。 >>> f = open(D:/test.txt,r) 注意了,对初学python的同学而言,open()函数着实存在一个不大不小的坑,而且十分不容易发现。 错误演示: >>> f = open…

linux open详解,Linux系统open函数详解

Linux系统中open函数主要作用就是打开和创建文件,可以根据参数来定制我们需要的文件的属性和用户权限等各种参数,下面良许教程网为大家分享一下Linux系统open函数具体使用方法。 一、open函数用来干什么 open函数在Linux下一般用来打开或者创建一个文件,我们可以根据参数来定…

第9.2节 Python的文件打开函数open详解

一、 引言 在操作一个文件前&#xff0c;大部分情况需要先打开文件&#xff0c;才能进行&#xff0c;在Python中使用内置函数open来打开一个文件。open函数是Python的一个内置函数&#xff0c;io模块 定义的函数open是该内置函数的同义词&#xff08;这是Python官网中关于io.op…

Python open 函数

open 函数语法 open() 函数的作用是打开一个文件,并返回一个 file对象(即文件对象)。 open 是一个动作,可以理解为我们打开文档的点击动作。 file 对象是一个实物,可以理解为我们打开的具体文档,例如记事本、表格、Word 或其他具体的文档。 open() 函数的语法为: f =…

Linux 文件IO学习之open函数深入了解

open()函数是在学习文件IO中的第一个函数。作用是打开一个文件&#xff0c;或者创建出一个文件。 需要注意的是&#xff0c;能实现这样功能的函数其实有两套&#xff0c;一个是系统IO所提供的open()函数&#xff0c;一个是标准IO提供的fopen()函数。(二者对比放在最后) 先看看…

Python open函数详解

演示环境&#xff0c;操作系统&#xff1a;Win10 21H2&#xff08;64bit&#xff09;&#xff1b;Python解释器&#xff1a;3.8.10。 open是Python的一个内置函数&#xff0c;一般用于本地文件的读写操作。用法如下。 my_file open(file, mode, buffering, encoding, errors…

sql去重查询

背景&#xff1a; 项目有消息推送&#xff0c;根据消息推送记录&#xff0c;筛选出一共有哪几种消息类型&#xff0c;并且标题和文本是什么 表部分结构如图&#xff1a; 主要是根据subject来去重所有数据&#xff0c;难点是&#xff0c;使用distinct的话&#xff0c;无法显示…

sql 去重查询 distinct

sql 去重查询 select Distinct UserId,Name from UserInfo where UserType1 介绍 distinct一般是用来去除查询结果中的重复记录的&#xff0c;而且这个语句在select、insert、delete和update中只可以在select中使用&#xff0c; 具体的语法如下&#xff1a; select distinc…

SQL去重的三种方法

目录 1.distinct去重 2.group by去重 3.row_number() over (parttion by 分组列 order by 排序列) 有这么一张test的表, 我们将对这张表进行操作来自验证去重 这里的去重&#xff1a;查询的时候, 不显示重复&#xff0c;并不是删除表中的重复项 1.distinct去重 只能一列去…

SQL去重方法汇总

更多教程请到友情连接&#xff1a; 菜鸟教程https://www.piaodoo.com 茂名一技http://www.enechn.com ppt制作教程步骤 http://www.tpyjn.cn 兴化论坛http://www.yimoge.cn 电白论坛 http://www.fcdzs.com 在使用SQL提数的时候&#xff0c;常会遇到表内有重复值的时候&…

SQL中去除重复数据的几种方法,我一次性都告诉你​

使用SQL对数据进行提取和分析时&#xff0c;我们经常会遇到数据重复的场景&#xff0c;需要我们对数据进行去重后分析。 以某电商公司的销售报表为例&#xff0c;常见的去重方法我们用到distinct 或者group by 语句&#xff0c; 今天介绍一种新的方法&#xff0c;利用窗口函数对…

SQL:数据去重的三种方法

1、使用distinct去重 distinct用来查询不重复记录的条数&#xff0c;用count(distinct id)来返回不重复字段的条数。用法注意&#xff1a; distinct【查询字段】&#xff0c;必须放在要查询字段的开头&#xff0c;即放在第一个参数&#xff1b;只能在SELECT 语句中使用&#…

SQL去重的三种方法汇总​

SQL去重的三种方法汇总​ 这里的去重是指&#xff1a;查询的时候, 不显示重复&#xff0c;并不是删除表中的重复项 1.distinct去重 注意的点&#xff1a;distinct 只能一列去重&#xff0c;当distinct后跟大于1个参数时&#xff0c;他们之间的关系是&&(逻辑与)关系&a…