-
Notifications
You must be signed in to change notification settings - Fork 0
/
Snake.cs
80 lines (73 loc) · 1.82 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Threading.Tasks;
namespace SnakeGame
{
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);
}
}
public void Move()
{
Point tail = pList.First();
pList.Remove(tail);
Point head = GetNextPoint();
pList.Add(head);
tail.Clear();
head.Draw();
}
public Point GetNextPoint()
{
Point head = pList.Last();
Point nextPoint = new Point(head);
nextPoint.Move(1, direction);
return nextPoint;
}
public bool IsHitTail()
{
var head = pList.Last();
for (int i = 0; i < pList.Count - 2; i++)
{
if (head.IsHit(pList[i]))
return true;
}
return false;
}
public void HandleKey(ConsoleKey key)
{
if (key == ConsoleKey.LeftArrow && direction != Direction.RIGHT && direction != Direction.LEFT)
direction = Direction.LEFT;
else if (key == ConsoleKey.RightArrow && direction != Direction.LEFT && direction != Direction.RIGHT)
direction = Direction.RIGHT;
else if (key == ConsoleKey.DownArrow && direction != Direction.UP && direction != Direction.DOWN)
direction = Direction.DOWN;
else if (key == ConsoleKey.UpArrow && direction != Direction.DOWN && direction != Direction.UP)
direction = Direction.UP;
}
public bool Eat(Point food, Score score, Level level, Sounds sound, bool soundSwitch)
{
Point head = GetNextPoint();
if (head.IsHit(food))
{
food.sym = head.sym;
score.AddPoint(score, level, sound, soundSwitch);
pList.Add(food);
return true;
}
else { return false; }
}
}
}