源代码下载地址为:www.clang.cc

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

程序可在VS2013下编译运行。

 #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 = , add = ;//总得分与每次吃食物得分。
int status, sleeptime = ;//每次运行的时间间隔
snake *head, *food;//蛇头指针,食物指针
snake *q;//遍历蛇的时候用到的指针
int endGamestatus = ; //游戏结束的情况,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 = ; i<; i += )//打印上下边框
{
Pos(i, );
printf("■");//一个方块占两个位置
Pos(i, );
printf("■");
}
for (i = ; i<; i++)//打印左右边框
{
Pos(, i);
printf("■");
Pos(, i);
printf("■");
}
} void initSnake()//初始化蛇身
{
snake *tail;
int i;
tail = (snake*)malloc(sizeof(snake));//从蛇尾开始,头插法,以x,y设定开始的位置//
tail->x = ;
tail->y = ;
tail->next = NULL;
for (i = ; i <= ; i++)//初始长度为4
{
head = (snake*)malloc(sizeof(snake));
head->next = tail;
head->x = + * i;
head->y = ;
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 ;
}
self = self->next;
}
return ;
} void createFood()//随机出现食物
{
snake *food_1;
srand((unsigned)time(NULL));//为了防止每次产生的随机数相同,种子设置为time
food_1 = (snake*)malloc(sizeof(snake));
while ((food_1->x % ) != ) //保证其为偶数,使得食物能与蛇头对其
{
food_1->x = rand() % + ;
}
food_1->y = rand() % + ;
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 == || head->x == || head->y == || head->y == )
{
endGamestatus = ;
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 - ;
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 + ;
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 - ;
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 + ;
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() == ) //判断是否会咬到自己
{
endGamestatus = ;
endGame();
}
} void pause()//暂停
{
while ()
{
Sleep();
if (GetAsyncKeyState(VK_SPACE))
{
break;
} }
} void runGame()//控制游戏
{ Pos(, );
printf("不能穿墙,不能咬到自己\n");
Pos(, );
printf("用↑.↓.←.→分别控制蛇的移动.");
Pos(, );
printf("F1 为加速,F2 为减速\n");
Pos(, );
printf("ESC :退出游戏.space:暂停游戏.");
Pos(, );
printf("C语言研究中心 www.clang.cc");
status = R;
while ()
{
Pos(, );
printf("得分:%d ", score);
Pos(, );
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 = ;
break;
}
else if (GetAsyncKeyState(VK_F1))
{
if (sleeptime >= )
{
sleeptime = sleeptime - ;
add = add + ;
if (sleeptime == )
{
add = ;//防止减到1之后再加回来有错
}
}
}
else if (GetAsyncKeyState(VK_F2))
{
if (sleeptime<)
{
sleeptime = sleeptime + ;
add = add - ;
if (sleeptime == )
{
add = ; //保证最低分为1
}
}
}
Sleep(sleeptime);
snakeMove();
}
} void initGame()//开始界面
{
Pos(, ); system("title C语言研究中心 www.clang.cc");
printf("欢迎来到贪食蛇游戏!");
Pos(, );
printf(" C语言研究中心 www.clang.cc.\n");
system("pause");
system("cls");
Pos(, );
printf("用↑.↓.←.→分别控制蛇的移动, F1 为加速,2 为减速\n");
Pos(, );
printf("加速将能得到更高的分数。\n");
system("pause");
system("cls");
} void endGame()//结束游戏
{ system("cls");
Pos(, );
if (endGamestatus == )
{
printf("对不起,您撞到墙了。游戏结束.");
}
else if (endGamestatus == )
{
printf("对不起,您咬到自己了。游戏结束.");
}
else if (endGamestatus == )
{
printf("您的已经结束了游戏。");
}
Pos(, );
printf("您的得分是%d\n", score);
while (getchar() != 'y')
{
printf("close?[y]");
}
exit();
} void gameStart()//游戏初始化
{
system("mode con cols=100 lines=30");
initGame();
creatMap();
initSnake();
createFood();
} int main()
{
gameStart();
runGame();
endGame();
return ;
}

原博客地址:http://www.cnblogs.com/jacklu/p/5214692.html

