初学C++,我试着写一个贪吃蛇游戏,代码如下:
#include
#include
#include
#include
#include
#include
using namespace std;
class Snake
{
friend void displaySnake(Snake &s);
public:
using ssbody = pair, string>;
Snake() = default;
vector body() const { return snakebody; }
private:
int lengh=1; //蛇的长度
vector snakebody{make_pair(make_pair(10, 10), "●")}; //蛇的位置和符号
};
void gotoxy(int x, int y)
{
HANDLE h;//句柄,对象的索引
COORD c;//结构体,坐标值
c.X = x;
c.Y = y;
h = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(h, c);
}
void displaySnake(Snake &s) //打印蛇
{
for (auto &c : s.snakebody){
gotoxy(c.fir