贪吃蛇项目源码

时间:2022-01-20 04:02:55
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace _2d
{
class Dit
{
public int x, y;
public Dit(int _x=0,int _y=0)
{
x = _x;
y = _y;

}
}
class Map
{
int dir1 = 3;
Dit renwu;
const int buy = 20;
Dit _food;
bool food;
Random where = new Random();
Random _where = new Random();
int speed = 300;
char c;
public void mp()
{

for (int i = 0; i < buy; i++)
{
for (int j = 0; j < buy; j++)
{
if (i == 0 || i == buy - 1 || j == 0 || j == buy - 1)
Console.Write("█");
else
Console.Write(" ");
}
Console.WriteLine();
}
}
const char b = 'O';
const char d = ' ';
const char e = '*';
const char f = 'o';
public int fenshu = 0;
public void set(int x, int y, char b)
{
int buy = 20;
Console.SetCursorPosition(2 * y, x);
Console.Write(b);
Console.SetCursorPosition(0, buy);
}

public void put()
{

int buy = 20;
bool end = false;
Dit renwu = new Dit();
renwu.x = _where.Next(5, buy - 7);
renwu.y = _where.Next(5, buy - 7);

List<Dit> s = new List<Dit>();
s.Add(new Dit(renwu.x, renwu.y));
s.Add(new Dit(renwu.x - 1,renwu.y));
set(renwu.x - 1, renwu.y, f);
s.Add(new Dit(renwu.x - 2, renwu.y));
set(renwu.x - 2, renwu.y, f);


set(renwu.x, renwu.y, b);

bool food = false;
Dit _food = new Dit();
_food.x = where.Next(1, buy - 2);
_food.y = where.Next(1, buy - 2);
for (int i=0;i<= s.Count-1 ; i++)
{
if (_food.y == s[i].x && _food.x == s[i].y)
{
_food.x = where.Next(1, buy - 2);
_food.y = where.Next(1, buy - 2);
}
else { break; }
}
set(_food.y, _food.x, e);
Thread th = new Thread(new ThreadStart(dir));
th.Start();
for (; ; )
{
food = false;
if (renwu.x == _food.y && renwu.y == _food.x)
{

food = true;
s.Add(new Dit());
_food.x = where.Next(1, buy - 2);
_food.y = where.Next(1, buy - 2);
set(_food.y, _food.x, e);
fenshu++;
}
if(food==false)
{
set(s[s.Count - 1].x, s[s.Count - 1].y, d);
}
for (int i = s.Count - 1; i > 0; i--)
{

s[i].x = s[i - 1].x;
s[i].y = s[i - 1].y;
} if (food == true)
{
set(s[0].x, s[0].y, b);
}
else
set(s[0].x, s[0].y, f);
switch (dir1)
{

case 1: set(renwu.x - 1, renwu.y, b); renwu.x--; s[0].x--; break;
case 2: set(renwu.x, renwu.y - 1, b); renwu.y--; s[0].y--; break;
case 3: set(renwu.x + 1, renwu.y, b); renwu.x++; s[0].x++; break;
case 4: set(renwu.x, renwu.y + 1, b); renwu.y++; s[0].y++; break;
}
if (renwu.x == 0 || renwu.y == 0 || renwu.x == buy - 1 || renwu.y == buy - 1)
{
Console.Clear();
Console.WriteLine("GameOver!\n您的得分是{0}", fenshu);
return;
}
for (int i = 1; i <= s.Count - 1; i++)
{
if (renwu.x == s[i].x &&renwu.y == s[i].y)
{
Console.Clear();
Console.WriteLine("GameOver!\n您的得分是{0}", fenshu);
return;
}
}


Thread.Sleep(speed);
}
}
void dir()
{

for (; ; )
{
c = Console.ReadKey(true).KeyChar;
switch (c)
{

case 'w': if(dir1!=3)dir1 = 1; break;
case 'a': if(dir1!=4)dir1 = 2; break;
case 's': if(dir1!=1)dir1 = 3; break;
case 'd': if(dir1!=2)dir1 = 4; break;
}
}

}
}
class Menu
{
public void paly()
{
Console.WriteLine("欢迎!\n命令行贪吃蛇2.0 · 碰撞? 碰撞!");
Console.WriteLine("****************************************");
Console.WriteLine("输入1开始游戏\n请将输入法调至英文状态");
}
public void start()
{
int a = Convert.ToInt32(Console.ReadLine());
if (a == 1)
{
Console.Clear();
Map _map = new Map();
_map.mp();
_map.put();
}
}
}
class Program
{
static void Main(string[] args)
{
Menu _menu = new Menu();
Console.CursorVisible = false;//隐藏光标
_menu.paly();
_menu.start();
Console.ReadLine();
}
}
}