声明: 以下内容可能会引起某些读者不适, 请小心阅读. 有些内容并没有详细介绍, 可能简单理解也是错误的, 但是这都是为了尽量简单。
前言: 代码是很久之前写的,属于边想边写的那种,很混乱。
推荐材料:
贪吃蛇应该是我们这代人都玩过的游戏。而如果我们要写一个贪吃蛇最需要考虑的就是贪吃蛇是如何移动的。其实贪吃蛇的移动就是尾部的减少和头部的增加。
这篇文章:
ncurses的一些函数的简单解释
|
|
这是我从这里拿到的一个例子。
我们应该注意到这个程序并没有导入标准输入输出库,而是导入了ncurses.h文件。在这个程序里面也没有使用printf(format printing),而是使用了printw这个函数(print window)就是打印到标准屏幕的意思
- initscr:初始化屏幕,启动curses模式
- raw:关闭行缓冲,一般的我们在命令行中输入数据的时候,只有当我们按下回车之后,才可以把数据提交给程序
- noecho: 负责按键事务,我不太懂
- noecho: 关闭回显(按下一个字符之后,屏幕并不显示。比如说在普通的情况下按下a,a会被显示在屏幕上)
- getch: 获取一个字符
- attron: 在后面的打印中增加打印属性,这里是粗体的意思
- attroff: 移除属性
- refresh:之前的printw只是打印到逻辑屏幕,而refresh函数会依据逻辑屏幕的内容,刷新到物理屏幕。
- endwin:退出curses模式
其他的可以自己慢慢去学习
ncurses库的安装和使用
安装
如果你是使用debian系列的linux发行版,例如ubuntu,应该直接键入命令:
如果是别的系列的版本,建议下载源码包,编译安装。或者百度搜索正确的答案。
使用
一般的,只需要在gcc编译命令的后面加上-lncurses就可以正确导入ncurses库了。
没有使用过命令行编译操作的同学需要自己去学习。
贪吃蛇的代码解释
从哪里得到
点击这里。这份代码是我写的,也确实写的不好。但也可以随便看看。
代码结构
|
|
hungry_snake是一个可执行文件,在debian下编译的,在centos下面可能需要重新编译。
main.c是程序的主要逻辑部分。
wellcome.h包含了欢迎界面的代码实现。
snake.h
|
|
这里有一些结构定义和函数声明。
main.c
下面是程序主体逻辑部分:
编译
|
|
小结
这份代码写的太差劲,所以我也没有也不想过多的去讲它。