Tetris for the RA8875, derived from another users implementation.

Dependencies:   RA8875

Fork of Tetris by Sergejs Popovs

Tetris, adapted to the RA8875 graphics library and display.

As currently implemented, this version is defined for the 800x480 display. A number of macros can adapt it for other screen resolutions.

Further, while presently configured for landscape mode, it should be fairly easy to reconfigure it for portrait mode.

Files at this revision

API Documentation at this revision

Comitter:
sergun2311
Date:
Sat Feb 25 23:52:28 2017 +0000
Parent:
0:645509d95b8d
Child:
2:6b6986c3d2bd
Commit message:
Tetris with gestures

Changed in this revision

Block.cpp Show annotated file Show diff for this revision Revisions of this file
Block.h Show annotated file Show diff for this revision Revisions of this file
Field.cpp Show annotated file Show diff for this revision Revisions of this file
Field.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
playGround.cpp Show annotated file Show diff for this revision Revisions of this file
playGround.h Show annotated file Show diff for this revision Revisions of this file
touch.h Show diff for this revision Revisions of this file
--- a/Block.cpp	Mon Feb 20 14:14:30 2017 +0000
+++ b/Block.cpp	Sat Feb 25 23:52:28 2017 +0000
@@ -3,38 +3,91 @@
 #include "playGround.h"
 #include "Piece.h"
 #include "Field.h"
-    
-Block::Block() {
+
+Block::Block()
+{
     form = rand() % 7;
     angle = rand() % 4;
     x = 4 + rand() % 2;
-    y = 0;
-}      
+    y = -1;
+}
+
+Block::~Block()
+{
+}
 
-Block::~Block() {
-    
+void Block::rotateLeft()
+{
+    if ( angle == 0 )  
+        angle = 3;
+    else
+        angle = ( abs(angle - 1) ) % 4;
 }
-     
-void Block::rotateLeft() {
-    angle--;
-}  
+
+void Block::rotateRight()
+{
+    if ( angle == 3 )  
+        angle = 0;
+    else
+        angle = ( abs(angle + 1) ) % 4;
+}
 
-void Block::rotateRight() {
-    angle++;
-}   
+void Block::moveLeft()
+{
+    if ( !CheckLeft() )
+        x--;
+}
+
+void Block::moveRight()
+{
+    if ( !CheckRight() )
+        x++;
+}
 
 bool Block::CheckBottom()
 {
     int xx, yy;
     for ( xx = 0 ; xx < 5 ; xx++ ) {
-        for (yy = 0 ; yy < 5 ; yy++ )  {        
-            if ( (Piece[form][angle][xx][yy] != 0)   
-                && (Field[y + yy - 1][x + xx - 2] != 0) &&
-                ( y + yy - 3 > 0 ) ) 
-                return 1; 
-            if ( (Piece[form][angle][xx][yy] != 0) && ( yy + y == 13 ) )          
+        for (yy = 0 ; yy < 5 ; yy++ )  {
+            if ( (Piece[form][angle][xx][yy] != 0)
+                    && (Field[y + yy - 1][x + xx - 2] != 0) &&
+                    ( y + yy - 1 > 0 ) )
+                return 1;
+            if ( (Piece[form][angle][xx][yy] != 0) && ( yy + y == 13 ) )
                 return 1;
         }
     }
     return 0;
-}  
+}
+
+bool Block::CheckLeft()
+{
+    int xx, yy;
+    for ( xx = 0 ; xx < 5 ; xx++ ) {
+        for (yy = 0 ; yy < 5 ; yy++ )  {
+            if ( (Piece[form][angle][xx][yy] != 0)
+                    && (Field[y + yy - 1][x + xx - 3] != 0) &&
+                    ( y + yy - 3 > 0 ) )
+                return 1;
+            if ( (Piece[form][angle][xx][yy] != 0) && ( xx + x == 2 )  )
+                return 1;
+        }
+    }
+    return 0;
+}
+
+bool Block::CheckRight()
+{
+    int xx, yy;
+    for ( xx = 0 ; xx < 5 ; xx++ ) {
+        for (yy = 0 ; yy < 5 ; yy++ )  {
+            if ( (Piece[form][angle][xx][yy] != 0)
+                    && (Field[y + yy - 1][x + xx - 1] != 0) &&
+                    ( y + yy - 3 > 0 ) )
+                return 1;
+            if ( (Piece[form][angle][xx][yy] != 0) && ( (xx + x) == 11) ) 
+                return 1;
+        }
+    }
+    return 0;
+}
\ No newline at end of file
--- a/Block.h	Mon Feb 20 14:14:30 2017 +0000
+++ b/Block.h	Sat Feb 25 23:52:28 2017 +0000
@@ -13,6 +13,10 @@
         void rotateLeft();
         void rotateRight();
         bool CheckBottom();
+        bool CheckLeft();
+        bool CheckRight();
+        void moveLeft();
+        void moveRight();
 };
     
 #endif
