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
|
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id$
*
* Copyright (C) 1999 Mattis Wadman (nappe@sudac.org)
*
* Heavily modified for embedded use by Björn Stenberg (bjorn@haxx.se)
*
* All files in this archive are subject to the GNU General Public License.
* See the file COPYING in the source tree root for full license agreement.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
#include "config.h"
#ifdef HAVE_LCD_BITMAP
#include <stdbool.h>
#include "lcd.h"
#include "button.h"
#include "kernel.h"
#include <string.h>
#ifdef SIMULATOR
#include <stdio.h>
#endif
#include "sprintf.h"
#define TETRIS_TITLE "Tetris!"
#define TETRIS_TITLE_FONT 1
#define TETRIS_TITLE_XLOC 43
#define TETRIS_TITLE_YLOC 15
static const int start_x = 1;
static const int start_y = 2;
static const int max_x = 104;
static const int max_y = 48;
static const short level_speeds[10] = {1000,900,800,700,600,500,400,300,250,200};
static const int blocks = 7;
static const int block_frames[7] = {1,2,2,2,4,4,4};
static int current_x, current_y, current_f, current_b;
static int level, score;
static int next_b, next_f;
static short lines;
static char virtual[LCD_WIDTH * LCD_HEIGHT];
/*
block_data is built up the following way
first array index specifies the block number
second array index specifies the rotation of the block
third array index specifies:
0: x-coordinates of pixels
1: y-coordinates of pixels
fourth array index specifies the coordinate of a pixel
each block consists of four pixels whose relative coordinates are given
with block_data
*/
static const char block_data[7][4][2][4] =
{
{
{{0,1,0,1},{0,0,1,1}}
},
{
{{0,1,1,2},{1,1,0,0}},
{{0,0,1,1},{0,1,1,2}}
},
{
{{0,1,1,2},{0,0,1,1}},
{{1,1,0,0},{0,1,1,2}}
},
{
{{1,1,1,1},{0,1,2,3}},
{{0,1,2,3},{2,2,2,2}}
},
{
{{1,1,1,2},{2,1,0,0}},
{{0,1,2,2},{1,1,1,2}},
{{0,1,1,1},{2,2,1,0}},
{{0,0,1,2},{0,1,1,1}}
},
{
{{0,1,1,1},{0,0,1,2}},
{{0,1,2,2},{1,1,1,0}},
{{1,1,1,2},{0,1,2,2}},
{{0,0,1,2},{2,1,1,1}}
},
{
{{1,0,1,2},{0,1,1,1}},
{{2,1,1,1},{1,0,1,2}},
{{1,0,1,2},{2,1,1,1}},
{{0,1,1,1},{1,0,1,2}}
}
};
int t_rand(int range)
{
return current_tick % range;
}
void draw_frame(int fstart_x,int fstop_x,int fstart_y,int fstop_y)
{
lcd_drawline(fstart_x, fstart_y, fstop_x, fstart_y);
lcd_drawline(fstart_x, fstop_y, fstop_x, fstop_y);
lcd_drawline(fstart_x, fstart_y, fstart_x, fstop_y);
lcd_drawline(fstop_x, fstart_y, fstop_x, fstop_y);
}
void draw_block(int x, int y, int block, int frame, bool clear)
{
int i, a, b;
for(i=0;i < 4;i++) {
if (clear)
{
for (a = 0; a < 4; a++)
for (b = 0; b < 4; b++)
lcd_clearpixel(start_x + x + block_data[block][frame][1][i] * 4 + b,
start_y + y + block_data[block][frame][0][i] * 4 + a);
}
else
{
for (a = 0; a < 4; a++)
for (b = 0; b < 4; b++)
lcd_drawpixel(start_x+x+block_data[block][frame][1][i] * 4 + b,
start_y+y+block_data[block][frame][0][i] * 4 + a);
}
}
}
void to_virtual(void)
{
int i,a,b;
for(i = 0; i < 4; i++)
{
for (a = 0; a < 4; a++)
for (b = 0; b < 4; b++)
*(virtual +
(current_y + block_data[current_b][current_f][0][i] * 4 + a) * max_x +
current_x + block_data[current_b][current_f][1][i] * 4 + b) = current_b + 1;
}
}
bool block_touch (int x, int y)
{
int a,b;
for (a = 0; a < 4; a++)
for (b = 0; b < 4; b++)
if (*(virtual + (y + b) * max_x + (x + a)) != 0)
return true;
return false;
}
bool gameover(void)
{
int i;
int frame, block, y, x;
x = current_x;
y = current_y;
block = current_b;
frame = current_f;
for(i = 0; i < 4; i++){
/* Do we have blocks touching? */
if(block_touch(x + block_data[block][frame][1][i] * 4, y + block_data[block][frame][0][i] * 4))
{
/* Are we at the top of the frame? */
if(y + block_data[block][frame][1][i] * 4 < start_y)
{
/* Game over ;) */
return true;
}
}
}
return false;
}
bool valid_position(int x, int y, int block, int frame)
{
int i;
for(i=0;i < 4;i++)
if ((y + block_data[block][frame][0][i] * 4 > max_y - 4) ||
(x + block_data[block][frame][1][i] * 4 > max_x) ||
(y + block_data[block][frame][0][i] * 4 < 0) ||
(x + block_data[block][frame][1][i] * 4 < 0) ||
block_touch (x + block_data[block][frame][1][i] * 4, y + block_data[block][frame][0][i] * 4))
return false;
return true;
}
void from_virtual(void)
{
int x,y;
for(y = 0; y < max_y; y++)
for(x = 1; x < max_x - 1; x++)
if(*(virtual + (y * max_x) + x) != 0)
lcd_drawpixel(start_x + x, start_y + y);
else
lcd_clearpixel(start_x + x, start_y + y);
}
void move_block(int x,int y,int f)
{
int last_frame = current_f;
if(f != 0)
{
current_f += f;
if(current_f > block_frames[current_b]-1)
current_f = 0;
if(current_f < 0)
current_f = block_frames[current_b]-1;
}
if(valid_position(current_x + x, current_y + y, current_b, current_f))
{
draw_block(current_x,current_y,current_b,last_frame,true);
current_x += x;
current_y += y;
draw_block(current_x,current_y,current_b,current_f,false);
lcd_update();
}
else
current_f = last_frame;
}
void new_block(void)
{
current_b = next_b;
current_f = next_f;
current_x = max_x - 15;
current_y = (int)((max_y)/2);
next_b = t_rand(blocks);
next_f = t_rand(block_frames[next_b]);
// draw_block(max_x + 2, start_y - 1, current_b, current_f, true);
// draw_block(max_x + 2, start_y - 1, next_b, next_f, false);
if(!valid_position(current_x, current_y, current_b, current_f))
{
draw_block(current_x, current_y, current_b, current_f, false);
lcd_update();
}
else
draw_block(current_x, current_y, current_b, current_f, false);
}
int check_lines(void)
{
int x,y,i,j,a,b;
bool line;
int lines = 0;
for(x = 0; x < max_x; x++)
{
line = true;
for(y = 0; y < max_y; y++)
{
if(*(virtual + y * max_x + x) == 0)
{
line = false;
break;
}
}
if(line)
{
lines++;
// move rows down
for(i = x; i < max_x - 1; i++)
for (j = 0; j < max_y; j++)
*(virtual + j * max_x + i) = *(virtual + j * max_x + (i + 1));
x--; // re-check this line
}
}
return lines / 4;
}
void move_down(void)
{
int l;
char s[25];
if(!valid_position(current_x - 4, current_y, current_b, current_f))
{
to_virtual();
l = check_lines();
if(l)
{
lines += l;
level = (int)lines/10;
if(level > 9)
level = 9;
from_virtual();
score += l*l;
}
snprintf (s, sizeof(s), "%d Rows - Level %d", lines, level);
lcd_putsxy (2, 52, s, 0);
new_block();
move_block(0,0,0);
}
else
move_block(-4,0,0);
}
void game_loop(void)
{
while(1)
{
int b=0;
int count = 0;
while(count * 300 < level_speeds[level])
{
b = button_get(false);
if ( b & BUTTON_OFF )
return; /* get out of here */
if ( b & BUTTON_UP )
move_block(0,-4,0);
if ( b & BUTTON_DOWN )
move_block(0,4,0);
if ( b & BUTTON_RIGHT )
move_block(0,0,-1);
if ( b & BUTTON_LEFT )
move_down();
count++;
sleep(HZ/10);
}
if(gameover())
{
int w, h;
lcd_getfontsize(TETRIS_TITLE_FONT, &w, &h);
lcd_clearrect(TETRIS_TITLE_XLOC, TETRIS_TITLE_YLOC,
TETRIS_TITLE_XLOC+(w*sizeof(TETRIS_TITLE)),
TETRIS_TITLE_YLOC-h);
lcd_putsxy(TETRIS_TITLE_XLOC, TETRIS_TITLE_YLOC, "You lose!",
TETRIS_TITLE_FONT);
lcd_update();
sleep(HZ);
return;
}
move_down();
}
}
void init_tetris(void)
{
memset(&virtual, 0, sizeof(virtual));
current_x = 0;
current_y = 0;
current_f = 0;
current_b = 0;
level = 0;
lines = 0;
score = 0;
next_b = 0;
next_f = 0;
}
void tetris(void)
{
init_tetris();
draw_frame(start_x, start_x + max_x - 1, start_y - 1, start_y + max_y);
lcd_putsxy (2, 52, "0 Rows - Level 0", 0);
lcd_update();
next_b = t_rand(blocks);
next_f = t_rand(block_frames[next_b]);
new_block();
game_loop();
}
#endif
|