首页 > 作文

C语言实现贪吃蛇游戏代码

更新时间:2023-04-04 22:18:39 阅读: 评论:0

目录
一、实现效果二、部分代码解释总结

一、实现效果

键位:使用wasd四个键位来控制方向,按q键退出(注意在终用英文输入法实现键控)

规则:蛇每吃一个豆会得10分,同时身体边长、移速加快

当蛇碰到墙壁或咬到自身时游戏结束,同时会输出游戏得分

二、部分代码解释

(1)用结构体定义蛇和豆

typedef struct snakes{int x;int y;struct snakes *next;}snake; snake *head,*taivegetable是可数名词吗l; struct food{int x;int y;}food;

(2)打印墙壁

void creatgraph()             {int i;for (i = 0; i<58; i += 2)//打印上下边框{gotoprint(i, 0);gotoprint(i, 26);}for (i = 1; i < 26; i++){gotoprint(0课件中心, i);gotoprint(56, i);}head = (snake*)malloc(sizeof(snake));head->x = 16;hea漂亮牛仔裤d->y = 15;//gotoprint(head->x, head->y);tail = (snake*)malloc(sizeof(snake));snake *p = (snake*)malloc(sizeof(snake));snake *q = (snake*)malloc(sizeof(snake));p->x = 16;p->y = 16;q->x = 16;q->y = 17;head->next = p;p->next = q;q->next = tail;//gotoprint(p->x, p->y);//gotoprint(q->x, q->y);tail->next = null;tail->x = 4;tail->y = 2;} void gotoxy(int x, int y){coord pos;handle houtput;pos.x = x;pos.y = y;houtput = getstdhandle(std_output_handle);tconsolecursorposition(houtput, pos);} void gotoprint(int x, int y){gotoxy(x, y);printf("■");} void gotodelete(int x, int y){gotoxy(x, y);printf("  ");}

(3)生成豆

void creatfood(){srand((int)time(null));lable:food.y = rand() % (25 - 1 + 1) + 1;food.x = rand() % (54 - 2 + 1) + 2;if (food.x % 2 != 0){food.x = food.x+1;}snake *judge = head;while (1)  {if (judge->next == null) break;if (food.x == judge->x&&food.y == judge->y){goto lable;}judge = judge->next;}gotoxy(food.x, food.y);printf("⊙");}

(4)点击控制函数

int clickcontrol(){char c;while (1){if (judge()==0) return 0;if (_kbhit()){click = _getch();}movingbody();eating();}return 1;}

(5)移动控制

void movingbody(){int count = 0;int a = head->x, b = head->y;snake *p = head;while (1){if (p->next == null) break;gotodelete(p->x, p->y);count++;p = p->next;}switch (click){ca up:head->y -= 1;changebody(a,b);break;ca down:head->y += 1;changebody(a,b);break;ca left:head->x -= 2;changebody(a,b);break;ca right:head->x += 2;changebody(a,b);break;ca stop:_getch();break;}p = head;while (1){if (p->next == null) break;gotoprint(p->x, p->y);p = p->next;}p = h鸭子怎么做好吃又简单的做法ead;gotoxy(0, 28);if (count <= 10) speed = 150;el if (count > 10 && count <= 20) speed = 100;el if (count > 20 && count <= 40) speed = 50;el speed = 10;sleep(speed);}

(6)更改蛇身

void changebody(int a,int b){snake *p = head->next;int mid1, mid2,_mid1,_mid2;mid1 = p->x;mid2 = p->y;while (1){if (p->next->next == null) break;_mid1 = p->next->x;_mid2 = p->next->y;p->next->x = mid1;p->next->y = mid2;mid1 = _mid1;mid2 = _mid2; p = p->next;}p = head->next;{p->x = a;p->y = b;}}

总结

到此这篇关于c语言实现贪吃蛇游戏代码的文章就介绍到这了,更多相关c语言贪吃蛇游戏内容请男人要有男人味搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!

本文发布于:2023-04-04 22:18:38,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/b25608d2208e91e6435ff8c98f78e617.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:C语言实现贪吃蛇游戏代码.doc

本文 PDF 下载地址:C语言实现贪吃蛇游戏代码.pdf

下一篇:返回列表
标签:键位   游戏   代码   墙壁
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图