Touchtetris3_small
Reddit Twitter Digg Facebook Delicious Stumbleupon
4 images, 1 part, 4 comments

TouchShield Tetris

A first attempt at the popular Tetris game on the TouchShield GamePack!

Parts for TouchShield Tetris:

Even though all the standard tetris features aren’t there yet, it’s still pretty fun to play so far!

Above is an up close and personal shot. Besides the fun joystick control, my favorite part of this game is the next shape box, strangely.

The Arduino forwards the joytstick control from the InputShield to the TouchShield which allows the TouchShield to control gameplay and display.


Copy and paste the following code listing and download to the respective device.
  • TouchShield Code
  • Arduino Code

TouchShield Code

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


#define GRID_WIDTH  10
#define GRID_HEIGHT 20

#define CELL_WIDTH    6
#define CELL_HEIGHT   6

typedef enum
{
    NOT_OCCUPIED,
    OCCUPIED,
    NUM_STATES
} GRID_CELL_STATE_T;

typedef enum
{
    MOVED,
    NOT_MOVED,
} SHAPE_MOVEMENT_T;

typedef struct
{
    LCD_RECT           Cell;
    COLOR              Color;
    GRID_CELL_STATE_T  State;
} GRID_CELL_T;

typedef enum
{
    ANG_0,
    ANG_90,
    ANG_180,
    ANG_270,
    NUM_ROTATIONS
} SHAPE_ROTATION_T;

/* 4 rotations of shape offets */ 
typedef struct
{
    POINT  Pattern[4][4];
} PATTERN_T;

typedef struct
{
    PATTERN_T         ShapeData[4];
    POINT             ShapeLoc[4];
    COLOR             Color;
    uint8_t           CurrAngle;
    uint8_t           CurrShape;
    SHAPE_MOVEMENT_T  State;
    uint8_t           NextShape;
    COLOR             DrawColor;
} SHAPE_T;

SHAPE_T  shape1 = {
    /*  Shape 0  */
        /* ANG_0      */{{{ {{0,2}, {1,2}, {2,2}, {1,3}},
        /* ANG_90     */    {{1,1}, {1,2}, {0,2}, {1,3}},
        /* ANG_180    */    {{0,2}, {1,2}, {2,2}, {1,1}},  
        /* ANG_270    */    {{1,1}, {1,2}, {2,2}, {1,3}}}},   
    /*  Shape 1  */
        /* ANG_0      */{{ {{0,2}, {1,2}, {0,3}, {1,3}},
        /* ANG_90     */   {{0,2}, {1,2}, {0,3}, {1,3}},
        /* ANG_180    */   {{0,2}, {1,2}, {0,3}, {1,3}},
        /* ANG_270    */   {{0,2}, {1,2}, {0,3}, {1,3}}}},
    /*  Shape 2  */
    /* ANG_0      */{{ {{1,0}, {1,1}, {1,2}, {1,3}},
        /* ANG_90     */   {{0,2}, {1,2}, {2,2}, {3,2}},
        /* ANG_180    */   {{1,0}, {1,1}, {1,2}, {1,3}},
        /* ANG_270    */   {{0,2}, {1,2}, {2,2}, {3,2}}}},
    /*  Shape 3  */
    /* ANG_0      */{{ {{0,2}, {1,2}, {1,3}, {2,3}},
        /* ANG_90     */   {{0,2}, {0,3}, {1,1}, {1,2}},
        /* ANG_180    */   {{0,2}, {1,2}, {1,3}, {2,3}},
        /* ANG_270    */   {{0,2}, {0,3}, {1,1}, {1,2}}}}},                 
    /* ShapeLoc  */{{5,0}, {5,0}, {5,0}, {5,0}},
    /* Color     */{255,0,0},
    /* CurrAngle */ 0,
    /* CurrShape */ 0,
    /* State     */ NOT_MOVED,
    /* NextShape */ 1,
    /* DrawColor */ {255,0,0},
};

GRID_CELL_T grid[GRID_WIDTH][GRID_HEIGHT];
GRID_CELL_T nextShapeGrid[4][4];

COLOR black = {0,0,0};
COLOR grey = {50,50,50};
COLOR white = {255,255,255};
COLOR red = {255,0,0};

