接上文《C/C++编程笔记:C语言贪吃蛇源代码控制台(一),会动的那种哦!》如果你在学习C语言开发贪吃蛇的话,零基础建议从上一篇开始哦!接下来正式开始吧!
![](https://upload-images.jianshu.io/upload_images/19341983-fcc262c3bd041398.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
三、蛇的运动
上次我已经教大家画出蛇了,现在我就教大家让蛇动起来。为了让大家更好理解,蛇的移动就用最简单的办法,这里就不用链表,顺序队列什么的了。蛇的移动就是头往前移动,然后后面的身体移动到前一节身体原来的位置上。就像这样:
![](https://upload-images.jianshu.io/upload_images/19341983-210d2dd862c6d9d3.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
那蛇头怎么办呢?
为了确定蛇头的位置,我们定义一个变量原来存储蛇现在的方向。
intdirection; //蛇的方向
我们可以通过direction的值来标记现在蛇的方向。比如0代表上,1代表下。。。
为了方便,我们定义一些常量
//定义direction的每个值代表的方向
#defineUP 0
#defineDOWN 1
#defineLEFT 2
#defineRIGHT 3
这样,我们就可以这样写:
![](https://upload-images.jianshu.io/upload_images/19341983-267b13cc9d9ef57c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
写好的move函数如下
![](https://upload-images.jianshu.io/upload_images/19341983-47e3a8473983f133.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
然后move写好了,我们还要写个循环,这样就可以让蛇一直动了。地图画完一次并且画了蛇之后,我们要把光标移动到起始位置。最简单的办法是每一次画地图的时候都把关标移动到(0,0)。
修改drawMap函数为下面这样。在开头加一个设置位置的语句。
![](https://upload-images.jianshu.io/upload_images/19341983-1905cb5774ef1da5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
然后建立一个循环,不断地画蛇,移动。
![](https://upload-images.jianshu.io/upload_images/19341983-58ff7ccc01d07392.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
然后观察一下,蛇确实走了,但是蛇的痕迹还在。
![](https://upload-images.jianshu.io/upload_images/19341983-00abc7d13d3008cc.gif?imageMogr2/auto-orient/strip)
这样我们可以在蛇移动函数里面,擦去上一次的尾巴。
修改过后的move函数如下
![](https://upload-images.jianshu.io/upload_images/19341983-c4a562c6d12806ba.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
运行一下,蛇动起来了。
![](https://upload-images.jianshu.io/upload_images/19341983-158ce9c3bb64f99a.gif?imageMogr2/auto-orient/strip)
然后试试修改下方向。在init函数最后写一个
![](https://upload-images.jianshu.io/upload_images/19341983-1831a29ad18ef708.gif?imageMogr2/auto-orient/strip)
好,感觉很正常。
现在我们可以通过给direction变量赋值来改变蛇的前进方向了。
接下来,我们试试用键盘控制它。
写一个新函数key,用来处理键盘输入。
我们的输入不能在屏幕上有痕迹,并且不能输入一个按键就回车一下。。。所以我们用getch函数。
加conio.h头文件。然后getch会暂停程序等待输入,而蛇只在转弯的时候需要输入。所以我们就需要一个判断有没有输入的函数
![](https://upload-images.jianshu.io/upload_images/19341983-1c967bf8c7a7333a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
然后通过输入的字符是w还是a还是s或者d来控制蛇的方向。
![](https://upload-images.jianshu.io/upload_images/19341983-06c79497e1cffc4b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
好了,现在我们就能通过wasd这四个按键控制蛇到处走了。(进入游戏先切换为英文输入法)
![](https://upload-images.jianshu.io/upload_images/19341983-ea6a8ce5647f838e.gif?imageMogr2/auto-orient/strip)
下面附这一步的完整代码。
![](https://upload-images.jianshu.io/upload_images/19341983-2923f461f11484ed.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](https://upload-images.jianshu.io/upload_images/19341983-71febc3f8a7df60e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](https://upload-images.jianshu.io/upload_images/19341983-6a4413790d378066.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](https://upload-images.jianshu.io/upload_images/19341983-98156c11eb92fd79.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
好了,现在食物能正常投放了
接下来我们就来实现一下蛇吃到食物的变长功能吧。
首先设置一个变量标记蛇是否吃到食物
bool eated=false; //标记是否吃到食物
然后在main里面的判断吃到食物时,使eated变为true
![](https://upload-images.jianshu.io/upload_images/19341983-82268938614a16d7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
然后在move函数里面加下面的代码
![](https://upload-images.jianshu.io/upload_images/19341983-ca28fae9e71ba1e1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
这样在下面的坐标移动环节,就不会舍弃掉上一次的尾巴节点。蛇就变长了。
来看看效果:
![](https://upload-images.jianshu.io/upload_images/19341983-0d1cf364b68298c0.gif?imageMogr2/auto-orient/strip)
蛇可以变长了。不过有时候玩着玩着有事,又舍不得玩了这么长的蛇,要是有个暂停功能就好了。接下来我们就来顺便实现下。
在key函数里面的switch里面加一个case
![](https://upload-images.jianshu.io/upload_images/19341983-7e61b6fd5142fb36.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
这样就可以实现按p键暂停了。(记得玩的时候切换为英文输入法呀)
![](https://upload-images.jianshu.io/upload_images/19341983-53c0ea043446bcfd.gif?imageMogr2/auto-orient/strip)
来看看到现在为止的代码:
![](https://upload-images.jianshu.io/upload_images/19341983-c0208b386632242c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](https://upload-images.jianshu.io/upload_images/19341983-a673ab23e2793309.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](https://upload-images.jianshu.io/upload_images/19341983-4d748e80a6bf0ef3.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](https://upload-images.jianshu.io/upload_images/19341983-32935074dd79825e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](https://upload-images.jianshu.io/upload_images/19341983-013c2d3ba7969044.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](https://upload-images.jianshu.io/upload_images/19341983-1662686f9c2b9a29.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](https://upload-images.jianshu.io/upload_images/19341983-4d587a36a492cb80.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
后续更新:
现在就来实现一下game over的功能吧。
写个函数判断蛇是否撞到自己或者撞到墙:
![](https://upload-images.jianshu.io/upload_images/19341983-4ac2c4e0ea297904.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
然后在main里面加这个实现gameOver
![](https://upload-images.jianshu.io/upload_images/19341983-33cddb34e5ab0d3c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](https://upload-images.jianshu.io/upload_images/19341983-145643e7da308ce2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
接下来,我们来显示一下成绩。
声明一个变量来记录分数
intscore=0;//得分
然后当吃到食物时分数增加(main里面)
![](https://upload-images.jianshu.io/upload_images/19341983-e60bd6986bd9a37f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
然后写一个printScore函数来写分数
![](https://upload-images.jianshu.io/upload_images/19341983-f1904bb780cf94ca.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
记得在main里面调用啊。看看效果:
![](https://upload-images.jianshu.io/upload_images/19341983-950e917e31788fe1.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
以上就是本期分享的全部内容,希望对大家有帮助哦!
学习C/C++编程知识,或者你学习C/C++的时候有难度,可以来笔者的C语言C++零基础编程学习圈。
微信公众号:C语言编程学习基地,欢迎关注~