Пишу змейку на c# и хотелось бы сделать так чтобы змейка и фроги были разными потоками и когда змея съедает фрога, то поток фрога убивается пока никаких идей
namespace Snake
{
public class Frog : BaseModel
{
private int _height;
private int _width;
public Frog(int x, int y)
{
_width = x;
_height = y;
points = new List<Point>();
}
public void AddFrog(int x = 0, int y = 0)
{
Random rand = new Random(unchecked((int)DateTime.Now.Millisecond));
if (x == 0 && y == 0)
do
{
x = rand.Next(_width);
y = rand.Next(_height);
} while (x == 0 || y == 0);
points.Add(new Point(x, y, Models.Frog));
}
public void Clear()
{
points.Clear();
}
public Point GetPoint()
{
if (points.Count == 1)
return points[0];
else
return null;
}
public override void Draw()
{
throw new NotImplementedException();
}
}
}
public class Game : BaseModel
{
private int _width;
private int _height;
private Snake _snake;
private Frog _frog;
private GameMode _gameMode;
public int Size { get { return _snake.points.Count; } }
public int FrogCount { get; private set; }
public Game()
{
}
public void Start()
{
points = new List<Point>();
_frog = new Frog(_width, _height);
Draw();
}
public void ChangeDirection(Directions direction)
{
_snake.DirectionControl = direction;
}
public bool Move()
{
bool res;
var frogPoint = _frog.points;
res = _snake.Move(ref frogPoint);
// Добавление frog на карту
if (frogPoint.Count < FrogCount)
{
for (int i = 0; i < FrogCount - frogPoint.Count; i++)
{
Random rand = new Random(unchecked((int)DateTime.Now.Millisecond));
int x;
int y;
var points = GetAllPoints();
do
{
x = rand.Next(_width);
y = rand.Next(_height);
} while (points.Exists(p => p.X == x && p.Y == y && p.Model != Models.Empty));
_frog.AddFrog(x, y);
}
}
return res;
}
public void Pause()
{
throw new System.AggregateException();
}
public void Сontinue()
{
throw new System.AggregateException();
}
public void CreateFood()
{
throw new System.AggregateException();
}
public void Exit()
{
throw new System.AggregateException();
}
public void LoadLevel(int level = 0)
{
if (level == 0)
{
FrogCount = 10;
EditSittings(GameMode.HitWall, 30, 25);
_snake = new Snake(5, _width, _height, Directions.Right, _gameMode);
}
}
public void EditSittings(GameMode gameMode, int width, int heigth)
{
_gameMode = gameMode;
_width = width;
_height = heigth;
}
/// <summary>
/// Получить все точки игры
/// </summary>
/// <returns></returns>
public List<Point> GetAllPoints()
{
var allPoints = new List<Point>();
allPoints.AddRange(points);
allPoints.AddRange(_snake.points);
allPoints.AddRange(_frog.points);
return allPoints;
}
public override void Draw()
{
for (int i = 0; i < _width; i++)
{
points.Add(new Point(i, 0, Models.Wall));
points.Add(new Point(i, _height, Models.Wall));
}
for (int i = 0; i < _height; i++)
{
points.Add(new Point(0, i, Models.Wall));
points.Add(new Point(_width, i, Models.Wall));
}
points.Add(new Point(_width, _height, Models.Wall));
for (int i = 1; i < _width; i++)
{
for (int j = 1; j < _height; j++)
{
if (points.Find(x => x.X == i && x.Y == j) == null)
points.Add(new Point(i, j, Models.Empty));
}
}
}
}
}
internal class Snake : BaseModel
{
public Directions _direction;
public int _lenght;
public int _width;
public int _height;
public GameMode _gameMod;
public Directions DirectionControl { get; set; }
public Snake(int lengthSnake, int width, int height, Directions direction, GameMode gameMod)
{
if (width <= 0)
throw new Exception();
if (height <= 0)
throw new Exception();
if (height <= 0)
throw new Exception();
if (lengthSnake >= width || lengthSnake >= height)
throw new Exception("Length of snake not be more size place ");
_width = width;
_height = height;
_direction = direction;
_gameMod = gameMod;
_lenght = lengthSnake;
Draw();
}
public bool Move(ref List<Point> frogPoints)
{
if (points.Count == 0)
throw new Exception("Length of snake not be null ");
int x = points.Last().X;
int y = points.Last().Y;
// Запрет разворота на 180
// Если попытка поворота на 180 градусов
if ((_direction == Directions.Right && DirectionControl == Directions.Left) ||
(_direction == Directions.Left && DirectionControl == Directions.Right) ||
(_direction == Directions.Up && DirectionControl == Directions.Down) ||
(_direction == Directions.Down && DirectionControl == Directions.Up))
{
//_direction;
}
else
{
_direction = DirectionControl;
}
switch (_direction)
{
case Directions.Up:
y -= 1;
break;
case Directions.Down:
y += 1;
break;
case Directions.Left:
x -= 1;
break;
case Directions.Right:
x += 1;
break;
}
bool confused = false;
bool hitTheWall = false;
// TODO учесть GameMod
// Проверка. Врежется в стену или в свое тело
foreach (var point in points)
{
confused = (point.X == x && point.Y == y);
hitTheWall = (x > _width - 1 || x < 1) || (y > _height - 1 || y < 1);
if (confused || hitTheWall)
break;
}
// Проверка. Сьест еду.
bool frogEated = false;
for (int i = 0; i < frogPoints.Count; i++)
{
// Поела, хвост не удаляется
if (frogPoints[i].X == x && frogPoints[i].Y == y)
{
frogPoints.Remove(frogPoints[i]);
points[0].Model = Models.Tail;
points.Last().Model = Models.Body;
frogEated = true;
}
}
// Осталась голодной, хвост удаляется
if (!frogEated)
{
points.RemoveAt(0);
points[0].Model = Models.Tail;
points.Last().Model = Models.Body;
}
// Добавляем голову
points.Add(new Point(x, y, Models.Head));
if (confused || hitTheWall)
{
return false;
}
else
{
return true;
}
}
public override void Draw()
{
int startWidth = 1;
int startHeight = 1;
points = new List<Point>();
for (int i = 0; i < _lenght; i++)
{
// Add Tail
if (i == 0)
points.Add(new Point(startWidth + i, startHeight, Models.Tail));
// Add Head
else if (i == _lenght - 1)
points.Add(new Point(startWidth + i, startHeight, Models.Head));
// Add Body
else
points.Add(new Point(startWidth + i, startHeight, Models.Body));
}
}
}
}