#include<iostream>
#include<ctime>
#include<string>
#include<conio.h>
using namespace std;
#define MapHeight 40
#define MapWidth 40
#define MaxSize 100
string Map[MapHeight][MapWidth];
//坐标(x,y)
struct Coord
{
int X;
int Y;
bool operator==(Coord c)
{
return (c.X) == X && (c.Y == Y);
}
Coord(int x, int y)
{
X = x;
Y = y;
}
};
//其中记录着蛇的长度,以及每一段蛇的坐标
class Snake
{
public:
int Length;
int m_X[MaxSize];
int m_Y[MaxSize];
Snake()
{
Length = 3;
}
}snake;
//其中记录着食物的坐标
class Food
{
public:
int m_X;
int m_Y;
}food;
//如果 食物的坐标 与 任意一个蛇体坐标 都不相等则输出1,视为符合条件
class Compare
{
public:
bool operator()(int a,int b)
{
Coord C(a, b);
bool judge=1;
bool temp;
for (int i = 0; i < snake.Length; i++)
{
Coord c = Coord(snake.m_X[i], snake.m_Y[i]);
temp = !(c == C);
judge = judge && temp;
}
return judge;
}
}compare;
//------------------------------------------------------------------------------------------------------------------------------
void PrintMap(string arr[MapHeight][MapWidth])
{
for (int i = 0; i < 40; i++)
{
for (int j = 0; j < 40; j++)
{
cout << arr[i][j];
}
cout << endl;
}
}
//------------------------------------------------------------------------------------------------------------------------------
void EvaluateSnake(Snake s)
{
for (int i = 0; i < s.Length; i++)
{
Map[s.m_X[i]][s.m_Y[i]] = "■";
}
}
//------------------------------------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------------------------------------
void CreateFood()
{
food.m_X = rand() % (MapHeight - 2) + 1;
food.m_Y = rand() % (MapWidth - 2) + 1;
for (int i = 0; !compare(food.m_X, food.m_Y); i++)
{
food.m_X = rand() % (MapHeight - 2) + 1;
food.m_Y = rand() % (MapWidth - 2) + 1;
}
Map[food.m_X][food.m_Y] = "⊙";
}
//---------------------------------------------------------------------------------------
int main()
{
srand((unsigned int)time(NULL));
//1·地图的初始化-----------------------------------------------------------------------------------------------------------
for (int i = 0; i < 40; i++)
{
for (int j = 0; j < 40; j++)
{
Map[i][j] = " ";
}
}
for (int i = 0; i < 40; i++)
{
if (i == 0 || i == 39)
{
for (int j = 0; j < 40; j++)
{
Map[i][j] = "■";
}
}
else
{
for (int j = 0; j < 40; j++)
{
Map[i][j] = " ";
if (j == 0 || j == 39)
{
Map[i][j] = "■";
}
}
}
}
//2·蛇的初始化-------------------------------------------------------------------------------------------------------------
snake.m_X[0] = rand() % (MapHeight - 19) + 10;
snake.m_Y[0] = rand() % (MapWidth - 19) + 10;
int judge = rand() % 4;
if (judge == 0)
{
for (int i = 1; i <= 2; i++)
{
snake.m_X[i] = snake.m_X[i - 1];
snake.m_Y[i] = snake.m_Y[i - 1]+1;
}
}
else if (judge == 1)
{
for (int i = 1; i <= 2; i++)
{
snake.m_X[i] = snake.m_X[i - 1];
snake.m_Y[i] = snake.m_Y[i - 1] - 1;
}
}
else if (judge == 2)
{
for (int i = 1; i <= 2; i++)
{
snake.m_X[i] = snake.m_X[i - 1] + 1;
snake.m_Y[i] = snake.m_Y[i - 1];
}
}
else
{
for (int i = 1; i <= 2; i++)
{
snake.m_X[i] = snake.m_X[i - 1] - 1;
snake.m_Y[i] = snake.m_Y[i - 1];
}
}
for (int i = 0; i < snake.Length; i++)
{
Map[snake.m_X[i]][snake.m_Y[i]] = "■";
}
//3·食物的初始化-----------------------------------------------------------------------------------------------------------
CreateFood();
//--------------------------------------------------------------------------------------------------------------------------
//范例轮回
while (true)
{
PrintMap(Map);
int key = _getch();
switch (key)
{
case 119:
if (snake.m_X[0] == snake.m_X[1])
{
snake.m_X[0] -= 1;
Map[snake.m_X[snake.Length - 1]][snake.m_Y[snake.Length - 1]] = " ";
for (int i = snake.Length - 1; i > 0; i--)
{
snake.m_X[i] = snake.m_X[i - 1];
snake.m_Y[i] = snake.m_Y[i - 1];
}
EvaluateSnake(snake);
break;
}
else
{
break;
}
case 115:
break;
case 97:
break;
case 100:
break;
default:
break;
}
system("cls");
}
}
我只写了w即向上,如果蛇也朝上,那就会断开,我不知道为什么