Some refactoring

Added frame time target
master
Constantin Köpplinger 2023-03-29 23:48:14 +02:00
parent 854d1bfde1
commit 0f752bdb1c
1 changed files with 40 additions and 24 deletions

View File

@ -37,6 +37,7 @@ namespace ConsoleSnake
private struct Snake private struct Snake
{ {
public bool Alive = true;
public Direction Direction { get; set; } = Direction.Down; public Direction Direction { get; set; } = Direction.Down;
public Position2D HeadPosition { get; set; } = new Position2D(); public Position2D HeadPosition { get; set; } = new Position2D();
public List<Position2D> Segments { get; set; } = new List<Position2D>(); public List<Position2D> Segments { get; set; } = new List<Position2D>();
@ -53,8 +54,13 @@ namespace ConsoleSnake
private static Snake _snakeP2 = new Snake(); private static Snake _snakeP2 = new Snake();
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();
private static Int32 _desiredFrameTime = 75;
private static DateTime _frameTiming = DateTime.Now;
static void Main(string[] args) static void Main(string[] args)
{ {
String blub = null;
WriteColAt(blub, ConsoleColor.DarkBlue, 0, 0);
Console.ReadKey(); Console.ReadKey();
Console.SetBufferSize(Console.WindowWidth, Console.WindowHeight); Console.SetBufferSize(Console.WindowWidth, Console.WindowHeight);
@ -68,16 +74,22 @@ namespace ConsoleSnake
while (_running) while (_running)
{ {
DateTime beginFrame = DateTime.Now; TimeFrame();
HandleInput(); HandleInput();
AllSnakeThings(); AllSnakeThings();
TimeSpan frameTime = DateTime.Now.Subtract(beginFrame);
if(50 - frameTime.Milliseconds > 0)
Thread.Sleep(50 - frameTime.Milliseconds);
} }
} }
private static void TimeFrame()
{
TimeSpan frameTime = DateTime.Now.Subtract(_frameTiming);
if(frameTime.Milliseconds < _desiredFrameTime)
{
Thread.Sleep(_desiredFrameTime - frameTime.Milliseconds);
}
_frameTiming = DateTime.Now;
}
private static void DrawPlayArea() private static void DrawPlayArea()
{ {
Console.BackgroundColor = ConsoleColor.DarkGray; Console.BackgroundColor = ConsoleColor.DarkGray;
@ -116,22 +128,24 @@ namespace ConsoleSnake
SpawnFood(); SpawnFood();
} }
private static void SpawnFood() private static bool SnakeCollision(Position2D position)
{ {
//Position2D oldCursorPos = new Position2D(Console.CursorLeft, Console.CursorTop); return _snakeP1.Segments.Contains(position) || _snakeP2.Segments.Contains(position);
do
{
_food = new Position2D(rng.Next(_playArea.LeftEdge+1, _playArea.RightEdge), rng.Next(_playArea.TopEdge, _playArea.BottomEdge));
} while (_snakeP1.Segments.Contains(_food) || _snakeP2.Segments.Contains(_food));
MoveCursor(_food);
Console.ForegroundColor = ConsoleColor.Red;
Console.Write('@');
//MoveCursor(oldCursorPos);
} }
private static void WriteColAt(char text, ConsoleColor color, Position2D pos) => WriteColAt(text.ToString(), color, pos.X, pos.Y); private static void SpawnFood()
{
while (SnakeCollision(_food = new Position2D(rng.Next(_playArea.LeftEdge + 1, _playArea.RightEdge), rng.Next(_playArea.TopEdge, _playArea.BottomEdge))))
{
}
WriteColAt('@', ConsoleColor.Red, _food);
}
private static void WriteColAt(object text, ConsoleColor color, Position2D pos) => WriteColAt(text.ToString(), color, pos.X, pos.Y);
private static void WriteColAt(string text, ConsoleColor color, Position2D pos) => WriteColAt(text, color, pos.X, pos.Y); private static void WriteColAt(string text, ConsoleColor color, Position2D pos) => WriteColAt(text, color, pos.X, pos.Y);
private static void WriteColAt(char text, ConsoleColor color, Int32 x, Int32 y) => WriteColAt(text.ToString(), color, x, y); private static void WriteColAt(object text, ConsoleColor color, Int32 x, Int32 y) => WriteColAt(text.ToString(), color, x, y);
private static void WriteColAt(string text, ConsoleColor color, Int32 x, Int32 y) private static void WriteColAt(string text, ConsoleColor color, Int32 x, Int32 y)
{ {
Console.SetCursorPosition(x, y); Console.SetCursorPosition(x, y);
@ -147,12 +161,12 @@ namespace ConsoleSnake
} }
private static void GrowSnakes() private static void GrowSnakes()
{ {
if (_snakeP1.Segments.Count > 0) if (_snakeP1.Alive)
{ {
_snakeP1.Segments.Add(_snakeP1.HeadPosition); _snakeP1.Segments.Add(_snakeP1.HeadPosition);
WriteColAt('#', ConsoleColor.Green, _snakeP1.HeadPosition); WriteColAt('#', ConsoleColor.Green, _snakeP1.HeadPosition);
} }
if (_snakeP2.Segments.Count > 0) if (_snakeP2.Alive)
{ {
_snakeP2.Segments.Add(_snakeP2.HeadPosition); _snakeP2.Segments.Add(_snakeP2.HeadPosition);
WriteColAt('#', ConsoleColor.Cyan, _snakeP2.HeadPosition); WriteColAt('#', ConsoleColor.Cyan, _snakeP2.HeadPosition);
@ -182,6 +196,7 @@ namespace ConsoleSnake
foreach(Position2D segment in _snakeP1.Segments) foreach(Position2D segment in _snakeP1.Segments)
DeleteAt(segment); DeleteAt(segment);
_snakeP1.Segments.Clear(); _snakeP1.Segments.Clear();
_snakeP1.Alive = false;
return; return;
} }
// P2 pepsi? // P2 pepsi?
@ -190,14 +205,13 @@ namespace ConsoleSnake
foreach (Position2D segment in _snakeP2.Segments) foreach (Position2D segment in _snakeP2.Segments)
DeleteAt(segment); DeleteAt(segment);
_snakeP2.Segments.Clear(); _snakeP2.Segments.Clear();
_snakeP2.Alive = false;
return; return;
} }
// everyone pepsi? game over?! // everyone pepsi? game over?!
if(_snakeP1.Segments.Count == 0 && _snakeP2.Segments.Count == 0) if(!_snakeP1.Alive && !_snakeP2.Alive)
_running = false; _running = false;
GrowSnakes();
if(Collision(_food, _snakeP1.HeadPosition)) if(Collision(_food, _snakeP1.HeadPosition))
{ {
_snakeP1.Growing = true; _snakeP1.Growing = true;
@ -208,7 +222,7 @@ namespace ConsoleSnake
SpawnFood(); SpawnFood();
} }
// growth P1 // Delete P1 tail if not growing
if (!_snakeP1.Growing && _snakeP1.Segments.Count > 0) if (!_snakeP1.Growing && _snakeP1.Segments.Count > 0)
{ {
DeleteAt(_snakeP1.Segments.First()); DeleteAt(_snakeP1.Segments.First());
@ -216,7 +230,7 @@ namespace ConsoleSnake
} }
else else
_snakeP1.Growing = false; _snakeP1.Growing = false;
// growth P2 // Delete P2 tail if not growing
if (!_snakeP2.Growing && _snakeP2.Segments.Count > 0) if (!_snakeP2.Growing && _snakeP2.Segments.Count > 0)
{ {
DeleteAt(_snakeP2.Segments.First()); DeleteAt(_snakeP2.Segments.First());
@ -224,6 +238,8 @@ namespace ConsoleSnake
} }
else else
_snakeP2.Growing = false; _snakeP2.Growing = false;
GrowSnakes();
} }
private static void ProcessMovement() private static void ProcessMovement()