-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnake.cs
99 lines (86 loc) · 2.13 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
92
93
94
95
96
97
98
99
namespace SnakeWinForms;
internal class Snake
{
private int _score = 0;
public int _snakeCellWidth;
public int Score { get => _score; }
private List<SnakeBodyPart> _bodyParts = new List<SnakeBodyPart>();
public List<SnakeBodyPart> BodyParts { get => _bodyParts; }
private SnakeBodyPart Head { get => _bodyParts[0]; }
public Point SnakeHeadPositionScaled { get => new Point(Head.x * _snakeCellWidth, Head.y * _snakeCellWidth); }
private SnakeBodyPart Tail { get => _bodyParts[_bodyParts.Count - 1]; }
private List<Point> fruitsList;
/// <summary>
/// Anchor point is the upper left corner
/// </summary>
public Snake(int startBodyParts, int startOffsetX, int startOffsetY, int snakeCellWidth, List<Point> fruitsList)
{
_snakeCellWidth = snakeCellWidth;
this.fruitsList = fruitsList;
for (int i = 0; i < startBodyParts; i++)
{
_bodyParts.Add(new SnakeBodyPart(i + startOffsetX, startOffsetY));
}
}
public void IncreaseScore() => _score++;
public void AddBodySegment()
{
_bodyParts.Add(new SnakeBodyPart(Tail.x, Tail.y));
}
private void Move(int xOffset, int yOffset)
{
Head.isHead = false;
int xHead = Head.x;
int yHead = Head.y;
_bodyParts.Insert(0, Tail);
_bodyParts.RemoveAt(_bodyParts.Count - 1);
Head.x = xHead - xOffset;
Head.y = yHead - yOffset;
Head.isHead = true;
}
public bool DetectFruitCollision(out Point? collidedFruit)
{
collidedFruit = null;
foreach (Point fruit in fruitsList)
{
if(fruit.X == Head.x && fruit.Y == Head.y)
{
collidedFruit = fruit;
return true;
}
}
return false;
}
public bool DidSnakeLeaveGameArea(Rectangle rect)
{
return !(Head.x < rect.Width &&
Head.y < rect.Height &&
Head.x > -1 &&
Head.y > -1);
}
public bool DetectSelfCollision()
{
return _bodyParts.GetRange(1, _bodyParts.Count - 1)/*get all body parts except for head*/
.Any(part => part.x == Head.x && part.y == Head.y);
}
public void MoveUp()
{
Move(0, 1);
}
public void MoveDown()
{
Move(0, -1);
}
public void MoveLeft()
{
Move(1, 0);
}
public void MoveRight()
{
Move(-1, 0);
}
public override string ToString()
{
return Head.ToString();
}
}