From df6832419fbf6c517e3f4b25cc09e4eb87dd99a0 Mon Sep 17 00:00:00 2001 From: maxwes08 Date: Fri, 30 Jan 2026 12:14:01 +0100 Subject: [PATCH] eating --- Snake/Form1.Designer.cs | 1 + Snake/Form1.cs | 15 +++++++++++ Snake/Game.cs | 55 ++++++++++++++++++++++++++++++++++++----- Snake/Worm.cs | 17 +++++++++++++ 4 files changed, 82 insertions(+), 6 deletions(-) diff --git a/Snake/Form1.Designer.cs b/Snake/Form1.Designer.cs index 520c5d9..98a8051 100644 --- a/Snake/Form1.Designer.cs +++ b/Snake/Form1.Designer.cs @@ -57,6 +57,7 @@ Name = "Form1"; Text = "Form1"; Load += Form1_Load; + KeyDown += Form1_KeyDown; ((System.ComponentModel.ISupportInitialize)gamearea).EndInit(); ResumeLayout(false); } diff --git a/Snake/Form1.cs b/Snake/Form1.cs index f820597..2802c0f 100644 --- a/Snake/Form1.cs +++ b/Snake/Form1.cs @@ -20,5 +20,20 @@ namespace Snake _game.Update(); Invalidate(); } + + private void Form1_KeyDown(object sender, KeyEventArgs e) + { + switch (e.KeyCode) + { + case Keys.W: + _game.UpdateInput(Directions.Up); break; + case Keys.A: + _game.UpdateInput(Directions.Left); break; + case Keys.S: + _game.UpdateInput(Directions.Down); break; + case Keys.D: + _game.UpdateInput(Directions.Right); break; + } + } } } diff --git a/Snake/Game.cs b/Snake/Game.cs index 513dfc0..234ad11 100644 --- a/Snake/Game.cs +++ b/Snake/Game.cs @@ -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(); } } } diff --git a/Snake/Worm.cs b/Snake/Worm.cs index e8ce675..820aeca 100644 --- a/Snake/Worm.cs +++ b/Snake/Worm.cs @@ -15,5 +15,22 @@ namespace Snake Body.Add(startCell); } + 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; + Body.Add(cell); + } + } }