帮解决个问题!!!
using System;using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace Snake
{
public class Game
{
static char foodChar = '$';
static char snakeChar = '#';
static char blank = ' ';
//游戏界面边界表示数组
static int[] border = new int[4] { 0, 24, 0, 79 };
/// <summary>
/// 在相应坐标绘制字符
/// </summary>
/// <param name="left">纵坐标</param>
/// <param name="top">横坐标</param>
/// <param name="c">字符</param>
public static void Draw(int left, int top, char c)
{
Console.SetCursorPosition(left, top);
Console.Write(c);
}
/// <summary>
/// 根据字符在蛇的相应坐标绘制图形
/// </summary>
/// <param name="s">蛇</param>
/// <param name="c">字符</param>
public static void Draw(Snake s, char c)
{
for (int i = s.Length - 1; i >= 0; --i)
{
Draw(s.Left[i], s.Top[i], c);
}
}
/// <summary>
/// 绘制蛇
/// </summary>
/// <param name="s">蛇</param>
public static void SnakeDraw(Snake s)
{
Draw(s, snakeChar);
}
/// <summary>
/// 清除已绘制的蛇
/// </summary>
/// <param name="s">蛇</param>
public static void ClearSnake(Snake s)
{
Draw(s, blank);
}
/// <summary>
/// 设置食物坐标
/// </summary>
/// <param name="f">食物</param>
public static void SetFood(Food f)
{
f.SetFood(border);
}
/// <summary>
/// 蛇吃食物
/// </summary>
/// <param name="s">蛇</param>
/// <param name="f">食物</param>
/// <returns>蛇是否吃了食物</returns>
public static bool SnakeEatFood(Snake s, Food f)
{
if (s.Left[s.Length - 1] == f.Left & s.Top[s.Length - 1] == f.Top)
{
s.EatFood(f);
return true;
}
else
{
return false;
}
}
/// <summary>
/// 绘制食物
/// </summary>
/// <param name="s">蛇</param>
/// <param name="f">食物</param>
public static void FoodDraw(Snake s, Food f)
{
if (SnakeEatFood(s, f))
{
Draw(f.Left, f.Top, blank);
SetFood(f);
}
else
{
Draw(f.Left, f.Top, foodChar);
}
}
/// <summary>
/// 判断游戏是否结束
/// </summary>
/// <param name="s">蛇</param>
/// <returns>是否结束的结果</returns>
public static bool IsEnd(Snake s)
{
if (s.Top[s.Length - 1] < border[0] || s.Top[s.Length - 1] > border[1] || s.Left[s.Length - 1] < border[2] || s.Left[s.Length - 1] > border[3])
{
return true;
}
for (int i = 0; i < s.Length - 1; i++)
{
if (s.Left[i] == s.Left[s.Length - 1] & s.Top[i] == s.Top[s.Length - 1])
{
return true;
}
}
return false;
}
/// <summary>
/// 蛇运动
/// </summary>
/// <param name="s">蛇</param>
/// <param name="k">变化的按键</param>
/// <param name="key">上一次的运动方向键</param>
public static void SnakeRun(Snake s, ref ConsoleKey k, ref ConsoleKey key)
{
if (Console.KeyAvailable)
{
k = Console.ReadKey(true).Key;
}
if (k == ConsoleKey.UpArrow)
{
s.UpRun();
}
else if (k == ConsoleKey.DownArrow)
{
s.DownRun();
}
else if (k == ConsoleKey.LeftArrow)
{
s.LeftRun();
}
else if (k == ConsoleKey.RightArrow)
{
s.RightRun();
}
else
{
k = key;
}
key = k;
}
/// <summary>
/// 运行
/// </summary>
public static void Start()
{
Console.CursorVisible = false;
ConsoleKey key = ConsoleKey.LeftArrow;
ConsoleKey k = ConsoleKey.RightArrow;
Snake s = new Snake();
Food f = new Food();
SetFood(f);
while (true)
{
FoodDraw(s, f);
SnakeDraw(s);
Thread.Sleep(800);
ClearSnake(s);
SnakeRun(s, ref k, ref key);
if (IsEnd(s))
{
Console.Clear();
Console.WriteLine("Game Over !!");
break;
}
}
}
}
}
是控制台的 红色这个地方要怎么控制蛇的速度啊,如果吃了食物就加速度,一直都是800我尝试了也不行啊 帮下。。。