贪吃蛇的实现以及优化(基于控制台)

编程入门 行业动态 更新时间:2024-10-05 21:23:13

贪吃蛇的实现以及优化(基于<a href=https://www.elefans.com/category/jswz/34/1771374.html style=控制台)"/>

贪吃蛇的实现以及优化(基于控制台)

文章目录

  • 1.前言
  • 2.基于控制台实现。
  • 3.为贪吃蛇添加更多创新。
    • (1)输出一个比较漂亮的图像作为墙体
    • (2)初始化改变颜色,以及窗口大小。
    • (3)更具想象力的贪吃蛇功能
    • (4)实现简单的交互界面以及功能
  • 4.总结

1.前言

上一个blog我们讲了讲贪吃蛇的核心思想,其实主要就是整个贪吃蛇游戏最核心的部分,有了那些核心思想完成一个简陋的贪吃蛇不成问题。那么如何让简陋的贪吃蛇不再简陋呢?这一篇我们就主要来讲一下如何实现更加完美的贪吃蛇。

2.基于控制台实现。

实验主要的要求有:实现贪吃蛇游戏基本功能,屏幕上随机出现一个“食物”,称为豆子,上下左右控制“蛇”的移动,吃到“豆子”以后“蛇”的身体加长一点。“蛇”碰到边界或蛇头与蛇身相撞,蛇死亡,游戏结束。为游戏设计友好的交互界面;例如欢迎界面,游戏界面,游戏结束界面。要有开始键、暂停键和停止退出的选项。对蛇吃到豆子进行分值计算,可以设置游戏速度,游戏音乐等拓展元素。
实验要求是需要用到一些交互界面开发软件的。开始的时候自己也是想过要用MFC来实现整个贪吃蛇的,百度了一下相关教程,我所知道的就是B站上只有一篇是讲述MFC实现贪吃蛇的视频。B站上讲解的视频确实讲了很重点的东西,但如果没有任何MFC基础去看那个的话,有点小自闭,很多按键以及添加的头文件和函数都是最大的难题。如果有兴趣的话觉得自己MFC的知识还足够丰富的话,推荐可以去看一下。
我就比较菜了,学了大概一整天,一个多小时的视频看了好几遍,学是没学到多少,整个人是真的自闭了。所以我果断放弃了MFC,就用控制台了。界面丑一点就丑一点了,功能多一点就好了。

3.为贪吃蛇添加更多创新。

(1)输出一个比较漂亮的图像作为墙体

一般来说贪吃蛇的墙都是很普通的“@"或者“#“,就是一个比较短的符号,我们可以在C++里看一下生成地图的效果。

```cpp
cout << " ";for (int i=1;i<=n;i++)cout << "-";cout << endl;for (int j=0;j<=m-1;j++){cout << "|";for (int i=1;i<=n;i++) cout << " ";cout << "|" << endl;}cout << " ";for (int i=1;i<=n;i++)cout << "-"

大概是这样的:

还是比较丑的,这时候我们能不能用特殊符号来当作墙呢,输出的话应该就比较漂亮一点了。就比如▲或◆或●这样的。但是这时候如果我们还是像上次那样定位然后输出的话可以试下效果

cout << " ";for (int i=1;i<=n;i++)cout << "■";cout << endl;for (int j=0;j<=m-1;j++){cout << "■";for (int i=1;i<=n;i++) cout << " ";cout << "■" << endl;}cout << " ";for (int i=1;i<=n;i++)cout << "■";


有点奇怪,为什么呢,这就要说明一下了,我们#$%这种符号都占一个字节,而在屏幕上你会发现屏幕的宽两个符号之间距离特别大,具体应该是宽是长的两倍,宽就占了大概两个字节。所以我们用特殊符号其实输出的不是一个字节的数,而是一个两字节的符号。我们刚刚输出的空格其实算是一个字节的,这时候只需要输出两个空格就好了。
这样看起来就舒服多了,但其实还是有问题的。我们所用到的蛇还是只占一个字节的,所以我们还必须改一下蛇占数组长度的值,把列数从1改为2.

(2)初始化改变颜色,以及窗口大小。

对于想让用户自定义颜色的问题,需要用到的函数其实也是包含在头文件下,就是windows.h这个头文件。实现改变颜色的话,我们需要用到system函数分别进行几个操作。system(”cls“),system(”pause“),system(“color. XX”),实现清屏功能,暂停功能以及改变前景色和背景色的功能。基本思想就是用户输入两个字符,然后判断这两个字符是不是十六进制中的一个(0~A),如果是的话就可以调用system函数。然后判断用户按键输入的字符是什么,然后调用System(“color XX”)函数,XX就可以赋值为用户所输入的那个值。实现起来的话,不嫌麻烦的话可以直接switch嵌套switc case,然后得到第一个十六进制数,再得到另一个十六进制的数字。

#include<Windows.h>
void color(char a, char b)
{switch (a){case '0':switch (b) {case'0':system("color 00"); break;case'1':system("color 01"); break;case'2':system("color 02"); break;case'3':system("color 03"); break;case'4':system("color 04"); break;case'5':system("color 05"); break;case'6':system("color 06"); break;case'7':system("color 07"); break;case'8':system("color 08"); break;case'9':system("color 09"); break;case'A':system("color 0A"); break;case'B':system("color 0B"); break;case'C':system("color 0C"); break;case'D':system("color 0D"); break;case'E':system("color 0E"); break;case'F':system("color 0F"); break;}break;case '1':switch (b) {case'0':system("color 10"); break;case'1':system("color 11"); break;case'2':system("color 12"); break;case'3':system("color 13"); break;case'4':system("color 14"); break;case'5':system("color 15"); break;case'6':system("color 16"); break;case'7':system("color 17"); break;case'8':system("color 18"); break;case'9':system("color 19"); break;case'A':system("color 1A"); break;case'B':system("color 1B"); break;case'C':system("color 1C"); break;case'D':system("color 1D"); break;case'E':system("color 1E"); break;case'F':system("color 1F"); break;}break;case '2':switch (b) {case'0':system("color 20"); break;case'1':system("color 21"); break;case'2':system("color 22"); break;case'3':system("color 23"); break;case'4':system("color 24"); break;case'5':system("color 25"); break;case'6':system("color 26"); break;case'7':system("color 27"); break;case'8':system("color 28"); break;case'9':system("color 29"); break;case'A':system("color 2A"); break;case'B':system("color 2B"); break;case'C':system("color 2C"); break;case'D':system("color 2D"); break;case'E':system("color 2E"); break;case'F':system("color 2F"); break;}break;case '3':switch (b) {case'0':system("color 30"); break;case'1':system("color 31"); break;case'2':system("color 32"); break;case'3':system("color 33"); break;case'4':system("color 34"); break;case'5':system("color 35"); break;case'6':system("color 36"); break;case'7':system("color 37"); break;case'8':system("color 38"); break;case'9':system("color 39"); break;case'A':system("color 3A"); break;case'B':system("color 3B"); break;case'C':system("color 3C"); break;case'D':system("color 3D"); break;case'E':system("color 3E"); break;case'F':system("color 3F"); break;}break;case '4':switch (b) {case'0':system("color 40"); break;case'1':system("color 41"); break;case'2':system("color 42"); break;case'3':system("color 43"); break;case'4':system("color 44"); break;case'5':system("color 45"); break;case'6':system("color 46"); break;case'7':system("color 47"); break;case'8':system("color 48"); break;case'9':system("color 49"); break;case'A':system("color 4A"); break;case'B':system("color 4B"); break;case'C':system("color 4C"); break;case'D':system("color 4D"); break;case'E':system("color 4E"); break;case'F':system("color 4F"); break;}break;case '5':switch (b) {case'0':system("color 50"); break;case'1':system("color 51"); break;case'2':system("color 52"); break;case'3':system("color 53"); break;case'4':system("color 54"); break;case'5':system("color 55"); break;case'6':system("color 56"); break;case'7':system("color 57")

更多推荐

贪吃蛇的实现以及优化(基于控制台)

本文发布于:2024-02-28 11:09:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1769263.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控制台   贪吃蛇

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!