-
Notifications
You must be signed in to change notification settings - Fork 4
/
tetris.h
77 lines (69 loc) · 1.79 KB
/
tetris.h
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
#define TETRIS_GAP 10
#define TETRIS_PIECES NUM_LEDS/TETRIS_GAP
#define TETRIS_LARGEST 4
typedef struct {
uint8_t len;
uint8_t col;
uint8_t pos;
} tetris_piece;
static tetris_piece (*tetris_pieces)[TETRIS_PIECES] = (tetris_piece(*)[TETRIS_PIECES]) &common_buffer;
static_assert(sizeof(*tetris_pieces) <= sizeof(common_buffer), "tetris_pieces must fit in common_buffer");
uint8_t tetris_div;
uint8_t tetris_gap;
void tetris_init(void) {
(*tetris_pieces)[0].col = random(RED,INDIGO+1);
(*tetris_pieces)[0].len = random(1,TETRIS_LARGEST+1);
(*tetris_pieces)[0].pos = NUM_LEDS;
for (uint8_t i = 1; i < TETRIS_PIECES; i++) {
(*tetris_pieces)[i].pos = 0;
}
tetris_div = 0;
tetris_gap = 0;
return;
}
void tetris_frame(void) {
//only run twice a second
if (tetris_div++ < 12) {
return;
}
tetris_div = 0;
//move pieces down
for (uint8_t i = 0; i < TETRIS_PIECES; i++) {
if ((*tetris_pieces)[i].pos == 0) {
continue;
}
(*tetris_pieces)[i].pos--;
}
//spawn a piece if we can
if (tetris_gap >= TETRIS_GAP) {
for (uint8_t i = 0; i < TETRIS_PIECES; i++) {
if ((*tetris_pieces)[i].pos > 0) {
continue;
}
(*tetris_pieces)[i].col = random(RED,INDIGO+1);
(*tetris_pieces)[i].len = random(1,TETRIS_LARGEST+1);
(*tetris_pieces)[i].pos = NUM_LEDS;
tetris_gap = 0;
break;
}
} else {
tetris_gap++;
}
return;
}
rgb24 tetris_getled(uint8_t led) {
led += 1;
for (uint8_t i = 0; i < TETRIS_PIECES; i++) {
if ((*tetris_pieces)[i].pos == 0) {
continue;
}
if (led < (*tetris_pieces)[i].pos) {
continue;
}
if (led > (*tetris_pieces)[i].pos + (*tetris_pieces)[i].len - 1) {
continue;
}
return colour((*tetris_pieces)[i].col, 99);
}
return (rgb24) {0x00,0x00,0x00};
}