-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Snake.cs
91 lines (80 loc) · 2.16 KB
/
Snake.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Snake
{
class Snake : IMovable, IDrawable
{
private enum Direction { Stop = 0, Left, Right, Up, Down }
private Direction Dir = Direction.Stop;
private GameField _gameField;
public Point Head { get; set; }
private List<Point> _tail = new List<Point>();
public List<Point> Tail
{
get { return _tail; }
}
public Snake(int aStartX, int aStartY)
{
Head = new Point(aStartX, aStartY);
}
public void SetField(GameField aGameField)
{
_gameField = aGameField;
}
public void Eat()
{
_tail.Insert(0,new Point(Head.X,Head.Y));
}
public void DirectionLeft()
{
Dir = Direction.Left;
}
public void DirectionRight()
{
Dir = Direction.Right;
}
public void DirectionUp()
{
Dir = Direction.Up;
}
public void DirectionDown()
{
Dir = Direction.Down;
}
public void Move()
{
_tail.Insert(0, new Point(Head.X, Head.Y));
_tail.RemoveAt(_tail.Count - 1);
switch (Dir)
{
case Direction.Left:
Head.X--;
break;
case Direction.Right:
Head.X++;
break;
case Direction.Up:
Head.Y--;
break;
case Direction.Down:
Head.Y++;
break;
default:
break;
}
}
public void Draw()
{
_gameField.Field[Head.Y][Head.X].Val = "O";
_gameField.Field[Head.Y][Head.X].Color = ConsoleColor.Green;
foreach (Point t in Tail)
{
_gameField.Field[t.Y][t.X].Val = "o";
_gameField.Field[t.Y][t.X].Color = ConsoleColor.Green;
}
}
}
}