-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTetris.cpp
210 lines (191 loc) · 6.33 KB
/
Tetris.cpp
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include "00Names.hpp"
// TODO: What is after this line
namespace Tetris {
const int FIELD_WIDTH = 10, FIELD_HEIGHT = 20, BLOCK_SIZE = 6;
const vec2i FIELD_OFFSET = vec2i(64 - FIELD_WIDTH * BLOCK_SIZE / 2, 64 - FIELD_HEIGHT * BLOCK_SIZE / 2);
const vec2f TETROMINOES[][4] = {
{ vec2f(-1.5, -0.5), vec2f(-0.5, -0.5), vec2f(0.5, -0.5), vec2f(1.5, -0.5) },
{ vec2f(-1, -1), vec2f(-1, 0), vec2f(0, 0), vec2f(1, 0) },
{ vec2f(-1, 0), vec2f(0, 0), vec2f(1, 0), vec2f(1, -1) },
{ vec2f(-0.5, -0.5), vec2f(0.5, -0.5), vec2f(-0.5, 0.5), vec2f(0.5, 0.5) },
{ vec2f(-1, 0), vec2f(0, 0), vec2f(0, -1), vec2f(1, -1) },
{ vec2f(-1, 0), vec2f(0, 0), vec2f(1, 0), vec2f(0, -1) },
{ vec2f(-1, -1), vec2f(0, -1), vec2f(0, 0), vec2f(1, 0) },
};
uint16_t field[FIELD_WIDTH * FIELD_HEIGHT];
uint8_t clearLines[4];
uint32_t gameOverAnimationTimer, clearAnimationTimer;
uint16_t score, timeout;
void start();
bool line(uint8_t y);
struct Tetromino {
uint8_t type = 0;
uint8_t rotation = 0;
vec2i pos = 0;
uint16_t color;
Tetromino() = default;
Tetromino(vec2i pos, uint8_t type, uint16_t color)
: type(type), pos(pos), color(color) {}
void randomize() {
const uint16_t colors[] = { 0x0215, 0x7647, 0xFEA0, 0xFCA3, 0xF982 };
type = random(7);
rotation = 0;
pos = vec2i(FIELD_WIDTH / 2 - 1, 0);
for (const auto& tile : TETROMINOES[type]) pos.y = max(-rotate(tile).y, pos.y);
color = colors[random(sizeof(colors) / sizeof(colors[0]))];
if (!fit()) gameOverAnimationTimer = millis();
}
vec2i rotate(vec2f tile) {
vec2f offset = type == 0 || type == 3 ? 0.5 : 0;
if (rotation == 1) return vec2f(-tile.y, tile.x) + offset;
else if (rotation == 2) return -tile + offset;
else if (rotation == 3) return vec2f(tile.y, -tile.x) + offset;
return tile + offset;
}
void drawRaw() {
for (const auto& tile : TETROMINOES[type]) {
oled::fillRect((pos + rotate(tile)) * BLOCK_SIZE + FIELD_OFFSET, BLOCK_SIZE - 1, color);
}
}
void draw() {
drawRaw();
vec2i lastPos = pos;
while (fit()) pos.y++;
pos.y--;
for (const auto& tile : TETROMINOES[type]) {
oled::drawRect((pos + rotate(tile)) * BLOCK_SIZE + FIELD_OFFSET, BLOCK_SIZE - 1, color);
}
pos = lastPos;
}
bool fit() {
for (const auto& tile : TETROMINOES[type]) {
vec2i point = pos + rotate(tile);
if (point.x < 0 || point.x >= FIELD_WIDTH || point.y < 0 || point.y >= FIELD_HEIGHT) return false;
if (field[point.x + point.y * FIELD_WIDTH]) return false;
}
return true;
}
void place() {
for (const auto& tile : TETROMINOES[type]) {
vec2i point = rotate(tile) + pos;
field[point.x + point.y * FIELD_WIDTH] = color;
}
int lineN = 0;
for (int y = FIELD_HEIGHT - 1; y >= 0; y--) {
if (line(y)) {
clearAnimationTimer = millis();
clearLines[lineN++] = y;
}
}
while (lineN < 4) clearLines[lineN++] = 255;
}
bool moveRaw(vec2i vel) {
pos += vel;
if (!fit()) {
pos -= vel;
return true;
}
return false;
}
bool move(vec2i vel) {
if (moveRaw(vel)) {
if (vel.y != 0) {
place();
randomize();
}
return true;
}
return false;
}
void rotate() {
rotation = (rotation + 1) % 4;
if (!fit()) rotation = rotation == 0 ? 3 : rotation - 1;
}
};
Tetromino tetromino;
uint32_t fallTimer, moveTimer;
bool line(uint8_t y) {
for (int x = 0; x < FIELD_WIDTH; x++) {
if (!field[x + y * FIELD_WIDTH]) return false;
}
return true;
}
void checkClearLines() {
static bool clear = false;
if (clearAnimationTimer) {
for (auto line : clearLines) {
if (line == 255) break;
for (int x = 0; x < FIELD_WIDTH / 2; x++) {
int dist = FIELD_WIDTH / 2 - (millis() - clearAnimationTimer) * FIELD_WIDTH / 2 / 300;
field[x + line * FIELD_WIDTH] = field[FIELD_WIDTH - x - 1 + line * FIELD_WIDTH] = x < dist ? WHITE : BLACK;
}
}
if (millis() - clearAnimationTimer > 300) clearAnimationTimer = 0;
clear = true;
return;
}
if (!clear) return;
clear = false;
int row = 0, lineN = 0;
for (int y = FIELD_HEIGHT - 1; y >= 0; y--) {
if (clearLines[lineN] == y) row++, lineN++;
else if (row != 0 && y < FIELD_HEIGHT - 1) {
for (int x = 0; x < FIELD_WIDTH; x++) field[x + (y + row) * FIELD_WIDTH] = field[x + y * FIELD_WIDTH];
}
}
if (row) score += 1 << row;
timeout = max(timeout - 50, 100);
}
void drawField(bool border = true) {
if (border) oled::drawRect(FIELD_OFFSET - 2, vec2i(FIELD_WIDTH, FIELD_HEIGHT) * BLOCK_SIZE + 2, BLACK);
for (int x = 0; x < FIELD_WIDTH; x++) {
for (int y = 0; y < FIELD_HEIGHT; y++) {
float k = gameOverAnimationTimer ? min((millis() - gameOverAnimationTimer) / 500.f, 1) * 0.5f : 0;
if (field[x + y * FIELD_WIDTH]) oled::fillRect(vec2i(x, y) * BLOCK_SIZE + FIELD_OFFSET, BLOCK_SIZE - 1, oled::interpolateColor(field[x + y * FIELD_WIDTH], BLACK, k));
if (border) oled::drawRect(vec2i(x, y) * BLOCK_SIZE + FIELD_OFFSET - 1, BLOCK_SIZE, BLACK);
}
}
}
void update() {
if (!gameOverAnimationTimer && !clearAnimationTimer) {
if (millis() - fallTimer > (buttonY.bHeld ? 100 : timeout)) {
fallTimer = millis();
tetromino.move(vec2i(0, 1));
}
if (millis() - moveTimer > 500 || bJoyMoved) {
moveTimer = millis();
tetromino.move(vec2i(joy.x, 0));
if (joy.y > 0) {
while (!tetromino.move(vec2i(0, 1))) continue;
}
}
if (buttonX.bPressed) tetromino.rotate();
}
if (!gameOverAnimationTimer) checkClearLines();
}
void draw() {
oled::fillScreen(0x3124);
drawField();
if (!gameOverAnimationTimer) {
if (!clearAnimationTimer)
tetromino.draw();
gui::textAt(format("%03d", score), FIELD_OFFSET + vec2i(FIELD_WIDTH * BLOCK_SIZE + 5, 0));
} else {
gui::centerText(gui::typeAsync("Game Over!", gameOverAnimationTimer), 10);
if (millis() - gameOverAnimationTimer > 1000) {
gui::centerText("You score is", 10 + oled::getCharHeight());
gui::centerText(format("0x%x", score), 10 + oled::getCharHeight() * 2);
gui::centerText("X to restart!", 10 + oled::getCharHeight() * 3);
}
if (buttonX.bReleased) start();
}
}
void start() {
for (int i = 0; i < FIELD_WIDTH * FIELD_HEIGHT; i++) field[i] = 0;
tetromino.randomize();
gameOverAnimationTimer = 0;
score = 0;
timeout = 500;
game = Game(update, draw);
}
}