My ELEC2645 joystick project Tetris Game NAME: JIANWEI CHEN SID: 200879849

Dependencies:   N5110 SDFileSystem mbed

Revision:
2:f427089e2bfa
Parent:
1:2a758565f691
Child:
3:5494a0fb3a33
--- a/main.cpp	Mon Apr 25 11:01:19 2016 +0000
+++ b/main.cpp	Tue Apr 26 00:14:57 2016 +0000
@@ -23,11 +23,17 @@
 void right_collisionDetect();
 int right_collision_flag = 0;
 
-int left_boundary[6][6];
+void bottom_collisionDetect();
+int bottom_collision_flag = 0;
+
+void top_collisionDetect();
+int top_collision_flag = 0;
 
-void bottom_collisionDetect();
-int bottom_yMax = 47;
-int bottom_collision_flag = 0;
+void rotation_collisionDetect();
+int rotation_collision_flag = 0;
+
+void fastmove_bottom_collisionDetect();
+int fastmove_bottom_collision_flag = 0;
 
 struct Position {
     float x;
@@ -62,10 +68,10 @@
 // struct for Joystick
 typedef struct JoyStick Joystick;
 struct JoyStick {
-    float x;    // current x value
-    float x0;   // 'centred' x value
-    float y;    // current y value
-    float y0;   // 'centred' y value
+    double x;    // current x value
+    double x0;   // 'centred' x value
+    double y;    // current y value
+    double y0;   // 'centred' y value
     int button; // button state (assume pull-down used, so 1 = pressed, 0 = unpressed)
     DirectionName direction;  // current direction
 };
@@ -74,6 +80,15 @@
 void calibrateJoystick();
 void updateJoystick();
 
+void cancelLine();// if one line is filled, cancel it and add the score
+int score=0;
+
+void finishAnimation();
+
+int xOld;
+int typeCount = 0;
+int nextTypeCount;
+
 int main()
 {
     wait(2.0);  // short delay for power to settle
@@ -84,31 +99,56 @@
     game.attach(&game_isr,0.4);
     lcd.refresh();
 
-    int xOld;
+
     calibrateJoystick();
 
-    int typeCount = 10;
-    int rotationCount = 5;
 
     while (1) {
 
         if(g_game_flag==1) {
             g_game_flag = 0;
             pos.type = typeArray[typeCount];
-            pos.rotation = rotationArray[rotationCount];
-            if (pos.y >= -6) { // clear previous pattern
+            nextTypeCount=typeCount+1;
+            if (nextTypeCount>99) {
+                nextTypeCount=0;
+            }
+            drawPattern(typeArray[nextTypeCount],0,55,41,1);// draw next pattern
+            if (pos.y >= -5) { // clear previous pattern
                 drawPattern(pos.type,pos.rotation,xOld,pos.y-1,0);
             }
             updateJoystick();
-
+            top_collisionDetect();
+            if (top_collision_flag == 1) {
+                finishAnimation();
+                lcd.clear();
+                char scoreBuffer[14];
+                sprintf(scoreBuffer,"%d",score);
+                lcd.printString("SCORE IS: ",10,2);
+                lcd.printString(scoreBuffer,35,3);
+                lcd.refresh();
+                break;
+            }
             switch(joystick.direction) {
                 case UP:
-                    pos.x +=2;
-                    drawPattern(pos.type,pos.rotation,pos.x,pos.y,1);
+                    rotation_collisionDetect();
+                    if(rotation_collision_flag == 0) {
+                        pos.rotation++;
+                        if (pos.rotation>3) {
+                            pos.rotation=0;
+                        }
+                        drawPattern(pos.type,pos.rotation,pos.x,pos.y,1);
+                    } else {// if collision
+                        drawPattern(pos.type,pos.rotation,pos.x,pos.y,1);
+                    }
                     break;
                 case DOWN:
-                    pos.y +=4;
-                    drawPattern(pos.type,pos.rotation,pos.x,pos.y,1);
+                    fastmove_bottom_collisionDetect();
+                    if (fastmove_bottom_collision_flag == 0) {
+                        pos.y +=4;
+                        drawPattern(pos.type,pos.rotation,pos.x,pos.y,1);
+                    } else {
+                        drawPattern(pos.type,pos.rotation,pos.x,pos.y,1);
+                    }
                     break;
                 case RIGHT:
                     right_collisionDetect();
@@ -133,10 +173,8 @@
                     if( left_collision_flag == 0) {
                         pos.x -=2;
                         drawPattern(pos.type,pos.rotation,pos.x,pos.y,1);
-                        //   lcd.printString("0",42,5);
                     } else {
                         drawPattern(pos.type,pos.rotation,pos.x,pos.y,1);
-                        //  lcd.printString("1",42,5);
                     }
                     break;
                 case CENTRE:
@@ -148,13 +186,23 @@
             }
 
             xOld = pos.x;
-            if (pos.y<42) {
+            bottom_collisionDetect();
+            if (bottom_collision_flag == 0) {
                 pos.y++;
             } else {
-                pos.y=42;
+                drawPattern(pos.type,pos.rotation,pos.x,pos.y,1);// fix pattern
+                cancelLine();
+                pos.x = 10;
+                pos.y = -6;
+                pos.rotation=0;
+                drawPattern(typeArray[nextTypeCount],pos.rotation,55,41,0);// clear the old next pattern
+                typeCount ++;
+                if (typeCount >99) {
+                    typeCount = 0;
+                }
             }
+
         }
-
         lcd.refresh();
     }
 
@@ -164,17 +212,16 @@
 {
     pos.x = 10;
     pos.y = -6;
-
+    pos.rotation=0;
     for(int i=0; i<=99; i++) {
         typeArray[i] = rand()%7;
         rotationArray[i] = rand()%5;
     }
-
     lcd.drawLine(30,0,30,47,1);
     lcd.printString("Level:",42,0);
     lcd.printString("easy",42,1);
     lcd.printString("Score:",42,2);
-    lcd.printString("100",42,3);
+    lcd.printString("0",42,3);
     lcd.printString("Next:",42,4);
 
 }
@@ -210,11 +257,11 @@
             if (j>=0) {
                 if(pattern_buffer[i-x][j-y]==1) {
                     if (fill==0) {
-                        if (j<=47 && i>=0 && i <= 29) {
+                        if (j<=47 && i>=0) {
                             lcd.clearPixel(i,j);
                         }
                     } else if (fill==1) {
-                        if (j<=47 && i>=0 && i <= 29) {
+                        if (j<=47 && i>=0) {
                             lcd.setPixel(i,j);
                         }
                     }
@@ -226,9 +273,9 @@
 
 void left_collisionDetect()
 {
-    scan();
+    int left_boundary[6][6];
     get_pattern(pos.type,pos.rotation);
-    // int left_boundary[6][6];
+
     // get the left boundary pattern
     for (int j=0; j<=5; j++) { //
         for (int i=0; i<=5; i++) {
@@ -256,16 +303,16 @@
                     if(i == 0) {
                         left_collision_flag = 1;
                         break;
-                    } else if (buffer[i-1][j]) {
+                    } else if (lcd.getPixel(i-1,j)) {
                         left_collision_flag = 1;
                         break;
                     } else {
                         left_collision_flag = 0;
                     }
                 }
-                if (left_collision_flag == 1) {
-                    break;
-                }
+            }
+            if (left_collision_flag == 1) {
+                break;
             }
         }
     }
@@ -274,7 +321,6 @@
 
 void right_collisionDetect()
 {
-    scan();
     int right_boundary[6][6];
     get_pattern(pos.type,pos.rotation);
     // get the left boundary pattern
@@ -302,7 +348,7 @@
                     if(i >= 29) {
                         right_collision_flag = 1;
                         break;
-                    } else if (buffer[i+1][j] == 1) {
+                    } else if (lcd.getPixel(i+1,j)) {
                         right_collision_flag = 1;
                         break;
                     } else {
@@ -310,9 +356,9 @@
                     }
                 }
             }
-            if (right_collision_flag == 1) {
-                break;
-            }
+        }
+        if (right_collision_flag == 1) {
+            break;
         }
 
     }
@@ -320,17 +366,17 @@
 
 void bottom_collisionDetect()
 {
-    scan();
+    int bot_boundary[6][6];
     get_pattern(pos.type,pos.rotation);
-    int bot_boundary[6][6];
     // get the left boundary pattern
     for (int i=0; i<=5; i++) {
         for (int j=5; j>=0; j--) {
             if (pattern_buffer[i][j]==1) {
                 bot_boundary[i][j]=1;
-                for(int k=i-1; k>=0; k--) {
+                for(int k=j-1; k>=0; k--) {
                     bot_boundary[i][k] = 0;
                 }
+                break;
             } else {
                 bot_boundary[i][j]=0;
             }
@@ -341,22 +387,114 @@
     int x = pos.x;
     int y = pos.y;
     for(int i = x; i <= x+5; i++) { // (x,y) is the left top point of a 6*6 square
+        for(int j=y+5; j >= y; j--) {
+            if (j>=-1) {
+                if(bot_boundary[i-x][j-y]==1) {
+                    if(j >= 47) {
+                        bottom_collision_flag = 1;
+                        break;
+                    } else if (lcd.getPixel(i,j+1)) {
+                        bottom_collision_flag = 1;
+                        break;
+                    } else {
+                        bottom_collision_flag = 0;
+                    }
+                }
+            } else {
+                bottom_collision_flag = 0;
+            }
+        }
+        if( bottom_collision_flag == 1) {
+            break;
+        }
+    }
+}
+
+void rotation_collisionDetect()
+{
+    int rotation = pos.rotation+1;
+    if (rotation>3) {
+        rotation=0;
+    }
+    get_pattern(pos.type,rotation);
+
+    //check
+    int x = pos.x;
+    int y = pos.y;
+    for(int i = x; i <= x+5; i++) { // (x,y) is the left top point of a 6*6 square
         for(int j=y; j <= y+5; j++) {
-            if(bot_boundary[i-x][j-y]==1) {
-                if(j == 47) {
-                    bottom_collision_flag = 1;
-                    bottom_yMax = 47;
-                } else if (buffer[i][j+1] == 1) {
-                    right_collision_flag = 1;
-                    if(j< bottom_yMax) {
-                        bottom_yMax = j;
-                    }
+            if(pattern_buffer[i-x][j-y]==1) {
+                if(i<0) {
+                    rotation_collision_flag = 1;
+                } else if(lcd.getPixel(i,j)) {
+                    rotation_collision_flag = 1;
+                    break;
                 } else {
-                    right_collision_flag = 0;
+                    rotation_collision_flag = 0;
                 }
             }
         }
+        if (rotation_collision_flag == 1) {
+            break;
+        }
+    }
+}
 
+void top_collisionDetect()
+{
+    if (pos.y==-6) {
+        bottom_collisionDetect();
+        if (bottom_collision_flag == 1) {
+            top_collision_flag = 1;
+        } else {
+            top_collision_flag = 0;
+        }
+    }
+}
+
+void fastmove_bottom_collisionDetect()
+{
+    int bot_boundary[6][6];
+    get_pattern(pos.type,pos.rotation);
+    // get the left boundary pattern
+    for (int i=0; i<=5; i++) {
+        for (int j=5; j>=0; j--) {
+            if (pattern_buffer[i][j]==1) {
+                bot_boundary[i][j]=1;
+                for(int k=j-1; k>=0; k--) {
+                    bot_boundary[i][k] = 0;
+                }
+                break;
+            } else {
+                bot_boundary[i][j]=0;
+            }
+        }
+    }
+
+    //check left collision
+    int x = pos.x;
+    int y = pos.y;
+    for(int i = x; i <= x+5; i++) { // (x,y) is the left top point of a 6*6 square
+        for(int j=y+5; j >= y; j--) {
+            if (j>=-1) {
+                if(bot_boundary[i-x][j-y]==1) {
+                    if(j >= 47) {
+                        fastmove_bottom_collision_flag = 1;
+                        break;
+                    } else if (lcd.getPixel(i,j+4)) {
+                        fastmove_bottom_collision_flag = 1;
+                        break;
+                    } else {
+                        fastmove_bottom_collision_flag = 0;
+                    }
+                }
+            } else {
+                fastmove_bottom_collision_flag = 0;
+            }
+        }
+        if( fastmove_bottom_collision_flag == 1) {
+            break;
+        }
     }
 }
 
@@ -398,4 +536,51 @@
         joystick.direction = UNKNOWN;
     }
 
-}
\ No newline at end of file
+}
+
+void cancelLine()
+{
+    // int linePattern[30][2]; //the pixel setting for one line(30x2 square)
+    int count;
+    for(int j=0; j<=46; j+=2) {
+        for(int i=0; i<=29; i++) {
+            if (lcd.getPixel(i,j)==0||lcd.getPixel(i,j+1)==0) {
+                count=0;
+                break;
+            } else if (lcd.getPixel(i,j)&&lcd.getPixel(i,j+1)) {
+                count++;
+            }
+        }
+        if(count==30) { // one line is filled
+            count=0;
+            lcd.drawRect(0,j,29,1,2); //clear the line
+            score+=10;  // add the score
+            //print the score
+            char scoreBuffer[14];
+            sprintf(scoreBuffer,"%d",score);
+            lcd.printString(scoreBuffer,42,3);
+            scan();
+            // move the patterns upon the line down for 2 pixels' hight
+            for (int x=0; x<=29; x++) {
+                for (int y=j; y>=0; y--) {
+                    if (buffer[x][y]) {
+                        lcd.clearPixel(x,y);
+                        lcd.setPixel(x,y+2);
+                    }
+                }
+            }
+        }
+    }
+}
+
+void finishAnimation()
+{
+    for (int j=47; j>=0; j--) {
+        lcd.drawRect(0,j,29,1,1);
+        wait(0.05);
+        lcd.refresh();
+    }
+}
+
+
+