Finished up game
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user