Finished up game
This commit is contained in:
@@ -11,12 +11,14 @@ namespace Snake
|
|||||||
public int X;
|
public int X;
|
||||||
public int Y;
|
public int Y;
|
||||||
public CellTypes Type;
|
public CellTypes Type;
|
||||||
|
public Color Color;
|
||||||
|
|
||||||
public Cell(int x, int y, CellTypes type)
|
public Cell(int x, int y, CellTypes type)
|
||||||
{
|
{
|
||||||
X = x;
|
X = x;
|
||||||
Y = y;
|
Y = y;
|
||||||
Type = type;
|
Type = type;
|
||||||
|
Color = Color.White;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
6
Snake/Form1.Designer.cs
generated
6
Snake/Form1.Designer.cs
generated
@@ -45,14 +45,14 @@
|
|||||||
// timer
|
// timer
|
||||||
//
|
//
|
||||||
timer.Enabled = true;
|
timer.Enabled = true;
|
||||||
timer.Interval = 500;
|
timer.Interval = 200;
|
||||||
timer.Tick += timer_Tick;
|
timer.Tick += timer_Tick;
|
||||||
//
|
//
|
||||||
// Form1
|
// Form1
|
||||||
//
|
//
|
||||||
AutoScaleDimensions = new SizeF(10F, 25F);
|
AutoScaleDimensions = new SizeF(10F, 25F);
|
||||||
AutoScaleMode = AutoScaleMode.Font;
|
AutoScaleMode = AutoScaleMode.Font;
|
||||||
ClientSize = new Size(800, 450);
|
ClientSize = new Size(800, 688);
|
||||||
Controls.Add(gamearea);
|
Controls.Add(gamearea);
|
||||||
Name = "Form1";
|
Name = "Form1";
|
||||||
Text = "Form1";
|
Text = "Form1";
|
||||||
@@ -63,7 +63,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
private System.Windows.Forms.Timer timer;
|
|
||||||
public PictureBox gamearea;
|
public PictureBox gamearea;
|
||||||
|
public System.Windows.Forms.Timer timer;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,12 +8,14 @@ namespace Snake
|
|||||||
{
|
{
|
||||||
public class Game
|
public class Game
|
||||||
{
|
{
|
||||||
//new Cell(10, 10, CellTypes.Snake)
|
|
||||||
Worm worm;
|
Worm worm;
|
||||||
private Directions _direction = Directions.Down;
|
private Directions _direction = Directions.Down;
|
||||||
|
Random random = new Random();
|
||||||
Map map;
|
Map map;
|
||||||
Form1 form;
|
Form1 form;
|
||||||
|
|
||||||
|
int mapSize = 20;
|
||||||
|
|
||||||
public Game(Form1 form)
|
public Game(Form1 form)
|
||||||
{
|
{
|
||||||
this.form = form;
|
this.form = form;
|
||||||
@@ -22,28 +24,27 @@ namespace Snake
|
|||||||
public void Start()
|
public void Start()
|
||||||
{
|
{
|
||||||
Cell startCell = new Cell(10, 10, CellTypes.Snake);
|
Cell startCell = new Cell(10, 10, CellTypes.Snake);
|
||||||
map = new Map(20, 20);
|
map = new Map(mapSize, mapSize);
|
||||||
map.SetCell(startCell);
|
map.SetCell(startCell);
|
||||||
worm = new Worm(startCell);
|
worm = new Worm(startCell);
|
||||||
|
SpawnApple();
|
||||||
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)
|
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;
|
_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)
|
if (_direction == Directions.Up)
|
||||||
{
|
{
|
||||||
return map.Cells[x, y + 1];
|
return map.Cells[x, y + 1];
|
||||||
@@ -63,18 +64,66 @@ namespace Snake
|
|||||||
return map.Cells[x + 1, y];
|
return map.Cells[x + 1, y];
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Update()
|
public bool IsNextPosOutOfBounds()
|
||||||
{
|
{
|
||||||
int x = worm.Body[0].X;
|
int x = worm.Body[0].X;
|
||||||
int y = worm.Body[0].Y;
|
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 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)
|
if (nextCell.Type == CellTypes.Food)
|
||||||
{
|
{
|
||||||
worm.Eat(ref nextCell);
|
worm.Eat(ref nextCell);
|
||||||
//map.SetCell(nextCell);
|
SpawnApple();
|
||||||
}
|
}
|
||||||
|
|
||||||
map.SwapCells(snakeCell, nextCell);
|
map.SwapCells(snakeCell, nextCell);
|
||||||
@@ -83,5 +132,10 @@ namespace Snake
|
|||||||
map.Render(form.gamearea.CreateGraphics());
|
map.Render(form.gamearea.CreateGraphics());
|
||||||
form.Invalidate();
|
form.Invalidate();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void Die()
|
||||||
|
{
|
||||||
|
form.timer.Enabled = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -63,7 +63,7 @@ namespace Snake
|
|||||||
}
|
}
|
||||||
else if (cell.Type == CellTypes.Snake)
|
else if (cell.Type == CellTypes.Snake)
|
||||||
{
|
{
|
||||||
brush.Color = Color.Green;
|
brush.Color = cell.Color;
|
||||||
}
|
}
|
||||||
else if (cell.Type == CellTypes.Food)
|
else if (cell.Type == CellTypes.Food)
|
||||||
{
|
{
|
||||||
@@ -72,9 +72,10 @@ namespace Snake
|
|||||||
|
|
||||||
int totalHeight = CELL_SIZE * _h;
|
int totalHeight = CELL_SIZE * _h;
|
||||||
|
|
||||||
graphics.FillRectangle(brush,
|
graphics.FillRectangle(
|
||||||
x * 20,
|
brush,
|
||||||
totalHeight - y * 20,
|
x * CELL_SIZE,
|
||||||
|
totalHeight - (y + 1) * CELL_SIZE,
|
||||||
CELL_SIZE,
|
CELL_SIZE,
|
||||||
CELL_SIZE
|
CELL_SIZE
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ namespace Snake
|
|||||||
public Worm(Cell startCell)
|
public Worm(Cell startCell)
|
||||||
{
|
{
|
||||||
Body.Add(startCell);
|
Body.Add(startCell);
|
||||||
|
startCell.Color = ColorFromHue((float)(Body.Count * 8));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Move(int x, int y, Map map)
|
public void Move(int x, int y, Map map)
|
||||||
@@ -29,8 +30,31 @@ namespace Snake
|
|||||||
public void Eat(ref Cell cell)
|
public void Eat(ref Cell cell)
|
||||||
{
|
{
|
||||||
cell.Type = CellTypes.Snake;
|
cell.Type = CellTypes.Snake;
|
||||||
Body.Add(cell);
|
cell.Color = ColorFromHue((float)(Body.Count * 8));
|
||||||
|
Body.Insert(1, cell);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Color ColorFromHue(float hue)
|
||||||
|
{
|
||||||
|
hue = hue % 360f;
|
||||||
|
float c = 1f;
|
||||||
|
float x = c * (1 - Math.Abs((hue / 60f) % 2 - 1));
|
||||||
|
float m = 0f;
|
||||||
|
|
||||||
|
float r = 0, g = 0, b = 0;
|
||||||
|
|
||||||
|
if (hue < 60) { r = c; g = x; }
|
||||||
|
else if (hue < 120) { r = x; g = c; }
|
||||||
|
else if (hue < 180) { g = c; b = x; }
|
||||||
|
else if (hue < 240) { g = x; b = c; }
|
||||||
|
else if (hue < 300) { r = x; b = c; }
|
||||||
|
else { r = c; b = x; }
|
||||||
|
|
||||||
|
return Color.FromArgb(
|
||||||
|
(int)((r + m) * 255),
|
||||||
|
(int)((g + m) * 255),
|
||||||
|
(int)((b + m) * 255)
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user