-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnakeElement.cpp
More file actions
86 lines (77 loc) · 1.58 KB
/
SnakeElement.cpp
File metadata and controls
86 lines (77 loc) · 1.58 KB
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
#include "SnakeElement.h"
int SnakeElement::TailCount = 0;
SnakeElement::SnakeElement(): Head() {
CurrentDirection = stop;
mTail = NULL;
}
SnakeElement::SnakeElement(int x, int y, int r): Head(x, y, r) {
CurrentDirection = stop;
mTail = NULL;
}
SnakeElement::~SnakeElement() {
if (NULL != mTail) {
delete mTail;
}
}
bool SnakeElement::update(SnakeDirection newDir) {
switch (newDir) {
case left:
Head.goLeft();
break;
case right:
Head.goRight();
break;
case up:
Head.goUp();
break;
case down:
Head.goDown();
break;
case stop:
break;
}
if (NULL != mTail) {
mTail->update(CurrentDirection);
}
CurrentDirection = newDir;
return true;
}
void SnakeElement::draw() {
Head.draw();
//Serial.println("display");
if (NULL != mTail) {
mTail->draw();
}
}
bool SnakeElement::elongate() {
if (TailCount >= 64) {
return false;
}
if (NULL == mTail) { // New tail direct with head
SnakeElement *newTail;
int x, y, r;
Head.getXYR(x, y, r);
switch (CurrentDirection) {
case left:
newTail = new SnakeElement(x + 2 * r, y, r);
break;
case right:
newTail = new SnakeElement(x - 2 * r, y, r);
break;
case up:
newTail = new SnakeElement(x, y + 2 * r, r);
break;
case down:
newTail = new SnakeElement(x, y - 2 * r, r);
break;
}
mTail = newTail;
TailCount++;
return true;
} else {
mTail->elongate();
}
}
bool SnakeElement::eating(Circle &Food) {
return Head.intersects(Food);
}