ELEC2645 Joseph Allison 200860349

Dependencies:   N5110 Tetris mbed

Revision:
1:2b9ba34131ca
Parent:
0:ce5c75c04dc2
Child:
2:61caca4dd3f0
--- a/main.cpp	Sun Mar 27 13:27:59 2016 +0000
+++ b/main.cpp	Thu Apr 07 10:38:32 2016 +0000
@@ -10,94 +10,13 @@
 
 easter - colliosn detection
        - piece placement
+       - scoring
+       - levels and game speed changing
 */
 
 
-#include "mbed.h"
-#include "N5110.h"
-#include "time.h"
 
-
-//         VCC,    SCE,   RST,   D/C,   MOSI,  SCLK,   LED
-N5110 lcd (PTE26 , PTA0 , PTC4 , PTD0 , PTD2 , PTD1 , PTC3);
-Serial pc(USBTX,USBRX);
-
-AnalogIn   ain(A0); //used for the noise to create random numbers
-/*
-InterruptIn left(PTA2);
-InterruptIn right(PTB23);
-InterruptIn spin(PTB9);//PTA1
-DigitalIn down(PTC2);//PTC2
-*/
-InterruptIn left(PTC0);
-InterruptIn right(PTC9);
-InterruptIn spin(PTC5);
-InterruptIn down(PTC7);
-
-int score = 0;
-int completedlines = 0;
-
-
-int pieceposition[2] = {4,0}; //set position of the current dropping piece
-int orientation = 0;
-int currentshape = 3;
-int nextpiece;
-int shapebagcounter = 0;
-
-int nextshape[7];//stores the next 7 shapes to be used - 7 bag system
-
-void gameSetup(); //performs instuctions nessary to start the game
-void gamePixel(int x,int y); //sets a pixel in the game area
-void clearGamePixel(int x, int y); //clears a pixel in the game area
-void clearGame(); //clears the game area
-void gameInfo(int score,int level,int next); //
-void piecePlace(int x,int y,int shape); //places a piece
-void pieceClear(int x,int y,int shape); //clears a spesific shape
-void newShapeBag();//randomly generates new bag of 7
-void movePieceRight();//moves the piece right
-void movePieceLeft();//moves the piece left
-void spinPiece(); //spins the piece clockwise
-void movePieceDown(); //movespiece
-bool movePossible(int xpos, int ypos, int orientation); //returns turn if piece placement is valid
-void newpiece();
-void checkCompleteLine();
-void setGameArea(); //sets the game area from the game area array
-void removeCompleteLines(int rowscomplete[4]);
-void setTicker(float time);
-
-Ticker ticker; //creates the object fo the ticker that moves the game downwards
-
-//creats interupt functions for the game inputs
-void moveDownTicker();
-void left_isr();
-void right_isr();
-void spin_isr();
-void down_isr();
-
-//interupt and ticker flags
-volatile int g_timer_flag = 0;
-volatile int g_left = 0;
-volatile int g_right = 0;
-volatile int g_spin = 0;
-volatile int g_down = 0;
-
-//misc game flags
-volatile int g_newpiece = 1;
-int harddropping = 0;
-
-/*
-int Ishape[4] = {0x00F0,0x4444,0x0F00,0x2222};
-int Jshape[4] = {0x0071,0x0226,0x0470,0x0322};
-int Lshape[4] = {0x0074,0x0622,0x0170,0x0223};
-int Oshape[4] = {0x0033,0x0033,0x0033,0x0033};
-int Tshape[4] = {0x0072,0x0262,0x0270,0x0232};
-int Sshape[4] = {0x0036,0x0462,0x0360,0x0231};
-int Zshape[4] = {0x0063,0x0264,0x0630,0x0132};
-*/
-
-//shapes I,J,L,O,T,S,Z all stored as Hex values
-int shapes[7][4] = {{0x00F0,0x4444,0x0F00,0x2222},{0x0071,0x0226,0x0470,0x0322},{0x0074,0x0622,0x0170,0x0223},{0x0033,0x0033,0x0033,0x0033},{0x0072,0x0262,0x0270,0x0232},{0x0036,0x0462,0x0360,0x0231},{0x0063,0x0264,0x0630,0x0132}};
-int gamearea[12][16]; //stores the current states of the game area and is used to check for collisions ***add sides
+#include "main.h"
 
 
 int main()
@@ -106,12 +25,13 @@
     pc.baud(115200);
     srand(floor(ain.read()*10000));
 
+    buzzer.write(0.5);
 
     left.mode(PullDown);
     right.mode(PullDown);
     spin.mode(PullDown);
     down.mode(PullDown);
-    
+
     lcd.init();
     gameSetup();
     newShapeBag();//create bag of shapes to use
@@ -119,12 +39,14 @@
     gameInfo(0,1,nextpiece);
 
 
-    left.fall(&left_isr);
-    right.fall(&right_isr);
-    spin.fall(&spin_isr);
-    down.fall(&down_isr);
+    left.rise(&left_isr);
+    right.rise(&right_isr);
+    spin.rise(&spin_isr);
+    down.rise(&down_isr);
 
