-
Notifications
You must be signed in to change notification settings - Fork 0
/
Snake.cs
76 lines (71 loc) · 1.96 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Snake
{
class Snake : Figure
{
Direction direction;
public Snake(Point tail, int length, Direction _direction)
{
direction = _direction;
pList = new List<Point>();
for (int i = 0; i < length; i++)
{
Point p = new Point(tail);
p.Move(i, direction);
pList.Add(p);
}
}
internal void Move()
{
Point tail = pList.First();
pList.Remove(tail);
Point head = GetNextPoint();
pList.Add(head);
tail.Clear();
head.Draw();
}
internal bool IsHitTail()
{
Point head = pList.Last();
for (int i = 0; i < pList.Count - 2; i++)
{
if (head.IsHit(pList[i]))
return true;
}
return false;
}
public Point GetNextPoint()
{
Point head = pList.Last();
Point nextPoint = new Point(head);
nextPoint.Move(1, direction);
return nextPoint;
}
public void HandleKey(ConsoleKey key)
{
if (key == ConsoleKey.LeftArrow)
direction = Direction.LEFT;
else if (key == ConsoleKey.RightArrow)
direction = Direction.RIGHT;
else if (key == ConsoleKey.UpArrow)
direction = Direction.UP;
else if (key == ConsoleKey.DownArrow)
direction = Direction.DOWN;
}
internal bool Eat(Point food)
{
Point head = GetNextPoint();
if (head.IsHit(food))
{
food.sym = head.sym;
pList.Add(food);
return true;
}
return false;
}
}
}