-
Notifications
You must be signed in to change notification settings - Fork 7
/
BlocksGame.h
299 lines (270 loc) · 8.7 KB
/
BlocksGame.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
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
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////
/////////
///////// B L O C K S G A M E
/////////
/////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class CBlocksGame : public CGame
{
int count;
public:
static void getGameIcon(byte *dst, byte count)
{
dst[ 0] = 0b11110000;
dst[ 1] = 0b11110110;
dst[ 2] = 0b11000110;
dst[ 3] = 0b11000110;
dst[ 4] = 0b00000110;
dst[ 5] = 0b00000110;
dst[ 6] = 0b00000110;
dst[ 7] = 0b00000000;
dst[ 8] = 0b11110000;
dst[ 9] = 0b11110000;
dst[10] = 0b11000000;
dst[11] = 0b11011000;
dst[12] = 0b00011000;
dst[13] = 0b01111000;
dst[14] = 0b01111000;
dst[15] = 0b00000000;
}
enum {
PIECE_I,
PIECE_O,
PIECE_S,
PIECE_Z,
PIECE_L,
PIECE_J,
PIECE_T,
PIECE_MAX
};
enum {
DIR_0,
DIR_90,
DIR_180,
DIR_270
};
void mapPiece(byte piece, byte dir, byte *result)
{
switch(piece)
{
// I PIECE
case PIECE_I:
switch(dir) {
case DIR_0: case DIR_180: result[1] = 0b11110000; break;
case DIR_90: case DIR_270: result[0] = result[1] = result[2] = result[3] = 0b01000000; break;
} break;
// O PIECE
case PIECE_O:
result[0] = result[1] = 0b11000000; break;
break;
// S PIECE
case PIECE_S:
switch(dir) {
case DIR_0: result[0] = 0b01100000; result[1] = 0b11000000; break;
case DIR_90: result[0] = 0b01000000; result[1] = 0b01100000; result[2] = 0b00100000; break;
case DIR_180: result[1] = 0b01100000; result[2] = 0b11000000; break;
case DIR_270: result[0] = 0b10000000; result[1] = 0b11000000; result[2] = 0b01000000; break;
} break;
// Z PIECE
case PIECE_Z:
switch(dir) {
case DIR_0: result[0] = 0b11000000; result[1] = 0b01100000; break;
case DIR_90: result[0] = 0b00100000; result[1] = 0b01100000; result[2] = 0b01000000; break;
case DIR_180: result[1] = 0b11000000; result[2] = 0b01100000; break;
case DIR_270: result[0] = 0b01000000; result[1] = 0b11000000; result[2] = 0b10000000; break;
} break;
// L PIECE
case PIECE_L:
switch(dir) {
case DIR_0: result[0] = 0b00100000; result[1] = 0b11100000; break;
case DIR_90: result[0] = result[1] = 0b01000000; result[2] = 0b01100000; break;
case DIR_180: result[1] = 0b11100000; result[2] = 0b10000000; break;
case DIR_270: result[0] = 0b11000000; result[1] = result[2] = 0b01000000; break;
} break;
// J PIECE
case PIECE_J:
switch(dir) {
case DIR_0: result[0] = 0b10000000; result[1] = 0b11100000; break;
case DIR_90: result[0] = 0b01100000; result[1] = result[2] = 0b01000000; break;
case DIR_180: result[1] = 0b11100000; result[2] = 0b00100000; break;
case DIR_270: result[0] = result[1] = 0b01000000; result[2] = 0b11000000; break;
} break;
// T PIECE
case PIECE_T:
switch(dir) {
case DIR_0: result[0] = 0b10000000; result[1] = 0b11000000; result[2] = 0b10000000; break;
case DIR_90: result[0] = 0b11100000; result[1] = 0b01000000; break;
case DIR_180: result[0] = 0b00100000; result[1] = 0b01100000; result[2] = 0b00100000; break;
case DIR_270: result[1] = 0b01000000; result[2] = 0b11100000; break;
} break;
}
}
byte placePiece;
byte placeDir;
byte placeCol;
byte placeRow;
byte gridMap[8];
byte shadowMap[8];
byte flashShadow;
void init()
{
flashShadow = 0;
placeCol = 1;
placePiece = PIECE_T;
placeDir = DIR_0;
memset(gridMap,0,sizeof gridMap);
memset(shadowMap,0,sizeof shadowMap);
Disp8x8.cls();
newPiece();
}
void newPiece()
{
placePiece = random(PIECE_MAX);
placeCol = random(6);
placeDir = random(4);
movePiece(0,0);
Timer1Period = 150; // flash shadow map
}
byte movePiece(char dr, char dx)
{
byte pm[4];
int row, i;
char newDir = placeDir + dr;
if(newDir < DIR_0)
newDir = DIR_270;
else if(newDir > DIR_270)
newDir = DIR_0;
char newCol = constrain(placeCol + dx,0,7);
for(byte attempt = 0; attempt < 4; ++attempt)
{
memset(pm, 0, 4);
mapPiece(placePiece, newDir, pm);
pm[0] >>= newCol;
pm[1] >>= newCol;
pm[2] >>= newCol;
pm[3] >>= newCol;
// outside play area on left
if((pm[0] & 0x80)||(pm[1] & 0x80)||(pm[2] & 0x80)||(pm[3] & 0x80))
{
newCol++; // otherwise must move to right
continue;
}
// outside play are to right?
else if((pm[0] & 0x01)||(pm[1] & 0x01)||(pm[2] & 0x01)||(pm[3] & 0x01))
{
newCol--; // otherwise must move to left
continue;
}
else
{
break;
}
}
placeDir = newDir;
placeCol = newCol;
// scan down looking for first collision
// betweem the piecemap and the gridmap
for(row=-3;row<8;++row)
{
// piecemap is 4 rows high
for(i=0;i<4;++i)
{
// calc gridmap row corresponding
// to the piecemap row and ensure
// it is valid on grid
int r = row + i;
if(r >= 0 && r < 8)
{
if(pm[i] && (r == 7)) // resting on bottom row
goto JUST_GET_OUT_OF_ALL_THESE_LOOPS_ALREADY;
if(r<7 && !!(pm[i] & gridMap[r+1])) // bitwise collision
goto JUST_GET_OUT_OF_ALL_THESE_LOOPS_ALREADY;
}
}
}
JUST_GET_OUT_OF_ALL_THESE_LOOPS_ALREADY: //imho a reasonable use of a goto :)
// now store the offset data as shadow map
placeRow = row;
memset(shadowMap,0,sizeof shadowMap);
for(i=0;i<4;++i)
{
int r = row + i;
if(r >= 0 && r < 8)
shadowMap[r] = pm[i];
}
}
void lockPieceInPlace()
{
int i,j;
for(int i=0;i<8;++i)
gridMap[i] |= shadowMap[i];
playSound(500,100);
Disp8x8.delayWithRefresh(50);
gameScore += 10;
// look for full rows we can remove
byte count = 0;
int bonus = 100;
byte again = 1;
while(again)
{
again=0;
for(i=8;i>0;--i)
{
if(gridMap[i] == 0x7e)
{
count++;
Disp8x8.red[i] = 0xff;
Disp8x8.green[i] = 0x7e;
playSound(500+200*count,100);
gameScore += bonus;
bonus *= 2;
Disp8x8.delayWithRefresh(100);
Disp8x8.red[i] = 0x81;
for(j=i;j>0;--j)
gridMap[j] = gridMap[j-1];
gridMap[0] = 0;
memcpy(Disp8x8.green, gridMap,8);
again = 1;
break;
}
}
}
// put another piece into play
newPiece();
}
void handleEvent(char event)
{
int i;
switch(event)
{
case EV_PRESS_B://left
movePiece(0,-1);
break;
case EV_PRESS_D://right
movePiece(0,+1);
break;
case EV_PRESS_A:
movePiece(1,0);//rotate
break;
case EV_PRESS_C:
lockPieceInPlace();
break;
case EV_TIMER_1: //FLASH SHADOW MAP
flashShadow = !flashShadow;
break;
}
for(int i=0;i<8;++i)
{
Disp8x8.green[i] = gridMap[i];
Disp8x8.red[i] = 0x81|shadowMap[i];
if(flashShadow)
Disp8x8.green[i] |= shadowMap[i];
}
}
};
CGameFactoryImpl<CBlocksGame> BlocksGame;