eating
This commit is contained in:
1
Snake/Form1.Designer.cs
generated
1
Snake/Form1.Designer.cs
generated
@@ -57,6 +57,7 @@
|
||||
Name = "Form1";
|
||||
Text = "Form1";
|
||||
Load += Form1_Load;
|
||||
KeyDown += Form1_KeyDown;
|
||||
((System.ComponentModel.ISupportInitialize)gamearea).EndInit();
|
||||
ResumeLayout(false);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user