Finished up game

This commit is contained in:
maxwes08
2026-02-03 13:44:30 +01:00
parent df6832419f
commit 46816d3dbf
5 changed files with 105 additions and 24 deletions

View File

@@ -11,12 +11,14 @@ namespace Snake
public int X;
public int Y;
public CellTypes Type;
public Color Color;
public Cell(int x, int y, CellTypes type)
{
X = x;
Y = y;
Type = type;
Color = Color.White;
}
}
}

View File

@@ -45,14 +45,14 @@
// timer
//
timer.Enabled = true;
timer.Interval = 500;
timer.Interval = 200;
timer.Tick += timer_Tick;
//
// Form1
//
AutoScaleDimensions = new SizeF(10F, 25F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 450);
ClientSize = new Size(800, 688);
Controls.Add(gamearea);
Name = "Form1";
Text = "Form1";
@@ -63,7 +63,7 @@
}
#endregion
private System.Windows.Forms.Timer timer;
public PictureBox gamearea;
public System.Windows.Forms.Timer timer;
}
}

View File

@@ -8,12 +8,14 @@ namespace Snake
{
public class Game
{
//new Cell(10, 10, CellTypes.Snake)
Worm worm;
private Directions _direction = Directions.Down;
Random random = new Random();
Map map;
Form1 form;
int mapSize = 20;
public Game(Form1 form)
{
this.form = form;
@@ -22,28 +24,27 @@ namespace Snake
public void Start()
{
Cell startCell = new Cell(10, 10, CellTypes.Snake);
map = new Map(20, 20);
map = new Map(mapSize, mapSize);
map.SetCell(startCell);
worm = new Worm(startCell);
map.SetCell(new Cell(15, 10, CellTypes.Food));
map.SetCell(new Cell(15, 11, CellTypes.Food));
map.SetCell(new Cell(15, 12, CellTypes.Food));
map.SetCell(new Cell(15, 13, CellTypes.Food));
map.SetCell(new Cell(15, 14, CellTypes.Food));
map.SetCell(new Cell(15, 15, CellTypes.Food));
map.SetCell(new Cell(15, 16, CellTypes.Food));
map.SetCell(new Cell(15, 17, CellTypes.Food));
map.SetCell(new Cell(15, 18, CellTypes.Food));
SpawnApple();
}
public void UpdateInput(Directions direction)
{
if (_direction == Directions.Down && direction == Directions.Up) return;
if (_direction == Directions.Up && direction == Directions.Down) return;
if (_direction == Directions.Left && direction == Directions.Right) return;
if (_direction == Directions.Right && direction == Directions.Left) return;
_direction = direction;
}
public Cell GetNextCell(int x, int y)
public Cell GetNextCell()
{
int x = worm.Body[0].X;
int y = worm.Body[0].Y;
if (_direction == Directions.Up)
{
return map.Cells[x, y + 1];
@@ -63,18 +64,66 @@ namespace Snake
return map.Cells[x + 1, y];
}
public void Update()
public bool IsNextPosOutOfBounds()
{
int x = worm.Body[0].X;
int y = worm.Body[0].Y;
if (_direction == Directions.Up)
if (y + 1 >= mapSize) return true;
if (_direction == Directions.Left)
if (x - 1 < 0) return true;
if (_direction == Directions.Down)
if (y - 1 < 0) return true;
if (_direction == Directions.Right)
if (x + 1 >= mapSize) return true;
return false;
}
public void SpawnApple()
{
int x = random.Next(mapSize);
int y = random.Next(mapSize);
Cell cell = map.Cells[x, y];
if (cell.Type == CellTypes.None)
{
cell.Type = CellTypes.Food;
return;
}
SpawnApple();
}
public void Update()
{
if (IsNextPosOutOfBounds())
{
Die();
return;
}
int x = worm.Body[0].X;
int y = worm.Body[0].Y;
Cell snakeCell = map.Cells[x, y];
Cell nextCell = GetNextCell(x, y);
Cell nextCell = GetNextCell();
if (nextCell.Type == CellTypes.Snake)
{
Die();
return;
}
if (nextCell.Type == CellTypes.Food)
{
worm.Eat(ref nextCell);
//map.SetCell(nextCell);
SpawnApple();
}
map.SwapCells(snakeCell, nextCell);
@@ -83,5 +132,10 @@ namespace Snake
map.Render(form.gamearea.CreateGraphics());
form.Invalidate();
}
public void Die()
{
form.timer.Enabled = false;
}
}
}

View File

@@ -63,7 +63,7 @@ namespace Snake
}
else if (cell.Type == CellTypes.Snake)
{
brush.Color = Color.Green;
brush.Color = cell.Color;
}
else if (cell.Type == CellTypes.Food)
{
@@ -72,9 +72,10 @@ namespace Snake
int totalHeight = CELL_SIZE * _h;
graphics.FillRectangle(brush,
x * 20,
totalHeight - y * 20,
graphics.FillRectangle(
brush,
x * CELL_SIZE,
totalHeight - (y + 1) * CELL_SIZE,
CELL_SIZE,
CELL_SIZE
);

View File

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