-
Notifications
You must be signed in to change notification settings - Fork 1
/
pacman.ino
87 lines (72 loc) · 1.9 KB
/
pacman.ino
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
void pacman(int nSteps) {
while (nSteps--) {
int start = random(ledsPerStrip);
int offset = eating(start);
victory(start + offset);
}
}
void victory(int offset) {
int flashes = 3;
for (int step = 0; step < (flashes * 2) + 1; step++) {
colorWipe(BLACK);
drawWall(step % 2 == 0 ? BLUE : WHITE);
drawPacman(offset, true);
leds.show();
delayMicroseconds(800000);
}
delayMicroseconds(1500000);
}
int eating(int start) {
int offset = 0;
bool mouthOpen = true;
int dotCount = 30;
int dotColor = PINK;
bool stillEating = true;
while (stillEating) {
colorWipe(BLACK);
drawWall(BLUE);
// DOTS
for (int i = 0; i < dotCount; i++) {
int mid = start + i * (ledsPerStrip / dotCount);
if (mid > start + offset) {
wrappedPixel(0, mid, dotColor);
wrappedPixel(0, mid + 1, dotColor);
}
}
// PACMAN
drawPacman(start + offset, mouthOpen);
if (offset + (ledsPerStrip / dotCount) >= ledsPerStrip) {
stillEating = false;
}
offset = wrap(offset + 1, ledsPerStrip);
mouthOpen = !mouthOpen;
leds.show();
delayMicroseconds(100000);
}
return offset;
}
void wrappedPixel(int row, int col, int color) {
pixel(wrap(row, nStrips), wrap(col, ledsPerStrip), color);
}
void drawWall(int color) {
for (int i = 0; i < ledsPerStrip; i++) {
pixel(5, i, color);
}
}
void drawPacman(int x, bool mouthOpen) {
wrappedPixel(0, x - 2, YELLOW);
wrappedPixel(1, x - 2, YELLOW);
wrappedPixel(0, x - 1, YELLOW);
wrappedPixel(1, x - 1, YELLOW);
wrappedPixel(2, x - 1, YELLOW);
wrappedPixel(1, x, YELLOW);
wrappedPixel(2, x, YELLOW);
wrappedPixel(2, x + 1, YELLOW);
if (!mouthOpen) {
wrappedPixel(0, x, YELLOW);
wrappedPixel(0, x + 1, YELLOW);
wrappedPixel(1, x + 1, YELLOW);
wrappedPixel(0, x + 2, YELLOW);
wrappedPixel(1, x + 2, YELLOW);
}
}