Tetris

Dependencies:   microbit

Files at this revision

API Documentation at this revision

Comitter:
bvnoake
Date:
Mon Feb 11 12:28:09 2019 +0000
Parent:
0:9188cbdecda0
Commit message:
game1

Changed in this revision

Game.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/Game.cpp	Mon Feb 11 12:16:39 2019 +0000
+++ b/Game.cpp	Mon Feb 11 12:28:09 2019 +0000
@@ -1,253 +1,195 @@
 //Including the microbit library
 #include "MicroBit.h"
 
-//using vectors throughout code
-#include <vector>
-
-//defining points system
+//defining points for the game to set how long runs for 
 int points = 0;
-//defining co-ordinates used
-int obsx = 2;
-int obsx2;
+//defining co-ordinates for pixels
 int obsy = 0;
-int obsy2;
+int x = 4;
+int y = 4;
+//sets whether game is running or not
+bool gamerunning = true;
+//users lives before game ends
+int lives = 3;
+
+//
+MicroBit uBit;
+//creates random x co-ordinate
+int x2 = uBit.random(5);
 
-//creates vectors for the x and y coordinates
-vector <int> x;
-vector <int> y;
+//collision test function
+void pointscollide() 
+{
+    //if two x co-ordinates of the bottom pixels are the same 
+    if (x == x2)
+    {
+        /*if points is equal to 8 then this ends the game, 
+        sets game running to be false and scrolls across the screen game over 
+        you win and then their points */
+        if (points == 8)
+        {
+            gamerunning = false;
+            uBit.display.scroll("Game Over, you win");
+            uBit.display.scroll(points);
+        }
+        /* if points isn't 8 then points just adds by 1 then a new pixel is 
+        created in a new random location again */
+        else
+        {
+            points = points + 1;
+            x2 = uBit.random(5);
+            uBit.display.image.setPixelValue(x2, 4, 255);
+        }
+            
+    }
+    
+            
+    
+}
+//function for button pressing
+void onButton(MicroBitEvent e)
+{
+    //if button A is pressed
+    if (e.source == MICROBIT_ID_BUTTON_A) 
+    {
+        /* shifts over pixel one place to the left whilst 
+        getting rid of pixels original place */
+        uBit.display.image.setPixelValue(x = x - 1, y, 255);
+        if (x < 4)
+        {
+            uBit.display.image.setPixelValue(x + 1, y, 0);
+        }
+        //calls collide function to check whether collides with other pixel
+        pointscollide();
+        
+        
+    }
+    //if button B is pressed
+    if (e.source == MICROBIT_ID_BUTTON_B) 
+    {
+        /* shifts over pixel one place to the right whilst 
+        getting rid of pixels original place */
+        uBit.display.image.setPixelValue(x = x + 1, y, 255);
+        if (x > 0 )
+        {
+            uBit.display.image.setPixelValue(x - 1, y, 0);
+        }
+        //calls collide function to check whether collides with other pixel
+        pointscollide();
+        
+        
+    }
+}
 
 
-MicroBit uBit;
-//MicroBitImage game(5,5);
-
 
 
-//function for when buttons pressed
-void onButton(MicroBitEvent e)
-{   //if button a pressed
-    if (e.source == MICROBIT_ID_BUTTON_A) {
-        //game.shiftLeft(1);
-        
-        //goes through vector depending on size of x vector looking at values and moving over one space left
-        for (int i = 0; i < x.size(); i++)
-        {
-            uBit.display.image.setPixelValue(x[i]- 1,obsy, 211);
-            uBit.display.image.setPixelValue(x[i],obsy, 0);
-        }
-        //goes through vector depending on size of y vector looking at values and moving over one space right
-        for (int j = 0; j < y.size(); j++)
-        {
-            uBit.display.image.setPixelValue(obsx - 1,y[j], 255);
-            uBit.display.image.setPixelValue(obsx,y[j], 0);
-        }
-        
-        
-        
-        
-        /*
-        uBit.display.image.setPixelValue(obsx = obsx - 1,obsy, 211);
-         
-        
-        if (obsy > 0)
-        {
-            for (int i = 0; i < x.size(); i++)
-            {
-                uBit.display.image.setPixelValue(x[i] = x[i] - 1,obsy, 211);
-                
-                      
-            }
-            if (y.size() > 0)
-            {
-                uBit.display.image.setPixelValue(obsx = obsx - 1,obsy+ 1, 211);
-            }
-            
-        }
-                
-    
-
-        if (obsy > 0)
-        {
-            //uBit.display.image.setPixelValue(obsx, obsy - 1, 0);
-            
-            
-        
-            uBit.display.image.setPixelValue(obsx + 1, obsy, 0);
-        }
-                
-            
-        if (obsx > 0 && obsy > 0)
-        {
-            
-            
-            uBit.display.image.setPixelValue(obsx = obsx - 1, obsy = obsy - 1, 0);
-            uBit.display.image.setPixelValue(obsx = obsx - 1, obsy = obsy + 1, 0);
-            
-                  
-            
-        }
-        */
-        
-        
-           
-    }
-    if (e.source == MICROBIT_ID_BUTTON_B) {
-        uBit.display.scroll("right");
-           
-    }
-
-    if (e.source == MICROBIT_ID_BUTTON_AB) {
-        uBit.display.scroll("BOTH");
-        //Rotation
-    }
-}
-  
- 
-           
-//main game function
-void game_start()
+//running game function - for pixels moving down screen
+void play()
 {
-   //displays pixel in starting place
-   uBit.display.image.setPixelValue(obsx,obsy, 211);
-   //sets a number for how many pixels i want shape to have
-   for (int num = 0; num < 2; num++)
-   {
-        //generates random number so i can have a random shape
-        int option = uBit.random(3);
-        //goes through options either placing x coordinate left right or placing pixel underneath
-        if (option == 0)
-            //uBit.display.image.setPixelValue(obsx + 1,obsy, 211);
-            x.push_back(obsx + 1);
-            
-        if (option == 1)
-            //uBit.display.image.setPixelValue(obsx - 1,obsy, 211);
-            x.push_back(obsx - 1);
-            
-        if (option == 2)
-            //uBit.display.image.setPixelValue(obsx,obsy + 1, 211);
-            y.push_back(1);
-    } 
-    
-    
-               
-  
-    
-     /* loop for the size of the y axis on led 
+    //creating random x co-ordinate
+    int obsx = uBit.random(5);
+    /* loop for the size of the y axis on led 
     matrix adding one each time so that the pixel 
     can move down the screen one line at a time */
     for (obsy = 0; obsy  < 5; obsy++){
-         //uBit.display.image.paste(game);
-         uBit.display.image.setPixelValue(obsx,obsy, 211);
-         
-        // using vector goes through to find x values in order to move them down one palce line by line 
-        
+        /* sets pixels on the screen for each y co-ordinate as it
+        increases by 1, sets pixels with x co-ordinates and then 
+        deletes pixels above */
+        uBit.display.image.setPixelValue(obsx, obsy, 255);
         if (obsx > 0)
         {
-            for (int i = 0; i < x.size(); i++)
-            {
-                uBit.display.image.setPixelValue(x[i],obsy, 211);
-                
-                      
-            }
-            if (y.size() > 0)
-            {
-                uBit.display.image.setPixelValue(obsx,obsy - 1, 211);
-            }
-            
+            uBit.display.image.setPixelValue(obsx  - 1, obsy, 255);
         }
-                
-    
-            
-            
-            
-                
-                
-        
         if (obsy > 0)
         {
-            //uBit.display.image.setPixelValue(obsx, obsy - 1, 0);
-            
-            
-            if (y.size() > 0)
-            {
-                uBit.display.image.setPixelValue(obsx, obsy - 2, 0);
-            }
-            else
-            {
-                uBit.display.image.setPixelValue(obsx, obsy - 1, 0);
-            }
-                
-            
-            
-        
-            
-            
+            uBit.display.image.setPixelValue(obsx, obsy - 1, 0);
         }
         if (obsx > 0 && obsy > 0)
         {
+            uBit.display.image.setPixelValue(obsx - 1, obsy - 1, 0);
+        }
+        /* sets the speed depending on the points that 
+        the user holds to show the pixels going down each line */
+        if (points > 4)
+        {
+            uBit.sleep(300);
+        }
+        else if (points > 6)
+        {
+           uBit.sleep(200);
+        } 
+        else
+        {
+           uBit.sleep(600);
+        } 
             
-            if (y.size() > 0)
+            
+    }  
+        /*tests whether this pixel moving down the screen hits 
+        another so it doesn't delete the points receiving pixel  */
+        if (obsx == x2 || obsx - 1 == x2)
+        {   
+           uBit.display.image.setPixelValue(obsx, 4, 255); 
+        }
+        else
+        {
+            uBit.display.image.setPixelValue(obsx, 4, 0);
+        }
+        if (obsx < 4){
+            uBit.display.image.setPixelValue(obsx - 1, 4, 0);    
+        }
+        /* If the pixel hits the pixel manually moved by the 
+        buttons then lives will be decreased by 1 but if they're at 0
+        then game running set at false and displayed on screen that user 
+        loses and displays points */
+        if (obsx == x || obsx - 1 == x)
+        {
+            if (lives == 0)
             {
-               uBit.display.image.setPixelValue(obsx - 1, obsy - 2, 0);
-               uBit.display.image.setPixelValue(obsx + 1, obsy - 2, 0);
+                gamerunning = false;
+                uBit.display.scroll("Game over, you lose");
+                uBit.display.scroll(points);
             }
             else
             {
-               uBit.display.image.setPixelValue(obsx - 1, obsy - 1, 0);
-               uBit.display.image.setPixelValue(obsx + 1, obsy - 1, 0);
+                lives--;
             }
-            
-            
-            
-                
-            
-            
-                
-                
-            
-        }
-        //so that as the score gets higher the speed gets faster
-        if (points > 300)
-        {
-            uBit.sleep(400);
-        }else{
-           uBit.sleep(800);
         }
         
+    //sets obsy back to 0 so can go through the loop again setting back to start    
+    obsy = 0;
+    /* recalls the function again to go through the system 
+    again with pixel starting at top and moving down again */
+    play(); 
+}
+
+//Main function
+int main()
+{
+    // Initialise the micro:bit runtime.
+    uBit.init();
+    //sets two bottom pixels, one to be manually moved by buttons and one for the points system 
+    uBit.display.image.setPixelValue(x, y, 255);
+    uBit.display.image.setPixelValue(x2, 4, 255);
+    //To connect with the buttons so it's acknowledged when one is pressed
+    uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, onButton);
+    uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButton);
+    //plays game when game running set as true but if not displays game over
+    if (gamerunning == true)
+    {
+        play();
+    }
+    else if (gamerunning == false)
+    {
+       uBit.display.scroll("Game Over");
+       uBit.display.scroll(points);
         
     }
-    //clearing vectors so it can be random
-    x.clear();
-    y.clear();
-    //calls function again
-    game_start();
-    
-}
-
-int main()
-{
-   // Initialise the micro:bit runtime.
-   uBit.init();
-   //To connect with the buttons so it's acknowledged when one is pressed
-   uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, onButton);
-   uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButton);
-   uBit.messageBus.listen(MICROBIT_ID_BUTTON_AB, MICROBIT_BUTTON_EVT_CLICK, onButton);
-   
-   //so game can start
-   game_start();
-     
-        
-    
-        
-    
-    
-    
-    
-
     // If main exits, there may still be other fibers running or 
     // registered event handlers etc.
     // Simply release this fiber, which will mean we enter the 
     // scheduler. Worse case, we then
-    // sit in the idle task forever, in a power efficient sleep.
+    // sit in the idle task forever, in a power efficient sleep.    
     release_fiber();
-    
 }