fixed window

This commit is contained in:
max
2026-02-04 20:29:49 +01:00
parent 46816d3dbf
commit c09428bb31
4 changed files with 35 additions and 37 deletions

View File

@@ -36,7 +36,9 @@
// //
// gamearea // gamearea
// //
gamearea.Location = new Point(200, 25); gamearea.Anchor = AnchorStyles.None;
gamearea.Location = new Point(43, 50);
gamearea.Margin = new Padding(2);
gamearea.Name = "gamearea"; gamearea.Name = "gamearea";
gamearea.Size = new Size(400, 400); gamearea.Size = new Size(400, 400);
gamearea.TabIndex = 0; gamearea.TabIndex = 0;
@@ -50,10 +52,11 @@
// //
// Form1 // Form1
// //
AutoScaleDimensions = new SizeF(10F, 25F); AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 688); ClientSize = new Size(484, 461);
Controls.Add(gamearea); Controls.Add(gamearea);
Margin = new Padding(2);
Name = "Form1"; Name = "Form1";
Text = "Form1"; Text = "Form1";
Load += Form1_Load; Load += Form1_Load;

View File

@@ -15,6 +15,7 @@ namespace Snake
Form1 form; Form1 form;
int mapSize = 20; int mapSize = 20;
float colorMultiplier = 8f;
public Game(Form1 form) public Game(Form1 form)
{ {
@@ -27,6 +28,7 @@ namespace Snake
map = new Map(mapSize, mapSize); map = new Map(mapSize, mapSize);
map.SetCell(startCell); map.SetCell(startCell);
worm = new Worm(startCell); worm = new Worm(startCell);
startCell.Color = ColorFromHue(0f);
SpawnApple(); SpawnApple();
} }
@@ -94,6 +96,7 @@ namespace Snake
if (cell.Type == CellTypes.None) if (cell.Type == CellTypes.None)
{ {
cell.Type = CellTypes.Food; cell.Type = CellTypes.Food;
cell.Color = ColorFromHue(worm.Body.Count*colorMultiplier);
return; return;
} }
@@ -137,5 +140,28 @@ namespace Snake
{ {
form.timer.Enabled = false; form.timer.Enabled = false;
} }
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)
);
}
} }
} }

View File

@@ -57,17 +57,11 @@ namespace Snake
{ {
Cell cell = Cells[x,y]; Cell cell = Cells[x,y];
brush.Color = cell.Color;
if (cell.Type == CellTypes.None) if (cell.Type == CellTypes.None)
{ {
brush.Color = Color.LightGray; brush.Color = Color.Black;
}
else if (cell.Type == CellTypes.Snake)
{
brush.Color = cell.Color;
}
else if (cell.Type == CellTypes.Food)
{
brush.Color = Color.Red;
} }
int totalHeight = CELL_SIZE * _h; int totalHeight = CELL_SIZE * _h;

View File

@@ -13,7 +13,6 @@ namespace Snake
public Worm(Cell startCell) public Worm(Cell startCell)
{ {
Body.Add(startCell); Body.Add(startCell);
startCell.Color = ColorFromHue((float)(Body.Count * 8));
} }
public void Move(int x, int y, Map map) public void Move(int x, int y, Map map)
@@ -30,31 +29,7 @@ namespace Snake
public void Eat(ref Cell cell) public void Eat(ref Cell cell)
{ {
cell.Type = CellTypes.Snake; cell.Type = CellTypes.Snake;
cell.Color = ColorFromHue((float)(Body.Count * 8));
Body.Insert(1, cell); 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)
);
}
} }
} }