unsigned int analogValues[6];
unsigned char digitalValues[10];

void setupGrids()
{
    unsigned char x,y;

    /* Setup the main playing grid */
    for (x=0;x<GRID_WIDTH; x++)
    {
        for (y=0;y<GRID_HEIGHT; y++)
        {
            grid[x][y].Cell.left = x*CELL_WIDTH;
            grid[x][y].Cell.right = grid[x][y].Cell.left 
                                    + CELL_WIDTH;
            grid[x][y].Cell.top = y*CELL_HEIGHT;
            grid[x][y].Cell.bottom = grid[x][y].Cell.top
                                     +CELL_HEIGHT;
            grid[x][y].Color.red=0; 
            grid[x][y].Color.green=0;
            grid[x][y].Color.blue=0; 
            grid[x][y].State = NOT_OCCUPIED;   
        }
    }

    /* Add a occupied bottom row */
    for (x=0;x<GRID_WIDTH; x++)
    {
        grid[x][GRID_HEIGHT-1].State = OCCUPIED;
        grid[x][GRID_HEIGHT-1].Color.red = 255;
    }    

    /* Add a occupied bottom row */
    for (x=0;x<GRID_HEIGHT; x++)
    {
        grid[0][x].State = OCCUPIED;
        grid[GRID_WIDTH-1][x].State = OCCUPIED;
    }    

    /* Setup the next shape grid */
    for (x=0;x<4; x++)
    {
        for (y=0;y<4; y++)
        {
            nextShapeGrid[x][y].Cell.left = 
            x*CELL_WIDTH+80;
            nextShapeGrid[x][y].Cell.right = 
            nextShapeGrid[x][y].Cell.left + CELL_WIDTH;
            nextShapeGrid[x][y].Cell.top =
            y* CELL_HEIGHT+30;
            nextShapeGrid[x][y].Cell.bottom = 
            nextShapeGrid[x][y].Cell.top+CELL_HEIGHT;  
        }
    }

}

void drawNextShapeGrid()
{
    unsigned char x,y;

    /* Draw the next shape grid */
    for (x=0;x<4; x++)
    {
        for (y=0;y<4; y++)
        {
            lcd_rect(nextShapeGrid[x][y].Cell,grey,black);
        }
    }    
}

void drawGrids()
{
    unsigned char x,y;

    /* Draw the playing grid */
    for (x=0;x< GRID_WIDTH; x++)
    {
        for (y=0;y<GRID_HEIGHT; y++)
        {
            lcd_rect(grid[x][y].Cell,grey,grid[x][y].Color);
        }
    }

    drawNextShapeGrid(); 

}

void drawShape()
{
    unsigned char x, left, top;

    for (x=0;x<4;x++)
    {
        left = (shape1.ShapeLoc[x].x + 
                shape1.ShapeData[shape1.CurrShape].
                Pattern[shape1.CurrAngle][x].x)*
               CELL_WIDTH;

        top = (shape1.ShapeLoc[x].y + 
               shape1.ShapeData[shape1.CurrShape].
               Pattern[shape1.CurrAngle][x].y)*
              CELL_HEIGHT;

        lcd_rectangle(left,
                      top,
                      left+CELL_WIDTH,
                      top+CELL_HEIGHT, 
                      grey,shape1.Color);
    }
}

void drawNextShape()
{
    unsigned char x, left, top;

    for (x=0;x<4;x++)
    {
        left = (nextShapeGrid[0][0].Cell.left +
                shape1.ShapeData[shape1.NextShape].
                Pattern[shape1.CurrAngle][x].x*CELL_WIDTH);

        top = (nextShapeGrid[0][0].Cell.top +
               shape1.ShapeData[shape1.NextShape].
               Pattern[shape1.CurrAngle][x].y*CELL_HEIGHT);

        lcd_rectangle(left,
                      top,
                      left+CELL_WIDTH,
                      top+CELL_HEIGHT, 
                      grey,shape1.Color);
    }
}