-    setTicker(0.4);
+    //musicticker.attach(&music_isr,0.25); ***** stopped the music
+
+    setTicker(gamespeed);
 
     while (true) {
 
@@ -135,6 +57,8 @@
 
         if(g_right) {
             g_right = 0;
+            g_righttimeout = 0;
+            righttimeout.attach(&righttimeout_isr,0.15);
             if(movePossible(pieceposition[0]-1,pieceposition[1],orientation)) {
                 movePieceRight();
             }
@@ -142,6 +66,8 @@
 
         if(g_left) {
             g_left = 0;
+            g_lefttimeout = 0;
+            lefttimeout.attach(&lefttimeout_isr,0.15);
             if(movePossible(pieceposition[0]+1,pieceposition[1],orientation)) {
                 movePieceLeft();
             }
@@ -149,14 +75,6 @@
 
         if (g_timer_flag) {
             g_timer_flag = 0;
-            /*
-            can piece move down
-                -move piece down
-                -if can move down move piece down
-                -if can move down place piece
-                    -get next piece
-
-            */
 
             if(movePossible(pieceposition[0],pieceposition[1]+1,orientation)) { //returns false if peicne cant move down
 
@@ -164,35 +82,14 @@
 
             } else { //piece placed
 
-                //*******check full line*******
-
-                //adds the piece to the gamearea array
-                int count = 0;
-                for(int y = 0; y<4; y++) {
-                    for(int x = 0; x<4; x++) {
-                        int bit = shapes[currentshape][orientation] & (1<<count);
+                pieceToGameArray();
+                g_newpiece = 1;
 
-                        if (bit) {
-                            gamearea[pieceposition[0]+x+1][pieceposition[1]+y]=1;
-                        }
-                        count++;
-                    }
-                }
-
-                g_newpiece = 1;
-                /*
-                 for(int j = 0; j<16; j++) {
-                    for(int i = 0; i<12; i++) {
-                        pc.printf("%d ",gamearea[i][j]);
-                    }
-                    pc.printf("\n");
-                }
-                */
-                if(harddropping){
+                if(harddropping) {
                     harddropping = 0;
                     ticker.detach();
-                    setTicker(0.4);
-                    }
+                    setTicker(gamespeed);
+                }
                 checkCompleteLine();
                 setGameArea();
 
@@ -201,18 +98,34 @@
 
         if(g_spin) {
             g_spin = 0;
+            g_spintimeout = 0;
+            spintimeout.attach(&spintimeout_isr,0.15);
             //check if peice can spin
             if(movePossible(pieceposition[0],pieceposition[1],orientation+1)) {
                 spinPiece();
             }
         }
-        
-        if(g_down){
+
+        if(g_down) {
             g_down = 0;
+            g_downtimeout = 0;
+            downtimeout.attach(&downtimeout_isr,0.15);
             ticker.detach();
-            setTicker(0.01);
+            setTicker(0.001);
             harddropping = 1;
+        }
+
+        if(g_music) {
+            g_music = 0;
+            buzzer.period_us(musisnotes[musiccounter]);
+            musiccounter++;
+
+            if(musiccounter > 63) {
+                musiccounter = 0;
             }
+
+        }
+
         sleep();
 
     }
@@ -247,15 +160,15 @@
         }
     }
 }
-
-void gamePixel(int x, int y)
+//**
+void gamePixel(int x, int y)//***
 {
 
     lcd.drawRect(42+(3*x),2+(3*y),2,2,1);
     lcd.refresh();
 }
-
-void clearGamePixel(int x, int y)
+//**
+void clearGamePixel(int x, int y)//***
 {
 
     for(int i =0; i<3; i++) {
@@ -268,7 +181,7 @@
 
 }
 
-void clearGame()
+void clearGame() //**
 {
 
     for(int i = 0; i<30; i++) {
@@ -308,7 +221,7 @@
 
 }
 
-void piecePlace(int x,int y,int shape)
+void piecePlace(int x,int y,int shape)//***
 {
 
 
@@ -326,13 +239,12 @@
     }
 }
 
-void pieceClear(int x,int y,int shape)
+void pieceClear(int x,int y,int shape)//***
 {
     int count = 0;
     for(int i = 0; i<4; i++) {
         for(int j = 0; j<4; j++) {
 
-
             int bit = shape & (1<<count);
 
             if (bit) {
@@ -359,14 +271,10 @@
             count ++;
 
             if ((bit !=0) && (gamearea[xpos+x+1][ypos+y]!=0)) {
-                //pc.printf("collision \n");
                 return false;
             }
-
         }
-        //pc.printf("\n");
     }
-    //pc.printf("\n");
 
     return true;
 }
@@ -377,6 +285,9 @@
     pieceposition[0] = 4; //sets the position back to the top
     pieceposition[1] = 0;
 
+    //clearGame();
+    //setGameArea();
+
     currentshape = nextshape[shapebagcounter];
     piecePlace(pieceposition[0],pieceposition[1],currentshape);//places the new piece
 
@@ -389,7 +300,12 @@
         nextpiece = nextshape[shapebagcounter+1];
         shapebagcounter++; //incrimenst the counter
     }
-    int level = int(completedlines/10);
+    for(int j = 0; j<16; j++) {
+        for(int i = 0; i<12; i++) {
+            pc.printf("%d ",gamearea[i][j]);
+        }
+        pc.printf("\n");
+    }
     gameInfo(score,level,nextpiece);
 }
 
@@ -439,12 +355,12 @@
 
         for(int x = 1; x<11; x++) {
             if(gamearea[x][y] == 1) {
-                sum++;
+                sum++; //counts how many filled cells
             }
         }
-        if(sum == 10) {
+        if(sum == 10) { //if all 10 cells are full
             //pc.printf("%d complete \n",y);
-            rowscomplete[rowscompletecounter] = y;
+            rowscomplete[rowscompletecounter] = y; //sets it as a completed row
             rowscompletecounter++;
             rowtoremove = true;
         }
@@ -456,24 +372,33 @@
     }
     pc.printf("\n");
 
-    if(rowtoremove) {
+    if(rowtoremove) {//runs if a row is complete
+        int prevlevel = level;
+        level = int((completedlines/10)+1);
+
+        if (prevlevel != level) {
+            gamespeed = 0.8606*exp(-0.073*level);
+
+            ticker.detach();
+            setTicker(gamespeed);
+        }
         removeCompleteLines(rowscomplete);
     }
     completedlines += rowscompletecounter;
-    if(rowscompletecounter == 1){
+    if(rowscompletecounter == 1) {
         score += 100;
-    }else if(rowscompletecounter == 2){
+    } else if(rowscompletecounter == 2) {
         score += 250;
-    }else if(rowscompletecounter == 3){
+    } else if(rowscompletecounter == 3) {
         score += 500;
-    }else if(rowscompletecounter == 4){
+    } else if(rowscompletecounter == 4) {
         score += 1000;
     }
 }
 
 void removeCompleteLines(int rowscomplete[4])
 {
-    for(int i = 0; i<4; i++) {
+    for(int i = 0; i<4; i++) { //loops throught the max 4 lines that have been completed
         if(rowscomplete[i] >0) {
 
             for(int j = 1; j<11; j++) { //delete complete row
@@ -482,7 +407,7 @@
 
             for(int j = rowscomplete[i]; j>0; j--) { //runs through all the rows to copy, j starts at deleted row
                 for(int k = 1; k<11; k++) { //runs through all the numbers in the row
-                gamearea[k][j] = gamearea[k][j-1];
+                    gamearea[k][j] = gamearea[k][j-1];
                 }
             }
             for(int j = 1; j<11; j++) { //add zeros into the top line
@@ -492,6 +417,23 @@
         }
     }
 }
+
+void pieceToGameArray()
+{
+    int count = 0;
+    for(int y = 0; y<4; y++) {
+        for(int x = 0; x<4; x++) {
+            int bit = shapes[currentshape][orientation] & (1<<count);
+
+            if (bit) {
+                gamearea[pieceposition[0]+x+1][pieceposition[1]+y]=1;
+            }
+            count++;
+        }
+    }
+
+}
+
 void setGameArea()
 {
     clearGame();//clears the area
@@ -503,9 +445,11 @@
         }
     }
 }
-void setTicker(float time){
+
+void setTicker(float time)
+{
     ticker.attach(&moveDownTicker, time);
-    }
+}
 
 void moveDownTicker()
 {
@@ -513,20 +457,52 @@
 }
 void left_isr()
 {
-    g_left = 1;
+    if(g_lefttimeout) {
+        g_left = 1;
+    }
 }
 void right_isr()
 {
-    g_right = 1;
+    if(g_righttimeout) {
+        g_right = 1;
+    }
 }
 void spin_isr()
 {
-    g_spin = 1;
+    if(g_spintimeout) {
+        g_spin = 1;
+    }
+}
+
+void down_isr()
+{
+    if(g_downtimeout) {
+        g_down = 1;
+    }
+}
+
+void music_isr()
+{
+    g_music = 1;
 }
 
-void down_isr(){
-    g_down = 1;
-    }
+void lefttimeout_isr()
+{
+    g_lefttimeout = 1;
+}
+void righttimeout_isr()
+{
+    g_righttimeout = 1;
+}
+void spintimeout_isr()
+{
+    g_spintimeout = 1;
+}
+void downtimeout_isr()
+{
+    g_downtimeout = 1;
+}
+
 
 void movePieceRight()
 {
@@ -558,3 +534,16 @@
     piecePlace(pieceposition[0],pieceposition[1],shapes[currentshape][orientation]);
 }
 
+void movePiece(int x, int y,int spin){
+    pieceClear(pieceposition[0],pieceposition[1],shapes[currentshape][orientation]);
+    pieceposition[0]= pieceposition[0] - x;
+    pieceposition[1]= pieceposition[1] + y;
+    orientation = orientation + spin;
+    if (orientation > 3){
+        orientation = 0;
+        }
+    piecePlace(pieceposition[0],pieceposition[1],shapes[currentshape][orientation]);
+    }
+    
+    
+    
\ No newline at end of file