88 lines
2.3 KiB
C#
88 lines
2.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Snake
|
|
{
|
|
public class Game
|
|
{
|
|
//new Cell(10, 10, CellTypes.Snake)
|
|
Worm worm;
|
|
private Directions _direction = Directions.Down;
|
|
Map map;
|
|
Form1 form;
|
|
|
|
public Game(Form1 form)
|
|
{
|
|
this.form = form;
|
|
}
|
|
|
|
public void Start()
|
|
{
|
|
Cell startCell = new Cell(10, 10, CellTypes.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()
|
|
{
|
|
int x = worm.Body[0].X;
|
|
int y = worm.Body[0].Y;
|
|
|
|
Cell snakeCell = map.Cells[x, y];
|
|
Cell nextCell = GetNextCell(x, y);
|
|
|
|
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();
|
|
}
|
|
}
|
|
}
|