bool collisionSide()
{
    unsigned char x,locx,locy;

    /* Test for a collision */
    for (x=0;x<4;x++)
    {
        locx = shape1.ShapeLoc[x].x + 
               shape1.ShapeData[shape1.CurrShape].
               Pattern[shape1.CurrAngle][x].x;
        locy = shape1.ShapeLoc[x].y + 
               shape1.ShapeData[shape1.CurrShape].
               Pattern[shape1.CurrAngle][x].y;

        if ((grid[locx-1][locy].State == OCCUPIED) ||
            (grid[locx+1][locy].State == OCCUPIED) )
        {
            return true;
        }

    }    
    return false;

}

bool collision()
{

    unsigned char x,locx,locy;

    /* Test for a collision */
    for (x=0;x<4;x++)
    {
        locx = shape1.ShapeLoc[x].x + 
               shape1.ShapeData[shape1.CurrShape].
               Pattern[shape1.CurrAngle][x].x;
        locy = shape1.ShapeLoc[x].y + 
               shape1.ShapeData[shape1.CurrShape].
               Pattern[shape1.CurrAngle][x].y;

        if (grid[locx][locy+1].State == OCCUPIED)
        {
            return true;
        }

    }    
    return false;
}

void moveShape(char xAmount, char yAmount)
{
    unsigned char x,locx,locy;

    if (!collision())
    {
        /* Move Each Cell */
        for (x=0;x<4;x++)
        {
            shape1.ShapeLoc[x].y+= yAmount;
            shape1.ShapeLoc[x].x+= xAmount;
        }

        /* Store the movement */
        shape1.State = MOVED;  
    } else
    {
        /* Paint the stationary shape */

        /* Paint the shape */
        shape1.Color.red = shape1.DrawColor.red;
        shape1.Color.green = shape1.DrawColor.green;
        shape1.Color.blue = shape1.DrawColor.blue;
        drawShape();

        /* Occupy Grid cells */
        for (x=0;x<4;x++)
        {

            locx = shape1.ShapeLoc[x].x + 
                   shape1.ShapeData[shape1.CurrShape].
                   Pattern[shape1.CurrAngle][x].x;
            locy = shape1.ShapeLoc[x].y + 
                   shape1.ShapeData[shape1.CurrShape].
                   Pattern[shape1.CurrAngle][x].y;
            grid[locx][locy].State = OCCUPIED;

            /* Reset the location */
            shape1.ShapeLoc[x].x = 5;
            shape1.ShapeLoc[x].y = 0;

        }

        shape1.CurrShape = shape1.NextShape;
        shape1.NextShape = random() % 4;
        drawNextShapeGrid();
        drawNextShape();
        shape1.DrawColor.red = random() % 255;
        shape1.DrawColor.green = random() % 255;
        shape1.DrawColor.blue = random() % 255;
    }  
}

void setup()
{
    Serial.begin(9600);
    delay(3000);
    /* The sync character */
    Serial.print('U');

    setupGrids();
    drawGrids();
    drawNextShape();

    lcd_puts("Touch",80,0,red, black);
    lcd_puts("Tetris",77,10,red, black);
    lcd_puts("Next Shape",68,65,red, black);

    lcd_puts("Score: 237",68,100,red, black);
}

POINT p;
long pTime, nTime;
SHAPE_MOVEMENT_T ShapeMoveResult = NOT_MOVED;
unsigned char button1State = HIGH; 
unsigned char button1StatePrev = HIGH;
unsigned int joystick;
unsigned int joystickPrev;
unsigned char x;