\ No newline at end of file
--- a/Field.cpp	Mon Feb 20 14:14:30 2017 +0000
+++ b/Field.cpp	Sat Feb 25 23:52:28 2017 +0000
@@ -1,6 +1,42 @@
 #include "Field.h"
+#include "playGround.h"
 
+#define BLACK           0
 #define MAXX            10
 #define MAXY            12
 
-extern int Field[MAXY][MAXX] = {0};
\ No newline at end of file
+extern int Field[MAXY][MAXX] = {0};
+
+int checkLine()    {
+    int x, y, score = 0;
+    bool status;
+    for ( y = 0 ; y < 12 ; y++ ) {
+        status = true;
+        for ( x = 0 ; x < 10 ; x++ )  {
+            if ( Field[y][x] == BLACK )
+                status = false;
+        }
+        if ( status )   {
+            score += 100;
+            int xx, yy;
+            for ( yy = y ; yy > 0 ; yy-- )   {
+                for (xx = 0 ; xx < 10 ; xx++ )  {
+                    Field[yy][xx] = Field[yy-1][xx];
+                }
+                
+            }
+        }
+    }
+    if (score)
+        drawMapV2();
+    return score;
+}
+
+bool checkGameOver()
+{
+    int x;
+    for ( x = 0 ; x < 10 ; x++ )
+        if ( Field[0][x] != BLACK )
+            return true;
+    return false;        
+}
\ No newline at end of file
--- a/Field.h	Mon Feb 20 14:14:30 2017 +0000
+++ b/Field.h	Sat Feb 25 23:52:28 2017 +0000
@@ -1,4 +1,6 @@
 #define MAXX            10
 #define MAXY            12
 
-extern int Field[MAXY][MAXX];
\ No newline at end of file
+extern int Field[MAXY][MAXX];
+int checkLine();
+bool checkGameOver();
\ No newline at end of file
--- a/main.cpp	Mon Feb 20 14:14:30 2017 +0000
+++ b/main.cpp	Sat Feb 25 23:52:28 2017 +0000
@@ -2,32 +2,45 @@
 #include <ctime>
 #include "playGround.h"
 #include "Block.h"
+#include "Field.h"
 
-#define SPEED 10
+#define SPEED 100
 
 int main()
 {
-    int flag;
+    int score = 0;
+    bool flag;
     clock_t start_s;
     TFTInit();
     drawMap();
     while (1) {
         Block NewBlock;
-        flag = 0;
+        flag = false;
         drawMap();
-        while(flag == 0) {
+        while( !flag ) {
             drawMap();
             drawBlock(NewBlock);
-            start_s =clock();
+            start_s = clock();
             while( start_s + SPEED > clock() ) {
+                if ( TouchStatus() )    {
+                    clrBlock(NewBlock);
+                    NewBlock = doGest(NewBlock);
+                    drawBlock(NewBlock);
+                    wait_ms(50);
+                }
             }
             if ( NewBlock.CheckBottom() ) {
                 saveToField(NewBlock);
-                flag = 1;
+                flag = true;
             } else {
                 clrBlock(NewBlock);
                 NewBlock.y += 1;
+                drawBlock(NewBlock);
             }
         }
+        score += checkLine();
+        if ( checkGameOver() )
+            break;
     }
+    gameOver(score);
 }
\ No newline at end of file
--- a/playGround.cpp	Mon Feb 20 14:14:30 2017 +0000
+++ b/playGround.cpp	Sat Feb 25 23:52:28 2017 +0000
@@ -3,6 +3,7 @@
 #include "Piece.h"
 #include "Block.h"
 #include "Field.h"
+#include "Arial24x23.h"
 
 #define PIN_XP          p20
 #define PIN_XM          p19
@@ -45,6 +46,22 @@
     }
 }
 
+void drawMapV2()
+{
+    int y , x;
+    for ( y = 0 ; y < MAXY ; y++ ) {
+        for ( x = 0 ; x < MAXX ; x++ ) {
+            TFT.fillrect(20 * ( x + 1 ), 20 * y,
+                             BLOCK_SIZE * ( x + 2 ), BLOCK_SIZE * ( y + 1 ),
+                             Field[y][x]);
+            if ( Field[y][x] != 0 ) 
+                TFT.rect(BLOCK_SIZE * ( x + 1 ), BLOCK_SIZE * y,
+                         BLOCK_SIZE * ( x + 2 ), BLOCK_SIZE * ( y + 1 ),
+                         0xFFFF );
+        }
+    }
+}
+
 void drawBlock(Block NewBlock)
 {
     int ix , iy , x , y;
@@ -112,6 +129,95 @@
     TFT.foreground(White);
     TFT.background(0);
     TFT.cls();
+    TFT.set_font((unsigned char*) Arial24x23);
     drawFrame();
 }
 
+int getGesture()
+{
+    point p;
+    int flag ,x ,y ,xx ,yy ,i;
+    flag = x = xx = y = yy = i = 0;
+    while( !flag ) {
+        p.x=0;
+        p.y=0;
+        if (TFT.getTouch(p)==TFT.YES) {
+            TFT.getTouch(p);            // read analog pos.
+            TFT.getPixel(p);            // convert to pixel pos
+            flag = 1;
+            x = p.x;
+            y = p.y;
+        }
+    }
+    while( ( TFT.getTouch(p)==TFT.YES ) || ( TFT.getTouch(p)==TFT.MAYBE ) ) {
+        i++;
+        TFT.getTouch(p);            // read analog pos.
+        TFT.getPixel(p);            // convert to pixel pos
+        xx = p.x;
+        yy = p.y;
+    }
+    if ( ( abs(xx - x) < 10 ) && ( abs(yy - y) < 10  ) && ( x < 25 ) ) {
+        return 0 ;
+    }
+    if ( i > 30 )  {
+        if ( ( ( yy - y ) > 0 ) && (abs(yy - y) > ( 5 * abs(xx-x) ) ) ) //To the RIGHT
+            return 1 ;
+        if ( ( ( y - yy ) > 0 ) && (abs(y - yy) > ( 5 * abs(xx-x) ) ) ) //To the LEFT
+            return 2 ;
+        if ( ( ( xx - x ) > 0 ) && (abs(xx - x) > ( 5 * abs(yy-y) ) ) ) //To the TOP
+            return 3 ;
+        if ( ( ( x - xx ) > 0 ) && (abs(x - xx) > ( 5 * abs(yy-y) ) ) ) //To the BOTTOM
+            return 4 ;
+    }
+    return 13;
+}
+
+bool TouchStatus()
+{
+    point p;
+    if ( ( TFT.getTouch(p)==TFT.YES ) || ( TFT.getTouch(p)==TFT.MAYBE ) )
+        return true;
+    return false;
+}
+
+Block doGest(Block NewBlock)
+{
+    int gest = getGesture();
+    if ( gest != 13 ) {
+        clrBlock(NewBlock);
+        switch ( gest ) {
+            case 0: {
+                while ( !NewBlock.CheckBottom() ) {
+                    NewBlock.y++;
+                }
+                saveToField(NewBlock);
+                drawFrame();
+                break;
+            }
+            case 1: {
+                NewBlock.moveRight();
+                break;
+            }
+            case 2: {
+                NewBlock.moveLeft();
+                break;
+            }
+            case 3: {
+                NewBlock.rotateLeft();
+                break;
+            }
+            case 4: {
+                NewBlock.rotateRight();
+                break;
+            }
+        }
+    }
+    return NewBlock;
+}
+
+void gameOver(int score)
+{
+    TFT.cls();
+    TFT.locate(40,160);
+    TFT.printf("Score : %i", score);
+}
\ No newline at end of file
--- a/playGround.h	Mon Feb 20 14:14:30 2017 +0000
+++ b/playGround.h	Sat Feb 25 23:52:28 2017 +0000
@@ -3,9 +3,14 @@
 #include "Block.h"
 
 void drawMap();
+void drawMapV2();
 void setColor( int y, int x, int color );
 void TFTInit();
 void drawBlock(Block NewBlock);
 void drawFrame();
 void clrBlock(Block NewBlock);
-void saveToField(Block NewBlock);
\ No newline at end of file
+void saveToField(Block NewBlock);
+int getGesture();
+bool TouchStatus();
+Block doGest(Block NewBlock);
+void gameOver(int score);
\ No newline at end of file