Multiplayer is in. It's messy.
This commit is contained in:
parent
fd552b1429
commit
3da34fadb2
@ -36,9 +36,14 @@ namespace ConsoleSnake
|
||||
|
||||
private static PlayArea _playArea = new PlayArea(1, 4, Console.WindowWidth - 2, Console.WindowHeight - 2);
|
||||
private static bool _running = true;
|
||||
private static bool _growing = false;
|
||||
private static Direction _direction = Direction.Up;
|
||||
private static List<Position2D> _snakeSegments = new List<Position2D>();
|
||||
private static bool _growingP1 = false;
|
||||
private static bool _growingP2 = false;
|
||||
private static Direction _directionP1 = Direction.Right;
|
||||
private static Direction _directionP2 = Direction.Left;
|
||||
private static Position2D _headP1 = new Position2D(-1, -1);
|
||||
private static Position2D _headP2 = new Position2D(-1, -1);
|
||||
private static List<Position2D> _snakeSegmentsP1 = new List<Position2D>();
|
||||
private static List<Position2D> _snakeSegmentsP2 = new List<Position2D>();
|
||||
private static Position2D _food = new Position2D(-1,-1);
|
||||
private static Random rng = new Random();
|
||||
static void Main(string[] args)
|
||||
@ -103,14 +108,18 @@ namespace ConsoleSnake
|
||||
{
|
||||
_playArea = new PlayArea(1, 4, Console.WindowWidth - 2, Console.WindowHeight - 2);
|
||||
_running = true;
|
||||
_growing = false;
|
||||
_direction = Direction.Up;
|
||||
_snakeSegments.Clear();
|
||||
_growingP1 = false;
|
||||
_growingP2 = false;
|
||||
_directionP1 = Direction.Right;
|
||||
_directionP2 = Direction.Left;
|
||||
_snakeSegmentsP1.Clear();
|
||||
_snakeSegmentsP2.Clear();
|
||||
|
||||
// Set up anew
|
||||
_snakeSegments.Add(new Position2D(Console.BufferWidth / 2, Console.BufferHeight / 2));
|
||||
_snakeSegmentsP1.Add(_headP1 = new Position2D(Console.BufferWidth / 2, Console.BufferHeight / 2));
|
||||
_snakeSegmentsP2.Add(_headP2 = new Position2D(Console.BufferWidth / 2 - 1, Console.BufferHeight / 2));
|
||||
SpawnFood();
|
||||
MoveCursor(_snakeSegments.Last());
|
||||
MoveCursor(_snakeSegmentsP1.Last());
|
||||
|
||||
}
|
||||
|
||||
@ -120,7 +129,7 @@ namespace ConsoleSnake
|
||||
do
|
||||
{
|
||||
_food = new Position2D(rng.Next(_playArea.LeftEdge+1, _playArea.RightEdge), rng.Next(_playArea.TopEdge, _playArea.BottomEdge));
|
||||
} while (_snakeSegments.Contains(_food));
|
||||
} while (_snakeSegmentsP1.Contains(_food) || _snakeSegmentsP2.Contains(_food));
|
||||
MoveCursor(_food);
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
Console.Write('@');
|
||||
@ -128,17 +137,33 @@ namespace ConsoleSnake
|
||||
MoveCursor(oldCursorPos);
|
||||
}
|
||||
|
||||
private static void JoinPlayer2()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private static void MoveCursor(Position2D pos) => MoveCursor(pos.X, pos.Y);
|
||||
private static void MoveCursor(Int32 left, Int32 top)
|
||||
{
|
||||
Console.SetCursorPosition(left, top);
|
||||
Console.CursorVisible = false;
|
||||
}
|
||||
|
||||
private static void GrowSnake() => GrowSnake(Console.CursorLeft, Console.CursorTop);
|
||||
private static void GrowSnake(Int32 x, Int32 y)
|
||||
private static void GrowSnakes()
|
||||
{
|
||||
_snakeSegments.Add(new Position2D(x, y));
|
||||
if (_snakeSegmentsP1.Count > 0)
|
||||
{
|
||||
_snakeSegmentsP1.Add(_headP1);
|
||||
Console.SetCursorPosition(_headP1.X, _headP1.Y);
|
||||
Console.ForegroundColor = ConsoleColor.Green;
|
||||
Console.Write('#');
|
||||
}
|
||||
if (_snakeSegmentsP2.Count > 0)
|
||||
{
|
||||
_snakeSegmentsP2.Add(_headP2);
|
||||
Console.SetCursorPosition(_headP2.X, _headP2.Y);
|
||||
Console.ForegroundColor = ConsoleColor.Cyan;
|
||||
Console.Write('#');
|
||||
}
|
||||
}
|
||||
|
||||
private static void DeleteAt(Position2D pos) => DeleteAt(pos.X, pos.Y);
|
||||
@ -158,33 +183,59 @@ namespace ConsoleSnake
|
||||
|
||||
private static void AllSnakeThings()
|
||||
{
|
||||
if (!_growing)
|
||||
{
|
||||
DeleteAt(_snakeSegments.First());
|
||||
_snakeSegments.Remove(_snakeSegments.First());
|
||||
}
|
||||
else
|
||||
_growing = false;
|
||||
|
||||
ProcessMovement();
|
||||
if (_snakeSegments.Contains(new Position2D(Console.CursorLeft, Console.CursorTop)))
|
||||
|
||||
if (_snakeSegmentsP1.Contains(_headP1) || _snakeSegmentsP2.Contains(_headP1))
|
||||
{
|
||||
_running = false;
|
||||
foreach(Position2D segment in _snakeSegmentsP1)
|
||||
DeleteAt(segment);
|
||||
_snakeSegmentsP1.Clear();
|
||||
return;
|
||||
}
|
||||
|
||||
GrowSnake();
|
||||
Console.Write('#');
|
||||
--Console.CursorLeft;
|
||||
if(_food.X == Console.CursorLeft && _food.Y == Console.CursorTop)
|
||||
if (_snakeSegmentsP1.Contains(_headP2) || _snakeSegmentsP2.Contains(_headP2))
|
||||
{
|
||||
_growing = true;
|
||||
foreach (Position2D segment in _snakeSegmentsP2)
|
||||
DeleteAt(segment);
|
||||
_snakeSegmentsP2.Clear();
|
||||
return;
|
||||
}
|
||||
if(_snakeSegmentsP1.Count == 0 && _snakeSegmentsP2.Count == 0)
|
||||
_running = false;
|
||||
|
||||
GrowSnakes();
|
||||
|
||||
if(_food.X == _headP1.X && _food.Y == _headP1.Y)
|
||||
{
|
||||
_growingP1 = true;
|
||||
SpawnFood();
|
||||
} else if (_food.X == _headP2.X && _food.Y == _headP2.Y)
|
||||
{
|
||||
_growingP2 = true;
|
||||
SpawnFood();
|
||||
}
|
||||
|
||||
// growth P1
|
||||
if (!_growingP1 && _snakeSegmentsP1.Count > 0)
|
||||
{
|
||||
DeleteAt(_snakeSegmentsP1.First());
|
||||
_snakeSegmentsP1.Remove(_snakeSegmentsP1.First());
|
||||
}
|
||||
else
|
||||
_growingP1 = false;
|
||||
// growth P2
|
||||
if (!_growingP2 && _snakeSegmentsP2.Count > 0)
|
||||
{
|
||||
DeleteAt(_snakeSegmentsP2.First());
|
||||
_snakeSegmentsP2.Remove(_snakeSegmentsP2.First());
|
||||
}
|
||||
else
|
||||
_growingP2 = false;/**/
|
||||
}
|
||||
|
||||
private static void ProcessMovement()
|
||||
{
|
||||
switch (_direction)
|
||||
/*switch (_directionP1)
|
||||
{
|
||||
case Direction.Up:
|
||||
if (Console.CursorTop - 1 < _playArea.TopEdge)
|
||||
@ -210,6 +261,60 @@ namespace ConsoleSnake
|
||||
else
|
||||
++Console.CursorLeft;
|
||||
break;
|
||||
}/**/
|
||||
switch (_directionP1)
|
||||
{
|
||||
case Direction.Up:
|
||||
if (_headP1.Y - 1 < _playArea.TopEdge)
|
||||
_headP1.Y = _playArea.BottomEdge;
|
||||
else
|
||||
--_headP1.Y;
|
||||
break;
|
||||
case Direction.Down:
|
||||
if (_headP1.Y + 1 > _playArea.BottomEdge)
|
||||
_headP1.Y = _playArea.TopEdge;
|
||||
else
|
||||
++_headP1.Y;
|
||||
break;
|
||||
case Direction.Left:
|
||||
if (_headP1.X - 1 < _playArea.LeftEdge)
|
||||
_headP1.X = _playArea.RightEdge;
|
||||
else
|
||||
--_headP1.X;
|
||||
break;
|
||||
case Direction.Right:
|
||||
if (_headP1.X + 1 > _playArea.RightEdge)
|
||||
_headP1.X = _playArea.LeftEdge;
|
||||
else
|
||||
++_headP1.X;
|
||||
break;
|
||||
}
|
||||
switch (_directionP2)
|
||||
{
|
||||
case Direction.Up:
|
||||
if (_headP2.Y - 1 < _playArea.TopEdge)
|
||||
_headP2.Y = _playArea.BottomEdge;
|
||||
else
|
||||
--_headP2.Y;
|
||||
break;
|
||||
case Direction.Down:
|
||||
if (_headP2.Y + 1 > _playArea.BottomEdge)
|
||||
_headP2.Y = _playArea.TopEdge;
|
||||
else
|
||||
++_headP2.Y;
|
||||
break;
|
||||
case Direction.Left:
|
||||
if (_headP2.X - 1 < _playArea.LeftEdge)
|
||||
_headP2.X = _playArea.RightEdge;
|
||||
else
|
||||
--_headP2.X;
|
||||
break;
|
||||
case Direction.Right:
|
||||
if (_headP2.X + 1 > _playArea.RightEdge)
|
||||
_headP2.X = _playArea.LeftEdge;
|
||||
else
|
||||
++_headP2.X;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@ -219,31 +324,51 @@ namespace ConsoleSnake
|
||||
{
|
||||
switch(Console.ReadKey(true).Key)
|
||||
{
|
||||
// Movement P1
|
||||
case ConsoleKey.UpArrow:
|
||||
case ConsoleKey.W:
|
||||
if(_direction != Direction.Down)
|
||||
_direction = Direction.Up;
|
||||
if(_directionP1 != Direction.Down)
|
||||
_directionP1 = Direction.Up;
|
||||
break;
|
||||
case ConsoleKey.DownArrow:
|
||||
case ConsoleKey.S:
|
||||
if (_direction != Direction.Up)
|
||||
_direction = Direction.Down;
|
||||
if (_directionP1 != Direction.Up)
|
||||
_directionP1 = Direction.Down;
|
||||
break;
|
||||
case ConsoleKey.LeftArrow:
|
||||
case ConsoleKey.A:
|
||||
if (_direction != Direction.Right)
|
||||
_direction = Direction.Left;
|
||||
if (_directionP1 != Direction.Right)
|
||||
_directionP1 = Direction.Left;
|
||||
break;
|
||||
case ConsoleKey.RightArrow:
|
||||
case ConsoleKey.D:
|
||||
if (_direction != Direction.Left)
|
||||
_direction = Direction.Right;
|
||||
if (_directionP1 != Direction.Left)
|
||||
_directionP1 = Direction.Right;
|
||||
break;
|
||||
|
||||
// Movement P2
|
||||
case ConsoleKey.W:
|
||||
if (_directionP2 != Direction.Down)
|
||||
_directionP2 = Direction.Up;
|
||||
break;
|
||||
case ConsoleKey.S:
|
||||
if (_directionP2 != Direction.Up)
|
||||
_directionP2 = Direction.Down;
|
||||
break;
|
||||
case ConsoleKey.A:
|
||||
if (_directionP2 != Direction.Right)
|
||||
_directionP2 = Direction.Left;
|
||||
break;
|
||||
case ConsoleKey.D:
|
||||
if (_directionP2 != Direction.Left)
|
||||
_directionP2 = Direction.Right;
|
||||
break;
|
||||
|
||||
case ConsoleKey.D2:
|
||||
JoinPlayer2();
|
||||
break;
|
||||
|
||||
case ConsoleKey.Escape:
|
||||
_running = false;
|
||||
break;
|
||||
case ConsoleKey.G:
|
||||
_growing = !_growing;
|
||||
_growingP1 = !_growingP1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user