我大概在一个多月前把自己上学期写的c代码的贪吃蛇游戏push到csdn上,并且说c风格的贪吃蛇写起来有些麻烦(贪吃蛇游戏的c语言实现),准备用面向对象的c++再写一遍。现在我们专业恰好刚教完了c++,学校也布置了一道简单的贪吃蛇的编程题目,实现下来,的确觉得c++的思路清晰很多,所以再次把c++的代码push上来,供大家对比参考:)

直接上代码,c++把整个游戏拆分成几个文件,分开上,有一定的c++基础的同学应该可以很容易看懂。
1、全局头文件(global.hpp)
#ifndef _GLOBAL_H_
#define _GLOBAL_H_
#ifndef SYMBOLS
#define HEAD '@'
#define BODY 'X'
#define EMPTY '+'
#define FOOD '$'
#endif // !SYMBOLS
enum direction { up = 0, down = 1, left = 2, right = 4, freeze = 5 };
struct point {
int x;
int y;
point(int x = 0, int y = 0) : x(x), y(y) {}
point(const point& another) : x(another.x), y(another.y) {}
point& operator=(const point& other) {
x = other.x;
y = other.y;
return *this;
}
friend bool operator==(const point& point1, const point& point2) {
return point1.x == point2.x && point1.y == point2.y;
}
point& move(direction d) {
switch (d) {
case up:
x--;
break;
case down:
x++;
break;
case left:
y--;
break;
case right:
y++;
break;
case freeze:
default:
break;
}
return *this;
}
};
#endif // !_GLOBAL_H_
另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。