阅读学习了源代码,并做了简单的注释和修改,里面只用了链表数据结构,非常适合C语言入门者学习阅读。

程序可在VS2013下编译运行。

#include

#include

#include

#include

#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 runGame();

void initGame();

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++)//初始长度为4

{

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));//为了防止每次产生的随机数相同,种子设置为time

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 runGame()//控制游戏

{

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("C语言研究中心 www.clang.cc");

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 initGame()//开始界面

{

Pos(40, 12);

system("title C语言研究中心 www.clang.cc");

printf("欢迎来到贪食蛇游戏!");

Pos(40, 25);

printf(" C语言研究中心 www.clang.cc.\n");

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);

while (getchar() != 'y')

{

printf("close?[y]");

}

exit(0);

}

void gameStart()//游戏初始化

{

system("mode con cols=100 lines=30");

initGame();

creatMap();

initSnake();

createFood();

}

int main()

{

gameStart();

runGame();

endGame();

return 0;

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

用c语言单链表编写贪吃蛇程序6,C语言链表实现贪吃蛇游戏相关推荐

  1. matlab m语言,我要编写matlab一个公式的m语言,请问错在哪里?,M语言的MATLAB的M语言...

    导航:网站首页 > 我要编写matlab一个公式的m语言,请问错在哪里?,M语言的MATLAB的M语言 我要编写matlab一个公式的m语言,请问错在哪里?,M语言的MATLAB的M语言 匿名网 ...

  2. 怎样设置一个函数C语言,C语言中怎样编写一个函数 如何在C语言中定义一个函数?...

    如何在C语言中定义一个函数?小编很想在你面前流泪最后却还是选择装作打个哈欠 为什么小编怎么定义函数都不正确呢? 总是说小编 表达语法错误在main函数中 小编们可以在头文件与main函数之间定义,并编 ...

  3. C语言学习之编写一个C程序,运行时输人abc三个值,输出其中值最大者。

    编写一个C程序,运行时输人abc三个值,输出其中值最大者. #include <stdio.h> void main(){int a,b,c,max;printf("请输入三个数 ...

  4. c语言的上级步骤,数据结构 上级程序一(C语言).doc

    数据结构 上级程序一(C语言) 程序一:顺序表的运算 #define MAX 100 #include "stdio.h" int n; int insert(int b[],in ...

  5. c语言程序设计科学出版社课后答案,程序设计基础C语言科学出版社.doc

    程序设计基础C语言科学出版社 程序设计基础--C语言 科学出版社 第六章--其他数据模型,教材习题答案. 东风冷雪 如果认为对自己不利那就离开. 你看了比没有看好,首先印象还是有的,如果自己不会你可能 ...

  6. 微信小程序的脚本就是c语言,新手尝试编写微信小程序(2)——我的第一个微信小程序...

    前面,我们大体上了解了微信小程序的框架,已经简单的页面元素显示方法及变量和自定义函数的调用.这里我们继续来探索隐藏在微信默认新建的小程序中的秘密.这篇博文,我们来进一步了解一下第一个小程序中的几个典型 ...

  7. c语言编写的点菜程序,基于C语言实现点菜系统

    本文实例为大家分享了C语言点菜系统的具体代码,供大家参考,具体内容如下 用C语言编写的简单的餐厅点菜系统,操作简单,代码不多,菜单可以自己更改,价格也是 #include #include #incl ...

  8. 把英文句子译成数字代码 c语言,10、编写一个译码程序,把一个英语句子译成数字代码。译码规则是以数字1代替字母A,数字2代替字母B,……...

    满意答案 zigv4500 2013.07.12 采纳率:40%    等级:12 已帮助:7564人 用C语言编写的,没有VC6.0就没有调试,你己调试一下吧 #include void main( ...

  9. php编写服务器端脚本程序,PHP脚本语言写的简单服务器程序

    $username = $_POST["username"];/*客户端请求方式为POST,请求参数封装成nsdata类型放在HTTPBody中传给服务器,服务器用PHP脚本语言接 ...

最新文章

  1. OpenCV | OpenCV彩色图像直方图算法实现
  2. linux的source命令,linux命令之Source命令
  3. Python.Scrapy.12-scrapy-source-code-analysis-part-2
  4. tomcat启动前端项目
  5. 小鸭脖大生意——绝味鸭脖背后的故事
  6. Java 序列化之 Externalizable
  7. RHEL5.7下iptabels防火墙配置(下)
  8. CNN结构:Windows使用FasterRCNN-C++版本
  9. android defStyleAttr/defStyleRes
  10. 【note】Head First Java笔记
  11. Blos查看计算机硬盘,bios查看硬盘损坏
  12. Oracle Database-PL/SQL
  13. maya绳子建模(包含插件)
  14. SrpingCloud微服务 服务调用逻辑图
  15. 等保三级核心-应用安全
  16. react-native-sound 音频
  17. 小米6无人直播详细教程+工具包
  18. itchat微信好友信息统计(性别区域)
  19. 前端对接打印机的一些经验总结
  20. AE基础教程第一阶段——11图层的介绍

热门文章

  1. 云图说|DRS数据对比——带您随时观测数据一致性
  2. 带你认识7种云化测试武器
  3. 一图看懂软件缺陷检查涉及的内容
  4. JavaScript实现:如何写出漂亮的条件表达式
  5. 一个15年的架构师谈“如何成为一名优秀的解决方案架构师”
  6. 从一盏路灯,看亿万级联接的智能之路
  7. 【华为云技术分享】ArcFace简介
  8. 如何不用BPM配置时间
  9. 华为云ModelArts 2.0全面升级,革新传统AI开发模式
  10. linux中切换到上级目录,vsftp中控制用户是否允许切换到上级目录