
#包括<stdio, h >
#包括<process。H >
#包括<Windows。H >
#包括<conio。H >
#包括<时间。H >
#包括<stdlib。H >
#defineWIDTH40
#defineHEIGH12
枚举方向{//方向
离开了,
对的,
向上
下
};
StructFood{//食品
Intx;
Inty;
};
{//绘制蛇体
intx;
inty;
structNode*next;
};
structSnake{//蛇属性du
intlenth;//长度
enumdirectiondir;//方向
};
structFood*food;//食物
structSnake*snake;//蛇属性
structNode*snode,*tail;//蛇身
intSPEECH=200;
intscore=0;//分数
intsmark=0;//吃食物标zhi记
inttimes=0;
intSTOP=0;
voidInitfood();//产生食物
voidInitsnake();//构造snake
voidEatfood();//头部前进
voidAddnode(intx,inty);//增加蛇身
voiddisplay(structNode*shead);//显示蛇身坐dao标
voidmove();//蛇移动
voiddraw();//画蛇
voidHomepage();//主页
voidkeybordhit();//监控键盘按键
voidAddtail();//吃到食物
voidgotoxy(intx,inty)//定位光标
{
COORDpos;
pos.X=x-1;
pos.Y=y-1;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
voidInitsnake()//构造snake
{
inti;
snake=(structSnake*)malloc(sizeof(structSnake));
tail=(structNode*)malloc(sizeof(structNode));
food=(structFood*)malloc(sizeof(structFood));
snake->lenth=5//初始长度 5
snake->dir=RIGHT//初始蛇头方向 右
for(i=2i<=snake->lenth+2i++)//增加 5 个结点
{
Addnode(i,2);
}
}
voidInitfood()//产生食物
{
structNode*p=snode;
intmark=1;
srand((unsigned)time(NULL));//以时间为种子产生随机数
while(1)
{
food->x=rand()%(WIDTH-2)+2//食物X坐标
food->y=rand()%(HEIGH-2)+2//食物Y坐标
while(p!=NULL)
{
if((food->x==p->x)&&(food->y==p->y))//如果食物产生在蛇身上
{//则重新生成食物
mark=0;//食物生成无效
break;
}
p=p->next
}
if(mark==1)//如果食物不在蛇身上,生成食物,否则重新生成食物
{
gotoxy(food->x,food->y)
printf("%c",3);
break;
}
mark=1;
p=snode;
}
}
voidmove()//移动
{
structNode*q,*p=snode;
if(snake->dir==RIGHT)
{
Addnode(p->x+1,p->y)
if(smark==0)
{
while(p->next!=NULL)
{
q=p;
p=p->next
}
q->next=NULL
free(p);
}
}
if(snake->dir==LEFT)
{
Addnode(p->x-1,p->y)
if(smark==0)
{
while(p->next!=NULL)
{
q=p;
p=p->next
}
q->next=NULL
free(p);
}
}
if(snake->dir==UP)
{
Addnode(p->x,p->y-1)
if(smark==0)
{
while(p->next!=NULL)
{
q=p;
p=p->next
}
q->next=NULL
free(p);
}
}
if(snake->dir==DOWN)
{
Addnode(p->x,p->y+1)
if(smark==0)
{
while(p->next!=NULL)
{
q=p;
p=p->next
}
q->next=NULL
free(p);
}
}
}
voidAddnode(intx,inty)//增加蛇身
{
structNode*newnode=(structNode*)malloc(sizeof(structNode));
structNode*p=snode;
newnode->next=snode
newnode->x=x
newnode->y=y
snode=newnode;//结点加到蛇头
if(x<2||x>=WIDTH||y<2||y>=HEIGH)//碰到边界
{
STOP=1;
gotoxy(10,19);
printf("撞墙,游戏结束,任意键退出!\n");//失败
_getch();
free(snode);//释放内存
free(snake);
exit(0);
}
while(p!=NULL)//碰到自身
{
if(p->next!=NULL)
if((p->x==x)&&(p->y==y))
{
STOP=1;
gotoxy(10,19);
printf("撞到自身,游戏结束,任意键退出!\n");//失败
_getch();
free(snode);//释放内存
free(snake);
exit(0);
}
p=p->next
}
}
voidEatfood()//吃到食物
{
Addtail();
score++;
}
voidAddtail()//增加蛇尾
{
structNode*newnode=(structNode*)malloc(sizeof(structNode));
structNode*p=snode;
tail->next=newnode
newnode->x=50
newnode->y=20
newnode->next=NULL//结点加到蛇头
tail=newnode;//新的蛇尾
}
voiddraw()//画蛇
{
structNode*p=snode;
inti,j;
while(p!=NULL)
{
gotoxy(p->x,p->y)
printf("%c",2);
tail=p;
p=p->next
}
if(snode->x==food->x&&snode->y==food->y)//蛇头坐标等于食物坐标
{
smark=1;
Eatfood();//增加结点
Initfood();//产生食物
}
if(smark==0)
{
gotoxy(tail->x,tail->y)//没吃到食物清除之前的尾结点
printf("%c",'');//如果吃到食物,不清楚尾结点
}
else
{
times=1;
}
if((smark==1)&&(times==1))
{
gotoxy(tail->x,tail->y)//没吃到食物清除之前的尾结点
printf("%c",'');//如果吃到食物,不清楚尾结点
smark=0;
}
gotoxy(50,12);
printf("食物: %d,%d",food->x,food->y)
gotoxy(50,5);
printf("分数:%d",score);
gotoxy(50,7);
printf("速度:%d",SPEECH);
gotoxy(15,14);
printf("按o键加速");
gotoxy(15,15);
printf("按p键减速");
gotoxy(15,16);
printf("按空格键暂停");
}
voidHideCursor()//隐藏光标
{
CONSOLE_CURSOR_INFOcursor_info={1,0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info)
}
voidHomepage()//绘主页
{
intx,y;
HideCursor();//隐藏光标
printf("----------------------------------------\n");
printf("|\t\t\t\t|\n");
printf("|\t\t\t\t|\n");
printf("|\t\t\t\t|\n");
printf("|\t\t\t\t|\n");
printf("|\t\t\t\t|\n");
printf("|\t\t\t\t|\n");
printf("|\t\t\t\t|\n");
printf("|\t\t\t\t|\n");
printf("|\t\t\t\t|\n");
printf("|\t\t\t\t|\n");
printf("----------------------------------------\n");
gotoxy(5,13);
printf("任意键开始游戏!按W.A.S.D控制方向");
_getch();
Initsnake();
Initfood();
gotoxy(5,13);
printf("");
}
voidkeybordhit()//监控键盘
{
charch;
if(_kbhit())
{
ch=getch();
switch(ch)
{
case'W':
case 'w':if(snake->dir==DOWN)//如果本来方向是下,而按相反方向无效
{
break;
}
else
snake->dir=UPbreak
case'A':
case 'a':if(snake->dir==RIGHT)//如果本来方向是右,而按相反方向无效
{
break;
}
else
snake->dir=LEFTbreak
case'S':
case 's':if(snake->dir==UP)//如果本来方向是上,而按相反方向无效
{
break;
}
else
snake->dir=DOWNbreak
case'D':
case 'd':if(snake->dir==LEFT)//如果本来方向是左,而按相反方向无效
{
break;
}
else
snake->dir=RIGHTbreak
case'O':
case'o':
if(SPEECH>=150)//速度加快
{
SPEECH=SPEECH-50;
}
break;
case'P':
case'p':
if(SPEECH<=400)//速度减慢
{
SPEECH=SPEECH+50;
}
break;
case''://暂停
gotoxy(15,18);
printf("游戏已暂停,按任意键恢复游戏");
system("pause>nul")
gotoxy(15,18);
printf("");
break;
default:break;
}
}
}
intmain(void)//程序入口
{
Homepage();
while(!STOP)
{
keybordhit();//监控键盘按键
move();//蛇的坐标变化
draw();//蛇的重绘
Sleep(SPEECH);//暂时挂起线程
}
return0;
}
扩展资料:
注意事项:
1.代码将源信息转换为易于通信或存储的符号。译码(译码)是还原和译码的过程,它将代码符号转换为接受者能够理解的形式。
2.编码的原因之一是为了在普通语言(口头或书面)难以实现的情况下进行交流。例如,一面旗帜可以用一个特定的标记来表达一个特定的信息,而站在远处的另一个人可以解释标记来重现该信息。
贪吃蛇游戏C语言源代码学习
源代码下载地址为:www.clang.cc
阅读学习了源代码,并做了简单的注释和修改,里面只用了链表数据结构,非常适合C语言入门者学习阅读。
程序可在VS2013下编译运行。
1 #include<stdio.h> 2 #include<time.h> 3 #include<windows.h> 4 #include<stdlib.h> 5 6 #define U 1 7 #define D 2 8 #define L 3
9 #define R 4 //蛇的状态,U:上 ;D:下;L:左 R:右 10 11 typedef struct SNAKE //蛇身的一个节点 12 { 13 int x14 int y15 struct SNAKE *next16 }snake17 18 //全局变量// 19 int score = 0, add = 10//总得分与每次吃食物得分。 20 int status, sleeptime = 200//每次运行的时间间隔 21 snake *head, *food//蛇头指针,食物指针 22 snake *q//遍历蛇的时候用到的指针 23 int endGamestatus = 0//游戏结束的情况,1:撞到墙;2:咬到自己;3:主动退出游戏。 24 25 //声明全部函数// 26 void Pos()27 void creatMap()28 void initSnake()29 int biteSelf()30 void createFood()31 void cantCrossWall()32 void snakeMove()33 void pause()34 void runGame()35 void initGame()36 void endGame()37 void gameStart()38 39 void Pos(int x, int y)//设置光标位置 40 { 41 COORD pos42 HANDLE hOutput43 pos.X = x44 pos.Y = y45 hOutput = GetStdHandle(STD_OUTPUT_HANDLE)//返回标准的输入、输出或错误的设备的句柄,也就是获得输入、输出/错误的屏幕缓冲区的句柄 46 SetConsoleCursorPosition(hOutput, pos)47 } 48 49 void creatMap()//创建地图 50 { 51 int i52 for (i = 0i<58i += 2)//打印上下边框 53 { 54 Pos(i, 0)55 printf("■")//一个方块占两个位置 56 Pos(i, 26)57 printf("■")58 } 59 for (i = 1i<26i++)//打印左右边框 60 { 61 Pos(0, i)62 printf("■")63 Pos(56, i)64 printf("■")65 } 66 } 67 68 void initSnake()//初始化蛇身 69 { 70 snake *tail71 int i72 tail = (snake*)malloc(sizeof(snake))//从蛇尾开始,头插法,以x,y设定开始的位置// 73 tail->x = 2474 tail->y = 575 tail->next = NULL76 for (i = 1i <= 4i++)//初始长度为4 77 { 78 head = (snake*)malloc(sizeof(snake))79 head->next = tail80 head->x = 24 + 2 * i81 head->y = 582 tail = head83 } 84 while (tail != NULL)//从头到为,输出蛇身 85 { 86 Pos(tail->x, tail->y)87 printf("■")88 tail = tail->next89 } 90 } 91 //?? 92 int biteSelf()//判断是否咬到了自己 93 { 94 snake *self95 self = head->next96 while (self != NULL) 97 { 98 if (self->x == head->x &&self->y == head->y) 99 {100 return 1101 }
102 self = self->next103 }104 return 0105 }106 107 void createFood()//随机出现食物108 {109 snake *food_1110 srand((unsigned)time(NULL))//为了防止每次产生的随机数相同,种子设置为time111 food_1 = (snake*)malloc(sizeof(snake))112 while ((food_1->x % 2) != 0) //保证其为偶数,使得食物能与蛇头对其113 {114 food_1->x = rand() % 52 + 2115 }116 food_1->y = rand() % 24 + 1117 q = head118 while (q->next == NULL)119 {120 if (q->x == food_1->x &&q->y == food_1->y) //判断蛇身是否与食物重合121 {122 free(food_1)123 createFood()124 }125 q = q->next126 }127 Pos(food_1->x, food_1->y)128 food = food_1129 printf("■")130 }131 132 void cantCrossWall()//不能穿墙133 {134 if (head->x == 0 || head->x == 56 || head->y == 0 || head->y == 26)135 {136 endGamestatus = 1137 endGame()138 }139 }140 141 void snakeMove()//蛇前进,上U,下D,左L,右R142 {143 snake * nexthead144 cantCrossWall()145 146 nexthead = (snake*)malloc(sizeof(snake))147 if (status == U)148 {149 nexthead->x = head->x150 nexthead->y = head->y - 1151 if (nexthead->x == food->x &&nexthead->y == food->y)//如果下一个有食物//152 {153 nexthead->next = head154 head = nexthead155 q = head156 while (q != NULL)157 {158 Pos(q->x, q->y)159 printf("■")160 q = q->next161 }162 score = score + add163 createFood()164 }165 else //如果没有食物//166 {167 nexthead->next = head168 head = nexthead169 q = head170 while (q->next->next != NULL)171 {172 Pos(q->x, q->y)173 printf("■")174 q = q->next175 }176 Pos(q->next->x, q->next->y)177 printf(" ")178 free(q->next)179 q->next = NULL180 }181 }182 if (status == D)183 {184 nexthead->x = head->x185 nexthead->y = head->y + 1186 if (nexthead->x == food->x &&nexthead->y == food->y) //有食物187 {188 nexthead->next = head189 head = nexthead190 q = head191 while (q != NULL)192 {193 Pos(q->x, q->y)194 printf("■")195 q = q->next196 }197 score = score + add198 createFood()199 }200 else //没有食物201 {202 nexthead->next = head203 head = nexthead204 q = head205 while (q->next->next != NULL)206 {207 Pos(q->x, q->y)208 printf("■")209 q = q->next210 }211 Pos(q->next->x, q->next->y)212 printf(" ")213 free(q->next)214 q->next = NULL215 }216 }217 if (status == L)218 {219 nexthead->x = head->x - 2220 nexthead->y = head->y221 if (nexthead->x == food->x &&nexthead->y == food->y)//有食物222 {223 nexthead->next = head224 head = nexthead225 q = head226 while (q != NULL)227 {228 Pos(q->x, q->y)229 printf("■")230 q = q->next231 }232 score = score + add233 createFood()234 }235 else //没有食物236 {237 nexthead->next = head238 head = nexthead239 q = head240 while (q->next->next != NULL)241 {242 Pos(q->x, q->y)243 printf("■")244 q = q->next245 }246 Pos(q->next->x, q->next->y)247 printf(" ")248 free(q->next)249 q->next = NULL250 }251 }252 if (status == R)253 {254 nexthead->x = head->x + 2255 nexthead->y = head->y256 if (nexthead->x == food->x &&nexthead->y == food->y)//有食物257 {258 nexthead->next = head259 head = nexthead260 q = head261 while (q != NULL)262 {263 Pos(q->x, q->y)264 printf("■")265 q = q->next266 }267 score = score + add268 createFood()269 }270 else //没有食物271 {272 nexthead->next = head273 head = nexthead274 q = head275 while (q->next->next != NULL)276 {277 Pos(q->x, q->y)278 printf("■")279 q = q->next280 }281 Pos(q->next->x, q->next->y)282 printf(" ")283 free(q->next)284 q->next = NULL285 }286 }287 if (biteSelf() == 1) //判断是否会咬到自己288 {289 endGamestatus = 2290 endGame()291 }292 }293 294 void pause()//暂停295 {296 while (1)297 {298 Sleep(300)299 if (GetAsyncKeyState(VK_SPACE))300 {301 break302 }303 304 }305 }306 307 void runGame()//控制游戏 308 {309 310 Pos(64, 15)311 printf("不能穿墙,不能咬到自己\n")312 Pos(64, 16)313 printf("用↑.↓.←.→分别控制蛇的移动.")314 Pos(64, 17)315 printf("F1 为加速,F2 为减速\n")316 Pos(64, 18)317 printf("ESC :退出游戏.space:暂停游戏.")318 Pos(64, 20)319 printf("C语言研究中心 www.clang.cc")320 status = R321 while (1)322 {323 Pos(64, 10)324 printf("得分:%d ", score)325 Pos(64, 11)326 printf("每个食物得分:%d分", add)327 if (GetAsyncKeyState(VK_UP) &&status != D)328 {329 status = U330 }331 else if (GetAsyncKeyState(VK_DOWN) &&status != U)332 {333 status = D334 }335 else if (GetAsyncKeyState(VK_LEFT) &&status != R)336 {337 status = L338 }339 else if (GetAsyncKeyState(VK_RIGHT) &&status != L)340 {341 status = R342 }343 else if (GetAsyncKeyState(VK_SPACE))344 {345 pause()346 }347 else if (GetAsyncKeyState(VK_ESCAPE))348 {349 endGamestatus = 3350 break351 }352 else if (GetAsyncKeyState(VK_F1))353 {354 if (sleeptime >= 50)355 {356 sleeptime = sleeptime - 30357 add = add + 2358 if (sleeptime == 320)359 {360 add = 2//防止减到1之后再加回来有错361 }362 }363 }364 else if (GetAsyncKeyState(VK_F2))365 {366 if (sleeptime<350)367 {368 sleeptime = sleeptime + 30369 add = add - 2370 if (sleeptime == 350)371 {372 add = 1 //保证最低分为1373 }374 }375 }376 Sleep(sleeptime)377 snakeMove()378 }379 }380 381 void initGame()//开始界面382 {383 Pos(40, 12)384 385 system("title C语言研究中心 www.clang.cc")386 printf("欢迎来到贪食蛇游戏!")387 Pos(40, 25)388 printf(" C语言研究中心 www.clang.cc.\n")389 system("pause")390 system("cls")391 Pos(25, 12)392 printf("用↑.↓.←.→分别控制蛇的移动, F1 为加速,2 为减速\n")393 Pos(25, 13)394 printf("加速将能得到更高的分数。\n")395 system("pause")396 system("cls")397 }398 399 void endGame()//结束游戏400 {401 402 system("cls")403 Pos(24, 12)404 if (endGamestatus == 1)405 {406 printf("对不起,您撞到墙了。游戏结束.")407 }408 else if (endGamestatus == 2)409 {410 printf("对不起,您咬到自己了。游戏结束.")411 }412 else if (endGamestatus == 3)413 {414 printf("您的已经结束了游戏。")415 }416 Pos(24, 13)417 printf("您的得分是%d\n", score)418 while (getchar() != 'y')419 {
420 printf("close?[y]")421 }422 exit(0)423 }424 425 void gameStart()//游戏初始化426 {427 system("mode con cols=100 lines=30")428 initGame()429 creatMap()430 initSnake()431 createFood()432 }433 434 int main()435 {436 gameStart()437 runGame()438 endGame()439 return 0440 }
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)