void loop()
{
    /* Test for a moving shape to erase */
    if (shape1.State == MOVED)
    {
        /* Erase the shape */
        shape1.Color.red = 0;
        shape1.Color.green = 0;
        shape1.Color.blue = 0;
        drawShape();
    }

    /* Move the shape downward 1 lcoation */
    moveShape(0,1);

    /* Paint the shape */
    shape1.Color.red = shape1.DrawColor.red;
    shape1.Color.green = shape1.DrawColor.green;
    shape1.Color.blue = shape1.DrawColor.blue;
    drawShape();

    /* Store the time */
    pTime = millis();

    //Read digital values for buttons a and b stat
    Serial.print('U');
    button1State = Serial.read();
    joystick = Serial.read();

    /* Check for an elapsed amount of time */   
    if (joystick<50)
    {
        /* Erase the shape */
        shape1.Color.red = 0;
        shape1.Color.green = 0;
        shape1.Color.blue = 0;

        drawShape();
        if (!collisionSide())
        {
            for (x=0;x<4;x++)
            {
                shape1.ShapeLoc[x].x--;
            }
        }
    }

    if (joystick>200)
    {
        /* Erase the shape */
        shape1.Color.red = 0;
        shape1.Color.green = 0;
        shape1.Color.blue = 0;

        drawShape();
        if (!collisionSide())
        {
            for (x=0;x<4;x++)
            {
                shape1.ShapeLoc[x].x++;
            }
        }
    }

    if ( (button1State != button1StatePrev))
    {
        if ( (button1State == LOW ))
        {
            /* Erase the shape */
            shape1.Color.red = 0;
            shape1.Color.green = 0;
            shape1.Color.blue = 0;

            drawShape();

            /* Rotate the shape */
            shape1.CurrAngle++;

            /* Range check the angle */
            if (shape1.CurrAngle == 4)
            {
                /* Wrap back around the angle */
                shape1.CurrAngle=0;  
            }

            /* Paint the shape */
            shape1.Color.red = shape1.DrawColor.red;
            shape1.Color.green = shape1.DrawColor.green;
            shape1.Color.blue = shape1.DrawColor.blue;
            drawShape();            
        }
        button1StatePrev = button1State;
    }

    {
        button1StatePrev = button1State;
    }      
    delay(100);
}

Arduino Code

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

#include <AFSoftSerial.h>

#define RXPIN 3
#define TXPIN 2

AFSoftSerial mySerial = AFSoftSerial(RXPIN, TXPIN);

unsigned char x=0;

void setup()
{
  Serial.begin(9600);
mySerial.begin(9600);

/* Sync up by waiting for character */
//while(mySerial.read() != 'U');
//while(mySerial.read() != 'U');
delay(200);
}

void loop()
{

  serial_sendDigital(6);
  serial_sendAnalog(5);
   delay(100);
}

void serial_sendDigital(unsigned char digitalPin)
{

if ( (digitalPin > 13) )
return;

mySerial.print((unsigned char)digitalRead(digitalPin));
delay(2);

}

void serial_sendAnalog(unsigned char analogPin)
{
unsigned char lowByte, highByte;
unsigned int val;

/* Pin number range check */
if (analogPin > 6)
return;

/* Get the value */
val = analogRead(analogPin);
val /=4;

/* Separate the value into 2 bytes */
lowByte = (unsigned char)val;
highByte = (unsigned char)(val >> 8);

/* Send the high byte */
//mySerial.print(highByte);

/* Write delay */
//delay(1);

/* Send the low byte */
mySerial.print(lowByte);
Serial.print(val);

Serial.print(" ");
/* Write delay */
///delay(1);
} 

4 Comments on TouchShield Tetris

Chris Oct 30 2008

Stay tuned for disappearing blocks and levels!

Aaron Nov 12 2008

if you put in square tetris (like on the new tetris for N64). I will SO be buying an input shield.

Mark Dec 21 2008

Please put more comments in the code so new users can understand how it all works

Thanks

John Mar 31 2009

doesn’t look this this works… I’ve tried to compile the code and I’m using the latest ide for the touch shield..

handling the run

Preparing libraries with target=processing.app.Target@a62593 build path=/tmp/build47560.tmp

/tmp/build47560.tmp/FlashFileSystem.cpp.o:(.bss.black+0×0): multiple definition of `black’

o:(.bss.black+0×0): first defined here /tmp/build47560.tmp/FlashFileSystem.cpp.o:(.data.white+0×0): multiple definition of `white’

o:(.data.white+0×0): first defined here o: In function `DisplaySplashScreen()’: undefined reference to `lcd_rect’o: In function `drawGrids()’: Temporary_4974_1941.cpp:(.text._Z9drawGridsv+0×78): undefined reference to `lcd_rect’

o: In function `setup’: Temporary_4974_1941.cpp:(.text.setup+0×60): undefined reference to `lcd_puts’

Temporary_4974_1941.cpp:(.text.setup+0×88): undefined reference to `lcd_puts’

Temporary_4974_1941.cpp:(.text.setup+0xb0): undefined reference to `lcd_puts’

Temporary_4974_1941.cpp:(.text.setup+0xd8): undefined reference to `lcd_puts’



Add A Comment: