Tetris game on mikroTFT touchscreen and LPC1768

Dependencies:   Tetris

Dependents:   Tetris

Committer:
sergun2311
Date:
Fri Mar 03 13:19:26 2017 +0000
Revision:
2:6b6986c3d2bd
Parent:
1:b4aa36ae11ac
Child:
3:36de55e63fdf
TETRIS

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sergun2311 0:645509d95b8d 1 #include "mbed.h"
sergun2311 0:645509d95b8d 2 #include "SeeedStudioTFTv2.h"
sergun2311 0:645509d95b8d 3 #include "Piece.h"
sergun2311 0:645509d95b8d 4 #include "Block.h"
sergun2311 0:645509d95b8d 5 #include "Field.h"
sergun2311 1:b4aa36ae11ac 6 #include "Arial24x23.h"
sergun2311 2:6b6986c3d2bd 7 #include "Arial12x12.h"
sergun2311 2:6b6986c3d2bd 8 #include <ctime>
sergun2311 0:645509d95b8d 9
sergun2311 2:6b6986c3d2bd 10 #define PIN_XP p20
sergun2311 2:6b6986c3d2bd 11 #define PIN_XM p19
sergun2311 2:6b6986c3d2bd 12 #define PIN_YP p18
sergun2311 2:6b6986c3d2bd 13 #define PIN_YM p17
sergun2311 2:6b6986c3d2bd 14 #define PIN_MOSI p11
sergun2311 2:6b6986c3d2bd 15 #define PIN_MISO p12
sergun2311 2:6b6986c3d2bd 16 #define PIN_SCLK p13
sergun2311 2:6b6986c3d2bd 17 #define PIN_CS_TFT p14
sergun2311 2:6b6986c3d2bd 18 #define PIN_DC_TFT p21
sergun2311 2:6b6986c3d2bd 19 #define PIN_BL_TFT p15
sergun2311 2:6b6986c3d2bd 20 #define PIN_CS_SD p4
sergun2311 2:6b6986c3d2bd 21 #define PURPLE 0x780F
sergun2311 2:6b6986c3d2bd 22 #define DARKGREY 0x7BEF
sergun2311 2:6b6986c3d2bd 23 #define BLOCK_SIZE 20
sergun2311 2:6b6986c3d2bd 24 #define SMALL_BLOCK_SIZE 8
sergun2311 2:6b6986c3d2bd 25 #define MAXX 10
sergun2311 2:6b6986c3d2bd 26 #define MAXY 12
sergun2311 0:645509d95b8d 27
sergun2311 0:645509d95b8d 28 SeeedStudioTFTv2 TFT(PIN_XP, PIN_XM, PIN_YP, PIN_YM, PIN_MOSI, PIN_MISO, PIN_SCLK, PIN_CS_TFT, PIN_DC_TFT, PIN_BL_TFT);
sergun2311 0:645509d95b8d 29
sergun2311 0:645509d95b8d 30 void setColor( int y, int x, int color )
sergun2311 0:645509d95b8d 31 {
sergun2311 0:645509d95b8d 32 Field[y][x] = color;
sergun2311 0:645509d95b8d 33 }
sergun2311 0:645509d95b8d 34
sergun2311 0:645509d95b8d 35 void drawMap()
sergun2311 0:645509d95b8d 36 {
sergun2311 0:645509d95b8d 37 int y , x;
sergun2311 0:645509d95b8d 38 for ( y = 0 ; y < MAXY ; y++ ) {
sergun2311 0:645509d95b8d 39 for ( x = 0 ; x < MAXX ; x++ ) {
sergun2311 0:645509d95b8d 40 if ( Field[y][x] != 0 ) {
sergun2311 0:645509d95b8d 41 TFT.fillrect(20 * ( x + 1 ), 20 * y,
sergun2311 0:645509d95b8d 42 BLOCK_SIZE * ( x + 2 ), BLOCK_SIZE * ( y + 1 ),
sergun2311 0:645509d95b8d 43 Field[y][x]);
sergun2311 0:645509d95b8d 44 TFT.rect(BLOCK_SIZE * ( x + 1 ), BLOCK_SIZE * y,
sergun2311 0:645509d95b8d 45 BLOCK_SIZE * ( x + 2 ), BLOCK_SIZE * ( y + 1 ),
sergun2311 0:645509d95b8d 46 0xFFFF );
sergun2311 0:645509d95b8d 47 }
sergun2311 0:645509d95b8d 48 }
sergun2311 0:645509d95b8d 49 }
sergun2311 0:645509d95b8d 50 }
sergun2311 0:645509d95b8d 51
sergun2311 1:b4aa36ae11ac 52 void drawMapV2()
sergun2311 1:b4aa36ae11ac 53 {
sergun2311 1:b4aa36ae11ac 54 int y , x;
sergun2311 1:b4aa36ae11ac 55 for ( y = 0 ; y < MAXY ; y++ ) {
sergun2311 1:b4aa36ae11ac 56 for ( x = 0 ; x < MAXX ; x++ ) {
sergun2311 1:b4aa36ae11ac 57 TFT.fillrect(20 * ( x + 1 ), 20 * y,
sergun2311 1:b4aa36ae11ac 58 BLOCK_SIZE * ( x + 2 ), BLOCK_SIZE * ( y + 1 ),
sergun2311 1:b4aa36ae11ac 59 Field[y][x]);
sergun2311 1:b4aa36ae11ac 60 if ( Field[y][x] != 0 )
sergun2311 1:b4aa36ae11ac 61 TFT.rect(BLOCK_SIZE * ( x + 1 ), BLOCK_SIZE * y,
sergun2311 1:b4aa36ae11ac 62 BLOCK_SIZE * ( x + 2 ), BLOCK_SIZE * ( y + 1 ),
sergun2311 1:b4aa36ae11ac 63 0xFFFF );
sergun2311 1:b4aa36ae11ac 64 }
sergun2311 1:b4aa36ae11ac 65 }
sergun2311 1:b4aa36ae11ac 66 }
sergun2311 1:b4aa36ae11ac 67
sergun2311 0:645509d95b8d 68 void drawBlock(Block NewBlock)
sergun2311 0:645509d95b8d 69 {
sergun2311 0:645509d95b8d 70 int ix , iy , x , y;
sergun2311 0:645509d95b8d 71 x = NewBlock.x;
sergun2311 0:645509d95b8d 72 y = NewBlock.y;
sergun2311 0:645509d95b8d 73 for ( ix = x - 2 ; ix < x + 2 ; ix++ ) {
sergun2311 0:645509d95b8d 74 for ( iy = y - 2 ; iy < y + 2 ; iy++ )
sergun2311 0:645509d95b8d 75 if ( Piece[NewBlock.form][NewBlock.angle][ix - x + 2][iy - y + 2] != 0 ) {
sergun2311 0:645509d95b8d 76 TFT.fillrect(BLOCK_SIZE * ( ix + 1 ), BLOCK_SIZE * iy,
sergun2311 0:645509d95b8d 77 BLOCK_SIZE * ( ix + 2 ), BLOCK_SIZE * ( iy + 1 ),
sergun2311 0:645509d95b8d 78 Piece[NewBlock.form][NewBlock.angle][ix - x + 2][iy - y + 2]);
sergun2311 0:645509d95b8d 79 TFT.rect(BLOCK_SIZE * ( ix + 1 ), BLOCK_SIZE * iy,
sergun2311 0:645509d95b8d 80 BLOCK_SIZE * ( ix + 2 ), BLOCK_SIZE * ( iy + 1 ),
sergun2311 0:645509d95b8d 81 0xFFFF );
sergun2311 0:645509d95b8d 82 }
sergun2311 0:645509d95b8d 83 }
sergun2311 0:645509d95b8d 84 }
sergun2311 0:645509d95b8d 85
sergun2311 0:645509d95b8d 86 void clrBlock(Block NewBlock)
sergun2311 0:645509d95b8d 87 {
sergun2311 0:645509d95b8d 88 int ix , iy , x , y;
sergun2311 0:645509d95b8d 89 x = NewBlock.x;
sergun2311 0:645509d95b8d 90 y = NewBlock.y;
sergun2311 0:645509d95b8d 91 for ( ix = x - 2 ; ix < x + 2 ; ix++ ) {
sergun2311 0:645509d95b8d 92 for ( iy = y - 2 ; iy < y + 2 ; iy++ )
sergun2311 0:645509d95b8d 93 if ( Piece[NewBlock.form][NewBlock.angle][ix - x + 2][iy - y + 2] != 0 ) {
sergun2311 0:645509d95b8d 94 TFT.fillrect(BLOCK_SIZE * ( ix + 1 ), BLOCK_SIZE * iy,
sergun2311 0:645509d95b8d 95 BLOCK_SIZE * ( ix + 2 ), BLOCK_SIZE * ( iy + 1 ),
sergun2311 0:645509d95b8d 96 0 );
sergun2311 0:645509d95b8d 97 }
sergun2311 0:645509d95b8d 98 }
sergun2311 0:645509d95b8d 99 }
sergun2311 0:645509d95b8d 100
sergun2311 0:645509d95b8d 101 void drawFrame()
sergun2311 0:645509d95b8d 102 {
sergun2311 0:645509d95b8d 103 int x, y;
sergun2311 0:645509d95b8d 104 for ( y = 0 ; y < 260 ; y += 20 ) {
sergun2311 0:645509d95b8d 105 TFT.fillrect(0, y, 20, y + 20, PURPLE );
sergun2311 0:645509d95b8d 106 TFT.rect(0, y, 20, y + 20, DARKGREY );
sergun2311 0:645509d95b8d 107 TFT.fillrect(220, y, 240, y + 20, PURPLE );
sergun2311 0:645509d95b8d 108 TFT.rect(220, y, 240, y + 20, DARKGREY );
sergun2311 0:645509d95b8d 109 }
sergun2311 0:645509d95b8d 110 for ( x = 0 ; x < 240 ; x += 20 ) {
sergun2311 0:645509d95b8d 111 TFT.fillrect(x, 240, x + 20, 260, PURPLE );
sergun2311 0:645509d95b8d 112 TFT.rect(x, 240, x + 20, 260, DARKGREY );
sergun2311 0:645509d95b8d 113
sergun2311 0:645509d95b8d 114 }
sergun2311 0:645509d95b8d 115 }
sergun2311 0:645509d95b8d 116
sergun2311 0:645509d95b8d 117 void saveToField(Block NewBlock)
sergun2311 0:645509d95b8d 118 {
sergun2311 0:645509d95b8d 119 int xx , yy;
sergun2311 0:645509d95b8d 120 for ( xx = 0 ; xx < 5 ; xx++ ) {
sergun2311 0:645509d95b8d 121 for (yy = 0 ; yy < 5 ; yy++ ) {
sergun2311 0:645509d95b8d 122 if ( Piece[NewBlock.form][NewBlock.angle][xx][yy] != 0 )
sergun2311 0:645509d95b8d 123 Field[NewBlock.y + yy - 2][NewBlock.x + xx - 2] =
sergun2311 0:645509d95b8d 124 Piece[NewBlock.form][NewBlock.angle][xx][yy];
sergun2311 0:645509d95b8d 125 }
sergun2311 0:645509d95b8d 126 }
sergun2311 0:645509d95b8d 127 }
sergun2311 0:645509d95b8d 128
sergun2311 0:645509d95b8d 129 void TFTInit()
sergun2311 0:645509d95b8d 130 {
sergun2311 0:645509d95b8d 131 TFT.set_orientation(0);
sergun2311 0:645509d95b8d 132 TFT.foreground(White);
sergun2311 0:645509d95b8d 133 TFT.background(0);
sergun2311 0:645509d95b8d 134 TFT.cls();
sergun2311 1:b4aa36ae11ac 135 TFT.set_font((unsigned char*) Arial24x23);
sergun2311 0:645509d95b8d 136 drawFrame();
sergun2311 0:645509d95b8d 137 }
sergun2311 0:645509d95b8d 138
sergun2311 1:b4aa36ae11ac 139 int getGesture()
sergun2311 1:b4aa36ae11ac 140 {
sergun2311 1:b4aa36ae11ac 141 point p;
sergun2311 2:6b6986c3d2bd 142 clock_t start_s = clock();
sergun2311 2:6b6986c3d2bd 143 int flag ,x ,y ;
sergun2311 2:6b6986c3d2bd 144 flag = x = y = 0;
sergun2311 1:b4aa36ae11ac 145 while( !flag ) {
sergun2311 1:b4aa36ae11ac 146 p.x=0;
sergun2311 1:b4aa36ae11ac 147 p.y=0;
sergun2311 2:6b6986c3d2bd 148 if ( ( TFT.getTouch(p)==TFT.YES ) || ( TFT.getTouch(p)==TFT.MAYBE ) ) {
sergun2311 1:b4aa36ae11ac 149 TFT.getTouch(p); // read analog pos.
sergun2311 1:b4aa36ae11ac 150 TFT.getPixel(p); // convert to pixel pos
sergun2311 1:b4aa36ae11ac 151 flag = 1;
sergun2311 1:b4aa36ae11ac 152 x = p.x;
sergun2311 1:b4aa36ae11ac 153 y = p.y;
sergun2311 2:6b6986c3d2bd 154 } else if ( start_s > 10 )
sergun2311 2:6b6986c3d2bd 155 return 13;
sergun2311 1:b4aa36ae11ac 156 }
sergun2311 2:6b6986c3d2bd 157 if ( ( x < 25 ) ) {
sergun2311 1:b4aa36ae11ac 158 return 0 ;
sergun2311 1:b4aa36ae11ac 159 }
sergun2311 2:6b6986c3d2bd 160 if ( x > 35 ) {
sergun2311 2:6b6986c3d2bd 161 if (( y > 170 ) && ( x < 190 ) && ( x > 70 ))//To the RIGHT
sergun2311 1:b4aa36ae11ac 162 return 1 ;
sergun2311 2:6b6986c3d2bd 163 if (( y < 60 ) && ( x < 190 ) && ( x > 70 )) //To the LEFT
sergun2311 1:b4aa36ae11ac 164 return 2 ;
sergun2311 2:6b6986c3d2bd 165 if ( x > 190 ) //To the TOP
sergun2311 1:b4aa36ae11ac 166 return 3 ;
sergun2311 2:6b6986c3d2bd 167 if ( x < 80 ) //To the BOTTOM
sergun2311 1:b4aa36ae11ac 168 return 4 ;
sergun2311 1:b4aa36ae11ac 169 }
sergun2311 2:6b6986c3d2bd 170 return 13; //13 = IGNORE
sergun2311 1:b4aa36ae11ac 171 }
sergun2311 1:b4aa36ae11ac 172
sergun2311 1:b4aa36ae11ac 173 bool TouchStatus()
sergun2311 1:b4aa36ae11ac 174 {
sergun2311 1:b4aa36ae11ac 175 point p;
sergun2311 1:b4aa36ae11ac 176 if ( ( TFT.getTouch(p)==TFT.YES ) || ( TFT.getTouch(p)==TFT.MAYBE ) )
sergun2311 1:b4aa36ae11ac 177 return true;
sergun2311 1:b4aa36ae11ac 178 return false;
sergun2311 1:b4aa36ae11ac 179 }
sergun2311 1:b4aa36ae11ac 180
sergun2311 1:b4aa36ae11ac 181 Block doGest(Block NewBlock)
sergun2311 1:b4aa36ae11ac 182 {
sergun2311 1:b4aa36ae11ac 183 int gest = getGesture();
sergun2311 1:b4aa36ae11ac 184 if ( gest != 13 ) {
sergun2311 2:6b6986c3d2bd 185 //clrBlock(NewBlock);
sergun2311 1:b4aa36ae11ac 186 switch ( gest ) {
sergun2311 1:b4aa36ae11ac 187 case 0: {
sergun2311 1:b4aa36ae11ac 188 while ( !NewBlock.CheckBottom() ) {
sergun2311 1:b4aa36ae11ac 189 NewBlock.y++;
sergun2311 1:b4aa36ae11ac 190 }
sergun2311 1:b4aa36ae11ac 191 saveToField(NewBlock);
sergun2311 1:b4aa36ae11ac 192 drawFrame();
sergun2311 1:b4aa36ae11ac 193 break;
sergun2311 1:b4aa36ae11ac 194 }
sergun2311 1:b4aa36ae11ac 195 case 1: {
sergun2311 1:b4aa36ae11ac 196 NewBlock.moveRight();
sergun2311 1:b4aa36ae11ac 197 break;
sergun2311 1:b4aa36ae11ac 198 }
sergun2311 1:b4aa36ae11ac 199 case 2: {
sergun2311 1:b4aa36ae11ac 200 NewBlock.moveLeft();
sergun2311 1:b4aa36ae11ac 201 break;
sergun2311 1:b4aa36ae11ac 202 }
sergun2311 1:b4aa36ae11ac 203 case 3: {
sergun2311 1:b4aa36ae11ac 204 NewBlock.rotateLeft();
sergun2311 1:b4aa36ae11ac 205 break;
sergun2311 1:b4aa36ae11ac 206 }
sergun2311 1:b4aa36ae11ac 207 case 4: {
sergun2311 1:b4aa36ae11ac 208 NewBlock.rotateRight();
sergun2311 1:b4aa36ae11ac 209 break;
sergun2311 1:b4aa36ae11ac 210 }
sergun2311 1:b4aa36ae11ac 211 }
sergun2311 1:b4aa36ae11ac 212 }
sergun2311 1:b4aa36ae11ac 213 return NewBlock;
sergun2311 1:b4aa36ae11ac 214 }
sergun2311 1:b4aa36ae11ac 215
sergun2311 1:b4aa36ae11ac 216 void gameOver(int score)
sergun2311 1:b4aa36ae11ac 217 {
sergun2311 1:b4aa36ae11ac 218 TFT.cls();
sergun2311 1:b4aa36ae11ac 219 TFT.locate(40,160);
sergun2311 1:b4aa36ae11ac 220 TFT.printf("Score : %i", score);
sergun2311 2:6b6986c3d2bd 221 }
sergun2311 2:6b6986c3d2bd 222
sergun2311 2:6b6986c3d2bd 223 void drawScore(int score)
sergun2311 2:6b6986c3d2bd 224 {
sergun2311 2:6b6986c3d2bd 225 TFT.set_font((unsigned char*) Arial12x12);
sergun2311 2:6b6986c3d2bd 226 TFT.locate(0,300);
sergun2311 2:6b6986c3d2bd 227 TFT.printf("Score : %i", score);
sergun2311 2:6b6986c3d2bd 228 TFT.set_font((unsigned char*) Arial24x23);
sergun2311 2:6b6986c3d2bd 229
sergun2311 2:6b6986c3d2bd 230 }
sergun2311 2:6b6986c3d2bd 231
sergun2311 2:6b6986c3d2bd 232 void drawNextBlock(Block NewBlock)
sergun2311 2:6b6986c3d2bd 233 {
sergun2311 2:6b6986c3d2bd 234 int ix , iy , x , y;
sergun2311 2:6b6986c3d2bd 235 x = 0;
sergun2311 2:6b6986c3d2bd 236 y = 0;
sergun2311 2:6b6986c3d2bd 237 for ( ix = x - 1 ; ix < x + 3 ; ix++ ) {
sergun2311 2:6b6986c3d2bd 238 for ( iy = y - 2 ; iy < y + 2 ; iy++ )
sergun2311 2:6b6986c3d2bd 239 if ( Piece[NewBlock.nextForm][NewBlock.angle][ix - x + 2][iy - y + 2] != 0 ) {
sergun2311 2:6b6986c3d2bd 240 TFT.fillrect(SMALL_BLOCK_SIZE * ( ix + 1 ) + 200, SMALL_BLOCK_SIZE * iy + 280,
sergun2311 2:6b6986c3d2bd 241 SMALL_BLOCK_SIZE * ( ix + 2 ) + 200, SMALL_BLOCK_SIZE * ( iy + 1 ) + 280,
sergun2311 2:6b6986c3d2bd 242 Piece[NewBlock.nextForm][NewBlock.angle][ix - x + 2][iy - y + 2]);
sergun2311 2:6b6986c3d2bd 243 TFT.rect(SMALL_BLOCK_SIZE * ( ix + 1 ) + 200, SMALL_BLOCK_SIZE * iy + 280,
sergun2311 2:6b6986c3d2bd 244 SMALL_BLOCK_SIZE * ( ix + 2 ) + 200, SMALL_BLOCK_SIZE * ( iy + 1 ) + 280,
sergun2311 2:6b6986c3d2bd 245 0xFFFF );
sergun2311 2:6b6986c3d2bd 246 }
sergun2311 2:6b6986c3d2bd 247 }
sergun2311 2:6b6986c3d2bd 248 }
sergun2311 2:6b6986c3d2bd 249
sergun2311 2:6b6986c3d2bd 250 void clrNextBlock(Block NewBlock)
sergun2311 2:6b6986c3d2bd 251 {
sergun2311 2:6b6986c3d2bd 252 TFT.fillrect( 180 , 263 , 240, 320 , 0 );
sergun2311 1:b4aa36ae11ac 253 }