-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathpicotetris.cpp
More file actions
474 lines (401 loc) · 9.73 KB
/
picotetris.cpp
File metadata and controls
474 lines (401 loc) · 9.73 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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
// Raspberry Pi Pico C port by Richard Birkby
// Based on the Arduino Microview port by Richard Birkby - https://github.com/rbirkby/ArduinoTetris
// Original JavaScript implementation - Jake Gordon - https://github.com/jakesgordon/javascript-tetris
// MIT licenced
#include <stdio.h>
#include <math.h>
#include "pico/stdlib.h"
#include "pico/multicore.h"
#include "hardware/gpio.h"
#include "pico_explorer.hpp"
#include "picotetris.hpp"
#include "song.cpp"
#include "autorepeat.cpp"
using namespace pimoroni;
uint16_t buffer[PicoExplorer::WIDTH * PicoExplorer::HEIGHT];
PicoExplorer pico_explorer(buffer);
//-------------------------------------------------------------------------
// game constants
//-------------------------------------------------------------------------
enum DIR
{
UP,
RIGHT,
DOWN,
LEFT,
MIN = UP,
MAX = LEFT
};
// 6 blocks reserved for info area
const int nx = 9; // width of tetris court (in blocks)
const int ny = 15; // height of tetris court (in blocks)
AutoRepeat autorepeatB, autorepeatY, autorepeatX;
//-------------------------------------------------------------------------
// game variables (initialized during reset)
//-------------------------------------------------------------------------
// Reserve 6 blocks width for the info area
const int dx = (PicoExplorer::WIDTH / (nx + 6)); // pixel size of a single tetris block
const int dy = PicoExplorer::HEIGHT / ny;
int blocks[nx][ny]; // 2 dimensional array (nx*ny) representing tetris court - either empty block or occupied by a 'piece'
bool playing = true; // true|false - game is in progress
bool lost = false;
Piece current, next; // the current and next piece
unsigned int score = 0; // the current score
int dir = 0;
//-------------------------------------------------------------------------
// tetris pieces
//
// blocks: each element represents a rotation of the piece (0, 90, 180, 270)
// each element is a 16 bit integer where the 16 bits represent
// a 4x4 set of blocks, e.g. j.blocks[0] = 0x44C0
//
// 0100 = 0x4 << 3 = 0x4000
// 0100 = 0x4 << 2 = 0x0400
// 1100 = 0xC << 1 = 0x00C0
// 0000 = 0x0 << 0 = 0x0000
// ------
// 0x44C0
//
//-------------------------------------------------------------------------
unsigned int i[] = {0x0F00, 0x2222, 0x00F0, 0x4444};
unsigned int j[] = {0x44C0, 0x8E00, 0x6440, 0x0E20};
unsigned int l[] = {0x4460, 0x0E80, 0xC440, 0x2E00};
unsigned int o[] = {0xCC00, 0xCC00, 0xCC00, 0xCC00};
unsigned int s[] = {0x06C0, 0x8C40, 0x6C00, 0x4620};
unsigned int t[] = {0x0E40, 0x4C40, 0x4E00, 0x4640};
unsigned int z[] = {0x0C60, 0x4C80, 0xC600, 0x2640};
int main()
{
stdio_init_all();
setup();
while (true)
{
loop();
}
return 0;
}
//------------------------------------------------
// do the bit manipulation and iterate through each
// occupied block (x,y) for a given piece
//------------------------------------------------
void getPositionedBlocks(unsigned int type[], int x, int y, int dir, Block *positionedBlocks)
{
unsigned int bit;
int blockIndex = 0, row = 0, col = 0, blocks = type[dir];
for (bit = 0x8000; bit > 0; bit = bit >> 1)
{
if (blocks & bit)
{
Block block = {x + col, y + row};
positionedBlocks[blockIndex++] = block;
}
if (++col == 4)
{
col = 0;
++row;
}
}
}
//-----------------------------------------------------
// check if a piece can fit into a position in the grid
//-----------------------------------------------------
bool occupied(unsigned int type[], int x, int y, int dir)
{
bool result = false;
Block positionedBlocks[4];
getPositionedBlocks(type, x, y, dir, positionedBlocks);
for (int i = 0; i < 4; i++)
{
Block block = positionedBlocks[i];
if ((block.x < 0) || (block.x >= nx) || (block.y < 0) || (block.y >= ny) || getBlock(block.x, block.y))
result = true;
}
return result;
}
bool unoccupied(unsigned int type[], int x, int y, int dir)
{
return !occupied(type, x, y, dir);
}
//-----------------------------------------
//-----------------------------------------
unsigned int *pieces[] = {i, j, l, o, s, t, z};
Pen pens[] = {
pico_explorer.create_pen(255, 0, 0),
pico_explorer.create_pen(0, 255, 0),
pico_explorer.create_pen(0, 0, 255),
pico_explorer.create_pen(255, 255, 0),
pico_explorer.create_pen(0, 255, 255),
pico_explorer.create_pen(255, 0, 255),
pico_explorer.create_pen(255, 255, 255)
};
Piece randomPiece()
{
int index = rand() % 6;
printf("New piece: %d\n", index);
Piece current = {pieces[index], 3, 0, UP, pens[index]};
return current;
}
//-------------------------------------------------------------------------
// GAME LOOP
//-------------------------------------------------------------------------
void core1_entry(void);
void setup()
{
pico_explorer.init();
pico_explorer.set_audio_pin(0);
pico_explorer.set_backlight(100);
pico_explorer.set_pen(0, 0, 0);
multicore_launch_core1(core1_entry);
pico_explorer.clear();
pico_explorer.update();
reset();
}
void loop()
{
int time = to_ms_since_boot(get_absolute_time());
if (autorepeatB.next(time, pico_explorer.is_pressed(pico_explorer.B)))
{
onLeftButton();
}
if (autorepeatY.next(time, pico_explorer.is_pressed(pico_explorer.Y)))
{
onRightButton();
}
if (autorepeatX.next(time, pico_explorer.is_pressed(pico_explorer.X)))
{
onRotateButton();
}
if (time % 60 == 0)
{
draw();
if (playing && (time % 300 == 0))
{
drop();
if (lost)
{
draw();
sleep_ms(1000);
}
}
}
}
void onLeftButton()
{
if (lost)
{
reset();
}
move(LEFT);
}
void onRightButton()
{
if (lost)
{
reset();
}
move(RIGHT);
}
void onRotateButton()
{
if (lost)
{
reset();
}
rotate();
}
//-------------------------------------------------------------------------
// GAME LOGIC
//-------------------------------------------------------------------------
void lose()
{
playing = false;
lost = true;
}
void addScore(int n) { score = score + n; }
void clearScore() { score = 0; }
int getBlock(int x, int y) { return (blocks[x] ? blocks[x][y] : NULL); }
void setBlock(int x, int y, int type) { blocks[x][y] = type; }
void clearBlocks()
{
for (int x = 0; x < nx; x++)
{
for (int y = 0; y < ny; y++)
{
blocks[x][y] = 0;
}
}
}
void setCurrentPiece(Piece piece) { current = piece; }
void setNextPiece(Piece piece) { next = piece; }
void reset()
{
lost = false;
playing = true;
clearBlocks();
clearScore();
setCurrentPiece(randomPiece());
setNextPiece(randomPiece());
}
bool move(int dir)
{
int x = current.x, y = current.y;
printf("Move %d", dir);
switch (dir)
{
case RIGHT:
x = x + 1;
break;
case LEFT:
x = x - 1;
break;
case DOWN:
y = y + 1;
break;
}
if (unoccupied(current.type, x, y, current.dir))
{
current.x = x;
current.y = y;
return true;
}
else
{
printf("Occupied. Cannot move piece\n");
return false;
}
}
void rotate()
{
int newdir = (current.dir == MAX ? MIN : current.dir + 1);
if (unoccupied(current.type, current.x, current.y, newdir))
{
current.dir = newdir;
}
}
void drop()
{
if (!move(DOWN))
{
addScore(10);
dropPiece();
removeLines();
setCurrentPiece(next);
setNextPiece(randomPiece());
if (occupied(current.type, current.x, current.y, current.dir))
{
lose();
}
}
}
void dropPiece()
{
Block positionedBlocks[4];
getPositionedBlocks(current.type, current.x, current.y, current.dir, positionedBlocks);
for (int i = 0; i < 4; i++)
{
setBlock(positionedBlocks[i].x, positionedBlocks[i].y, -1);
}
}
void removeLines()
{
int x, y, n = 0;
bool complete;
for (y = ny; y > 0; --y)
{
complete = true;
for (x = 0; x < nx; ++x)
{
if (!getBlock(x, y))
complete = false;
}
if (complete)
{
removeLine(y);
y = y + 1; // recheck same line
n++;
}
}
if (n > 0)
{
addScore(100 * pow(2, n - 1)); // 1: 100, 2: 200, 3: 400, 4: 800
}
}
void removeLine(int n)
{
int x, y;
for (y = n; y >= 0; --y)
{
for (x = 0; x < nx; ++x)
{
setBlock(x, y, (y == 0) ? NULL : getBlock(x, y - 1));
}
}
}
//-------------------------------------------------------------------------
// RENDERING
//-------------------------------------------------------------------------
void draw()
{
printf("Draw frame\n");
drawCourt();
drawNext();
drawScore();
removeLines();
pico_explorer.update();
}
void drawCourt()
{
pico_explorer.set_pen(0, 0, 0);
pico_explorer.clear();
pico_explorer.set_pen(255, 255, 255);
if (playing)
drawPiece(current.type, current.x + 6, current.y, current.dir, current.pen);
pico_explorer.set_pen(255, 255, 255);
int x, y;
for (y = 0; y < ny; y++)
{
for (x = 0; x < nx; x++)
{
if (getBlock(x, y))
drawBlock(x + 6, y);
}
}
if (lost)
{
pico_explorer.set_pen(255, 0, 0);
pico_explorer.text(" Game Over ", Point(0, 20), 240, 8);
printf("Game Over\n");
}
}
void drawNext()
{
if (playing)
drawPiece(next.type, 1, 3, next.dir, next.pen);
}
void drawScore()
{
pico_explorer.set_pen(255, 255, 255);
pico_explorer.text(std::to_string(score), Point(dx/2, dy/2), 240, 3);
}
void drawPiece(unsigned int type[], int x, int y, int dir, Pen pen)
{
Block positionedBlocks[4];
getPositionedBlocks(type, x, y, dir, positionedBlocks);
pico_explorer.set_pen(pen);
for (int i = 0; i < 4; i++)
{
drawBlock(positionedBlocks[i].x, positionedBlocks[i].y);
}
}
void drawBlock(int x, int y)
{
Rect block(x * dx, y * dy, dx, dy);
pico_explorer.rectangle(block);
}
void core1_entry()
{
while (true)
{
play_song(pico_explorer);
}
}