Multiplayer is in. It's messy.

This commit is contained in:
morpha 2023-03-27 23:46:43 +02:00
parent fd552b1429
commit 3da34fadb2

View File

@ -36,9 +36,14 @@ namespace ConsoleSnake
private static PlayArea _playArea = new PlayArea(1, 4, Console.WindowWidth - 2, Console.WindowHeight - 2); private static PlayArea _playArea = new PlayArea(1, 4, Console.WindowWidth - 2, Console.WindowHeight - 2);
private static bool _running = true; private static bool _running = true;
private static bool _growing = false; private static bool _growingP1 = false;
private static Direction _direction = Direction.Up; private static bool _growingP2 = false;
private static List<Position2D> _snakeSegments = new List<Position2D>(); 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 Position2D _food = new Position2D(-1,-1);
private static Random rng = new Random(); private static Random rng = new Random();
static void Main(string[] args) static void Main(string[] args)
@ -103,14 +108,18 @@ namespace ConsoleSnake
{ {
_playArea = new PlayArea(1, 4, Console.WindowWidth - 2, Console.WindowHeight - 2); _playArea = new PlayArea(1, 4, Console.WindowWidth - 2, Console.WindowHeight - 2);
_running = true; _running = true;
_growing = false; _growingP1 = false;
_direction = Direction.Up; _growingP2 = false;
_snakeSegments.Clear(); _directionP1 = Direction.Right;
_directionP2 = Direction.Left;
_snakeSegmentsP1.Clear();
_snakeSegmentsP2.Clear();
// Set up anew // 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(); SpawnFood();
MoveCursor(_snakeSegments.Last()); MoveCursor(_snakeSegmentsP1.Last());
} }
@ -120,7 +129,7 @@ namespace ConsoleSnake
do do
{ {
_food = new Position2D(rng.Next(_playArea.LeftEdge+1, _playArea.RightEdge), rng.Next(_playArea.TopEdge, _playArea.BottomEdge)); _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); MoveCursor(_food);
Console.ForegroundColor = ConsoleColor.Red; Console.ForegroundColor = ConsoleColor.Red;
Console.Write('@'); Console.Write('@');
@ -128,17 +137,33 @@ namespace ConsoleSnake
MoveCursor(oldCursorPos); MoveCursor(oldCursorPos);
} }
private static void JoinPlayer2()
{
}
private static void MoveCursor(Position2D pos) => MoveCursor(pos.X, pos.Y); private static void MoveCursor(Position2D pos) => MoveCursor(pos.X, pos.Y);
private static void MoveCursor(Int32 left, Int32 top) private static void MoveCursor(Int32 left, Int32 top)
{ {
Console.SetCursorPosition(left, top); Console.SetCursorPosition(left, top);
Console.CursorVisible = false; Console.CursorVisible = false;
} }
private static void GrowSnakes()
private static void GrowSnake() => GrowSnake(Console.CursorLeft, Console.CursorTop);
private static void GrowSnake(Int32 x, Int32 y)
{ {
_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); private static void DeleteAt(Position2D pos) => DeleteAt(pos.X, pos.Y);
@ -158,33 +183,59 @@ namespace ConsoleSnake
private static void AllSnakeThings() private static void AllSnakeThings()
{ {
if (!_growing)
{
DeleteAt(_snakeSegments.First());
_snakeSegments.Remove(_snakeSegments.First());
}
else
_growing = false;
ProcessMovement(); 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; return;
} }
if (_snakeSegmentsP1.Contains(_headP2) || _snakeSegmentsP2.Contains(_headP2))
GrowSnake();
Console.Write('#');
--Console.CursorLeft;
if(_food.X == Console.CursorLeft && _food.Y == Console.CursorTop)
{ {
_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(); 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() private static void ProcessMovement()
{ {
switch (_direction) /*switch (_directionP1)
{ {
case Direction.Up: case Direction.Up:
if (Console.CursorTop - 1 < _playArea.TopEdge) if (Console.CursorTop - 1 < _playArea.TopEdge)
@ -210,6 +261,60 @@ namespace ConsoleSnake
else else
++Console.CursorLeft; ++Console.CursorLeft;
break; 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) switch(Console.ReadKey(true).Key)
{ {
// Movement P1
case ConsoleKey.UpArrow: case ConsoleKey.UpArrow:
case ConsoleKey.W: if(_directionP1 != Direction.Down)
if(_direction != Direction.Down) _directionP1 = Direction.Up;
_direction = Direction.Up;
break; break;
case ConsoleKey.DownArrow: case ConsoleKey.DownArrow:
case ConsoleKey.S: if (_directionP1 != Direction.Up)
if (_direction != Direction.Up) _directionP1 = Direction.Down;
_direction = Direction.Down;
break; break;
case ConsoleKey.LeftArrow: case ConsoleKey.LeftArrow:
case ConsoleKey.A: if (_directionP1 != Direction.Right)
if (_direction != Direction.Right) _directionP1 = Direction.Left;
_direction = Direction.Left;
break; break;
case ConsoleKey.RightArrow: case ConsoleKey.RightArrow:
case ConsoleKey.D: if (_directionP1 != Direction.Left)
if (_direction != Direction.Left) _directionP1 = Direction.Right;
_direction = Direction.Right;
break; 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: case ConsoleKey.Escape:
_running = false; _running = false;
break; break;
case ConsoleKey.G: case ConsoleKey.G:
_growing = !_growing; _growingP1 = !_growingP1;
break; break;
} }
} }