feat: initial project structure with Snake.Core, CLI and Avalonia skeleton
This commit is contained in:
50
Snake.CLI/ConsoleRenderer.cs
Normal file
50
Snake.CLI/ConsoleRenderer.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using Snake.Core;
|
||||
|
||||
namespace Snake.CLI;
|
||||
|
||||
internal static class ConsoleRenderer
|
||||
{
|
||||
public static void Render(SnakeGame game)
|
||||
{
|
||||
Console.Clear();
|
||||
Console.WriteLine($"Score: {game.Score}");
|
||||
|
||||
if (game.IsGameOver)
|
||||
Console.WriteLine("Game Over! Press R to restart or Q to quit.");
|
||||
|
||||
var board = game.Board;
|
||||
var snakeCells = game.Snake.Segments.ToHashSet();
|
||||
var food = game.Food.Position;
|
||||
|
||||
Console.Write('+');
|
||||
Console.Write(new string('-', board.Width * 2));
|
||||
Console.WriteLine('+');
|
||||
|
||||
for (var y = 0; y < board.Height; y++)
|
||||
{
|
||||
Console.Write('|');
|
||||
|
||||
for (var x = 0; x < board.Width; x++)
|
||||
{
|
||||
var position = new Position(x, y);
|
||||
var symbol = position == food
|
||||
? '@'
|
||||
: position == game.Snake.Head
|
||||
? 'O'
|
||||
: snakeCells.Contains(position)
|
||||
? 'o'
|
||||
: ' ';
|
||||
|
||||
Console.Write(symbol);
|
||||
Console.Write(' ');
|
||||
}
|
||||
|
||||
Console.WriteLine('|');
|
||||
}
|
||||
|
||||
Console.Write('+');
|
||||
Console.Write(new string('-', board.Width * 2));
|
||||
Console.WriteLine('+');
|
||||
Console.WriteLine("Controls: Arrow keys to move, R to restart, Q to quit.");
|
||||
}
|
||||
}
|
||||
44
Snake.CLI/Program.cs
Normal file
44
Snake.CLI/Program.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using Snake.CLI;
|
||||
using Snake.Core;
|
||||
|
||||
const int boardWidth = 20;
|
||||
const int boardHeight = 15;
|
||||
const int tickMs = 120;
|
||||
|
||||
SnakeGame game = new(boardWidth, boardHeight);
|
||||
ConsoleRenderer.Render(game);
|
||||
|
||||
while (true)
|
||||
{
|
||||
if (Console.KeyAvailable)
|
||||
{
|
||||
var key = Console.ReadKey(intercept: true).Key;
|
||||
|
||||
switch (key)
|
||||
{
|
||||
case ConsoleKey.UpArrow:
|
||||
game.SetDirection(Direction.Up);
|
||||
break;
|
||||
case ConsoleKey.DownArrow:
|
||||
game.SetDirection(Direction.Down);
|
||||
break;
|
||||
case ConsoleKey.LeftArrow:
|
||||
game.SetDirection(Direction.Left);
|
||||
break;
|
||||
case ConsoleKey.RightArrow:
|
||||
game.SetDirection(Direction.Right);
|
||||
break;
|
||||
case ConsoleKey.Q:
|
||||
return;
|
||||
case ConsoleKey.R when game.IsGameOver:
|
||||
game = new SnakeGame(boardWidth, boardHeight);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!game.IsGameOver)
|
||||
game.Tick();
|
||||
|
||||
ConsoleRenderer.Render(game);
|
||||
Thread.Sleep(tickMs);
|
||||
}
|
||||
14
Snake.CLI/Snake.CLI.csproj
Normal file
14
Snake.CLI/Snake.CLI.csproj
Normal file
@@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Snake.Core\Snake.Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user