Files
snake/Snake/Worm.cs
2026-02-03 13:44:30 +01:00

61 lines
1.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Snake
{
public class Worm
{
public List<Cell> Body = new List<Cell>();
public Worm(Cell startCell)
{
Body.Add(startCell);
startCell.Color = ColorFromHue((float)(Body.Count * 8));
}
public void Move(int x, int y, Map map)
{
Cell swapCell = map.Cells[x, y];
for (int i = 1; i < Body.Count; i++)
{
map.SwapCells(swapCell, Body[i]);
swapCell = Body[i];
}
}
public void Eat(ref Cell cell)
{
cell.Type = CellTypes.Snake;
cell.Color = ColorFromHue((float)(Body.Count * 8));
Body.Insert(1, cell);
}
public Color ColorFromHue(float hue)
{
hue = hue % 360f;
float c = 1f;
float x = c * (1 - Math.Abs((hue / 60f) % 2 - 1));
float m = 0f;
float r = 0, g = 0, b = 0;
if (hue < 60) { r = c; g = x; }
else if (hue < 120) { r = x; g = c; }
else if (hue < 180) { g = c; b = x; }
else if (hue < 240) { g = x; b = c; }
else if (hue < 300) { r = x; b = c; }
else { r = c; b = x; }
return Color.FromArgb(
(int)((r + m) * 255),
(int)((g + m) * 255),
(int)((b + m) * 255)
);
}
}
}