一,食物类
class Block
{
int x, y;
public Block()
{
}
public Block(int _x, int _y)
{
x = _x;
y = _y;
}
public int Row
{
get { return x; }
set { x = value; }
}
public int Col
{
get { return y; }
set { y = value; }
}
public bool isEqual(Block b)
{
if (x==b.Row&&y==b.Col)
{
return true;
}
return false;
}
public static Block RandomFood()
{
Random r = new Random();
Block b = new Block(r.Next(1, 24), r.Next(1, 79));
return b;
}
}
二,蛇类
public enum Direction
{
left,
right,
top,
bottom
};
//蛇
class Snake
{
List snakeList = new List();
public List SnakeList
{
get { return snakeList; }
}
public Snake()
{
InitSnake();
}
void InitSnake()
{
int rowStart = 2;
int colStart = 5;
int lenth = 5 + colStart;
Block b;
for (int i = rowStart; i < lenth; i++)
{
b = new Block(rowStart, i);
SnakeList.Insert(0, b);
}
}
public bool IsEatFood(Block b)
{
Block head = snakeList[0];
if (head.Row == b.Row && head.Col == b.Col)
{
snakeList.Add(b);
return true;
}
return false;
}
public void Move(Direction dir)
{
Block head = snakeList[0];
snakeList.RemoveAt(snakeList.Count - 1);
Block newBlock = null;
switch (dir)
{
case Direction.left:
newBlock = new Block(head.Row, head.Col - 1);
break;
case Direction.right:
newBlock = new Block(head.Row, head.Col + 1);
break;
case Direction.top:
newBlock = new Block(head.Row - 1, head.Col);
break;
case Direction.bottom:
newBlock = new Block(head.Row + 1, head.Col);
break;
}
snakeList.Insert(0, newBlock);
}
//蛇死亡
public bool IsOver()
{
Block head = snakeList[0];
if (head.Row == 0 || head.Col == 0 || head.Row == 25 || head.Col == 80)
{
return true;
}
for (int i = 1; i < snakeList.Count; i++)
{
if (head.Row == SnakeList[i].Row && head.Col == snakeList[i].Col)
{
return true;
}
}
return false;
}
}
三,地图管理
class MapManager
{
const int row = 25;
const int col = 80;
Snake snake;
Block b;
int[,] gameMap = new int[row, col];
StringBuilder mapBuffer;
int nowSocre = 0;
public static Direction dir = Direction.right;
bool isDie = false;
Timer t;
void InitMap()
{
for (int x = 0; x < row; x++)
{
if (x==row-1||x==0)
{
for (int y = 0; y < col; y++)
{
gameMap[x, y] = 1;
}
}
else
{
for (int y = 0; y < col; y++)
{
if (y==col-1||y==0)
{
gameMap[x, y] = 1;
}
else
{
gameMap[x, y] = 0;
}
}
}
}
}
void InitSnake()
{
foreach (Block item in snake.SnakeList)
{
gameMap[item.Row, item.Col]=1;
}
}
void InitFood()
{
gameMap[b.Row, b.Col] = 1;
}
void DrawMap()
{
mapBuffer.Clear();
Console.Clear();
for (int x = 0; x < row; x++)
{
for (int y = 0; y < col; y++)
{
if (gameMap[ x,y]==1)
{
mapBuffer.Append("*");
}
else
{
mapBuffer.Append(" ");
}
}
mapBuffer.Append("\n");
}
Console.WriteLine("\n************当前的分:{0}************\n",nowSocre);
Console.WriteLine(mapBuffer.ToString());
}
void GameRun(object o)
{
InitMap();
InitFood();
InitSnake();
if (snake.IsEatFood(b) == true)
{
b = Block.RandomFood();
nowSocre++;
}
snake.Move(dir);
if (snake.IsOver()==true)
{
GameOver();
}
DrawMap();
}
void GameOver()
{
Console.Clear();
Console.WriteLine("游戏结束");
isDie = true;
t.Dispose();
}
void GameInit()
{
Console.WriteLine("按w,s,a,d控制蛇上下左右,按下q退出游戏");
Console.WriteLine("按下任意键进入游戏");
Console.ReadKey(true);
public void StartGame()
{
GameInit();
b = Block.RandomFood();
snake = new Snake();
mapBuffer = new StringBuilder();
t = new Timer(GameRun,null,200,100);
char keyChar;
//蛇移动
while (isDie == false)
{
keyChar = Console.ReadKey(true).KeyChar;
switch (keyChar)
{
case 's':
if (MapManager.dir != Direction.top)
{
MapManager.dir = Direction.bottom;
}
break;
case 'w':
if (MapManager.dir!=Direction.bottom)
{
MapManager.dir = Direction.top;
}
break;
case 'a':
if (MapManager.dir!=Direction.right)
{
MapManager.dir = Direction.left;
}
break;
case 'd':
if (MapManager.dir!=Direction.left)
{
MapManager.dir = Direction.right;
}
break;
case 'q':
GameOver();
break;
}
}
Console.ReadLine();
}
}