This commit is contained in:
maxwes08
2026-01-30 12:14:01 +01:00
parent b6e5c3dfbc
commit df6832419f
4 changed files with 82 additions and 6 deletions

View File

@@ -10,7 +10,7 @@ namespace Snake
{
//new Cell(10, 10, CellTypes.Snake)
Worm worm;
Directions direction = Directions.Down;
private Directions _direction = Directions.Down;
Map map;
Form1 form;
@@ -25,20 +25,63 @@ namespace Snake
map = new Map(20, 20);
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));
}
public void UpdateInput(Directions direction)
{
_direction = direction;
}
public Cell GetNextCell(int x, int y)
{
if (_direction == Directions.Up)
{
return map.Cells[x, y + 1];
}
if (_direction == Directions.Left)
{
return map.Cells[x - 1, y];
}
if (_direction == Directions.Down)
{
return map.Cells[x, y - 1];
}
// Right
return map.Cells[x + 1, y];
}
public void Update()
{
map.Render(form.gamearea.CreateGraphics());
form.Invalidate();
int x = worm.Body[0].X;
int y = worm.Body[0].Y;
Cell snakeCell = map.Cells[x, y];
Cell cellBelow = map.Cells[x, y-1];
Cell nextCell = GetNextCell(x, y);
map.SwapCells(snakeCell, cellBelow);
if (nextCell.Type == CellTypes.Food)
{
worm.Eat(ref nextCell);
//map.SetCell(nextCell);
}
map.SwapCells(snakeCell, nextCell);
worm.Move(x, y, map);
map.Render(form.gamearea.CreateGraphics());
form.Invalidate();
}
}
}