思路 (都是方法) : 1.设置蛇、食物、还有方向的结构体。(x与y) 2. 用数组在控制台输出 地图、蛇、食物。3. 用方向的机构写蛇的方向 4. 写碰撞的方法 {食物与蛇的头部碰撞(包含 食物之后的随机出现和蛇的身体长度加一)、蛇的头部与自己身体的碰撞、蛇与墙的碰撞} 5. 将出来的蛇的身体以逐层交换的循环方法,排序
注意 : 1. 蛇和食物的描绘中,都要以自身的长度描述。 2. 将蛇的长度加一的过程写成单独的方法 。 3. 单独设立蛇的长度,当碰撞后长度加一,加一可以放在蛇长度加一的过程。 4. 新出现的蛇身体是在第四部碰撞后并出现在食物位置,之后通过第五步移动将其坐标加在后面,并在第二步的描绘中,描述出来。 5.为了解决闪屏,执行程序进行的时候,没有用console.clear()。在移动的后面加了两句,目的是为了将移动后的蛇的最后一个位置去掉,起到局部刷新的目的。
结构体:
struct Snake
{
public int x, y;
}
struct Food
{
public int x, y;
}
struct Temp
{
public int x, y;
}
防止重复,将其声明直接设在外面
static int snakeLengh = 0;
static Random r = new Random();
static Temp temp = new Temp();
一些 基础数据 main函数里的
int[,] map = new int[20, 35]; //地图的大小
Snake[] snake = new Snake[15 * 15]; //蛇的数组
AddBody(snake, 5, 5); //一开始加蛇的数组 , 将位置该到5.5 此刻snakeLengh == 1
Food[] food = new Food[1]; //食物的数组
food[0] = new Food();
food[0].x = 3;
food[0].y = 3;
bool isGameOver; //判断跳出执行循环
int gameTime = 300; // 时间
Console.Title = "贪吃蛇"; // 标题
Console.BackgroundColor = ConsoleColor.DarkGray; //背景色
Console.ForegroundColor = ConsoleColor.DarkRed; //前景色
DrawMap(map); // 绘制地图
执行过程
while (true)
{
DrawSnake(snake); // 画蛇
DrawFood(food);
Thread.Sleep(gameTime); // 蛇的速度
gameTime = gameTime > 200 ? --gameTime : gameTime; // 当到200之前一直 加速
isGameOver = !Collition(snake, food, map); // 执行 碰撞判定 返回的bool值 决定是否跳出 循环
if (isGameOver)
{
Console.WriteLine(" GAME OVER ");
break;
}
Move(snake); //蛇的移动
}
开始写方法了 绘画地图
static void DrawMap(int[,] dmap)
{
for (int i = 0; i < dmap.GetLength(0); i++)
{
for (int j = 0; j < dmap.GetLength(1); j++)
{
if (i == 0 || j == 0 || i == dmap.GetLength(0) - 1 || j == dmap.GetLength(1) - 1)
{
Console.Write("■"); //边界
}
else
{
Console.Write(" ");
}
}
Console.WriteLine();
}
foreach (char coler in dmap) // 这句循环貌似没有运行
{
if (coler == '◆')
{
Console.ForegroundColor = ConsoleColor.Blue ;
}
}
}
绘画蛇
static void DrawSnake(Snake[] snake)
{
for (int i = 0; i < snakeLengh; i++) // 蛇的长度越长 画的越多
{
Console.SetCursorPosition(snake[i].x * 2, snake[i].y);
Console.Write("◆");
}
}
绘画食物
static void DrawFood(Food[] food)
{
for (int i = 0; i < food.Length; i++)
{
Console.SetCursorPosition(food[i].x * 2, food[i].y);
Console.Write("★");
}
}
控制方向
static void Direction()
{
while (true)
{
char intputChar = Console.ReadKey(true).KeyChar;
temp.x = 0; // 一直归零 这样在移动中 相加不会使得蛇越来越快
temp.y = 0;
switch (intputChar)
{
case 'w': temp.y -= 1; break;
case 's': temp.y += 1; break;
case 'a': temp.x -= 1; break;
case 'd': temp.x += 1; break;
}
}
}
蛇的移动
static void Move(Snake[] snake)
{
int oldX = snake[0].x; // 头当前位置
int oldY = snake[0].y;
int NewX = snake[0].x;
int NewY = snake[0].y;
snake[0].x += temp.x; // 头移动到下一个位置
snake[0].y += temp.y;
for (int i = 1; i < snakeLengh; i++) // 要从1 开始,否则snake【0】会无限循环,及蛇不动
{
NewX = snake[i].x; // 保证当前位置不丢失
NewY = snake[i].y;
snake[i].x = oldX; // 将之前的位置赋予这个位置
snake[i].y = oldY;
oldX = NewX; // 将当前位置赋予之前位置
oldY = NewY; // 描述的我有点晕
}
Console.SetCursorPosition(NewX * 2, NewY); // 此处将最后的i的位置 清零 ,达到局部清除的效果
Console.WriteLine(" ");
}
三个判断 碰撞循环
static bool Collition(Snake[] snake, Food[] food, int[,] map) // bool 类型
{
for (int i = 0; i < food.Length; i++)
{
if (snake[0].x == food[i].x && snake[0].y == food[i].y) // 是否碰到食物
{
int nwfoodx = 0;
int nwfoody = 0;
bool isRight = false;
while (true)
{
nwfoodx = r.Next(1, 14);
nwfoody = r.Next(1, 14);
for (int j = 0; j < snakeLengh; j++)
{
if (snake[j].x == nwfoodx && snake[j].y == nwfoody) // 食物新位置是否与蛇的身体重合
{
isRight = false; break;
}
if (j == snakeLengh - 1)
{
isRight = true;
}
}
if (isRight)
{
break;
}
}
food[i].x = nwfoodx; // 赋予食物新位置
food[i].y = nwfoody;
AddBody(snake, food[i].x, food[i].x); // 加身体 将位置给到碰撞的食物的位置
return true;
}
}
for (int i = 1; i < snakeLengh; i++) // 是否碰到自身
{
if (snake[0].x == snake[i].x && snake[0].y == snake[i].y)
{
return false;
}
}
for (int i = 0; i < map.GetLength(0); i++) // 是否碰到墙
{
for (int j = 0; j < map.GetLength(1); j++)
{
if (i == 0 || i == map.GetLength(0) - 1)
{
if (snake[0].x == i)
{
return false;
}
}
if (j == 0 || j == map.GetLength(1) - 1)
{
if (snake[0].y == j)
{
return false;
}
}
}
}
return true;
}
加蛇的身体,
static void AddBody(Snake[] snake, int a, int b) // 用在碰撞食物后的判断
{
snake[snakeLengh] = new Snake();
snake[snakeLengh].x = a;
snake[snakeLengh].y = b;
snakeLengh++; // 蛇的长度加一
}
程序到此就完啦。但这很简单,没有计分表,没有障碍物,也没有加分项。