贪吃蛇游戏C语言源代码学习的更多相关文章

  1. 贪吃蛇游戏——C语言双向链表实现

    采用了双向链表结点来模拟蛇身结点: 通过C语言光标控制函数来打印地图.蛇身和食物: /************************** *************************** 贪吃 ...

  2. 基于EasyX库的贪吃蛇游戏——C语言实现

    接触编程有段时间了,一直想学习怎么去写个游戏来练练手.在看了B站上的教学终于可以自己试试怎么实现贪吃蛇这个游戏了.好了,废话不多说,我们来看看如何用EasyX库来实现贪吃蛇. 一.准备 工具vc++6 ...

  3. 【C语言项目】贪吃蛇游戏(上)

    目录 00. 目录 01. 开发背景 02. 功能介绍 03. 欢迎界面设计 3.1 常用终端控制函数 3.2 设置文本颜色函数 3.3 设置光标位置函数 3.4 绘制字符画(蛇) 3.5 欢迎界面函 ...

  4. 贪吃蛇游戏(printf输出C语言版本)

    这一次我们应用printf输出实现一个经典的小游戏—贪吃蛇,主要难点是小蛇数据如何存储.如何实现转弯的效果.吃到食物后如何增加长度. 1 构造小蛇 首先,在画面中显示一条静止的小蛇.二维数组canva ...

  5. Qt 学习之路 2(34):贪吃蛇游戏(4)

    Qt 学习之路 2(34):贪吃蛇游戏(4) 豆子 2012年12月30日 Qt 学习之路 2 73条评论 这将是我们这个稍大一些的示例程序的最后一部分.在本章中,我们将完成GameControlle ...

  6. Qt 学习之路 2(33):贪吃蛇游戏(3)

    Qt 学习之路 2(33):贪吃蛇游戏(3) 豆子 2012年12月29日 Qt 学习之路 2 16条评论 继续前面一章的内容.上次我们讲完了有关蛇的静态部分,也就是绘制部分.现在,我们开始添加游戏控 ...

  7. Qt 学习之路 2(32):贪吃蛇游戏(2)

    Qt 学习之路 2(32):贪吃蛇游戏(2) 豆子 2012年12月27日 Qt 学习之路 2 55条评论 下面我们继续上一章的内容.在上一章中,我们已经完成了地图的设计,当然是相当简单的.在我们的游 ...

  8. Qt 学习之路 2(31):贪吃蛇游戏(1)

    Qt 学习之路 2(31):贪吃蛇游戏(1) 豆子 2012年12月18日 Qt 学习之路 2 41条评论 经过前面一段时间的学习,我们已经了解到有关 Qt 相当多的知识.现在,我们将把前面所讲过的知 ...

  9. Android快乐贪吃蛇游戏实战项目开发教程-01项目概述与目录

    一.项目简介 贪吃蛇是一个很经典的游戏,也很适合用来学习.本教程将和大家一起做一个Android版的贪吃蛇游戏. 我已经将做好的案例上传到了应用宝,无病毒.无广告,大家可以放心下载下来把玩一下.应用宝 ...

随机推荐

  1. c#读取INI文件

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...

  2. cWeb开发框架,基于asp.net的cWeb应用开发平台介绍(一)

    cWeb开发框架是基于asp.net的B/S应用开发平台,采用三层架构理论,应用简单.代码简洁.运行快速. cWeb是bubufx提供,是分享资源,无任何版权限制,bubufx继续传承互联网精神,可随 ...

  3. 承接Hololens内容定制外包

    近日,微软宣布第三批微软Hololens开发者版开始发货,包括:头显.头显手提包和一个遥控器.前两批开发者版本分别在今年3月30日和5月9日开始发货的. 第三批AR头显Hololens开发者版发货 虽 ...

  4. ListView addHeaderView 对 position 的影响

    1. 在 public View getView(int position, View convertView, ViewGroup parent) 中position 和 是否有headerView ...

  5. java基础-servlet-1

    servlet,是运行于服务器端的小程序.它既可以运行在http服务器端,也可以运行在mail等其他服务器端.我是这样理解,tomcat把接收到的客户端请求,转发给servlet,它负责业务处理,并且 ...

  6. sql server 替换特殊符号

    --create-- 去掉特殊符号alter function RepSymbol(@str nvarchar(max))returns nvarchar(max)as begin set @str= ...

  7. OAF_开发系列22_实现OAF条形码BarCode

    20150717 Created By BaoXinjian

  8. java io流 对文件夹的操作

    java io流 对文件夹的操作 检查文件夹是否存在 显示文件夹下面的文件 ....更多方法参考 http://www.cnblogs.com/phpyangbo/p/5965781.html ,与文 ...

  9. Windows下Apache配置域名

    1.打开httpd.conf 找到“Include conf/extra/httpd-vhosts.conf”这一行,取消前面的#号注释并保存 2.修改httpd-vhosts.conf文件 进入ap ...

  10. 每天一个 Linux 命令(22):find 命令的参数详解

    find一些常用参数的一些常用实例和一些具体用法和注意事项. 1.使用name选项: 文件名选项是find命令最常用的选项,要么单独使用该选项,要么和其他选项一起使用.  可以使用某种文件名模式来匹配 ...