Liam McNamara / Game

Dependencies:   microbit

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "MicroBit.h"
00002  
00003 int pLocationX;
00004 int pLocationY;
00005 int xPos = 4;
00006 bool gameOver = false;
00007 int y = 0;
00008 int obstacleX = 0;
00009 int score = 0;
00010 int playX;
00011 
00012 MicroBit uBit;
00013 
00014 int getXPos(){
00015     playX = uBit.accelerometer.getX();
00016     if(playX > 0){
00017         xPos++;
00018     }else{
00019         xPos--;
00020     }
00021     
00022 }
00023     
00024 void checkCollision(){
00025     //if (xPos == obstacleX || xPos == obstacleX - 1 && y == 4){
00026       //  gameOver = true;    
00027     //}    
00028         gameOver = false;
00029         
00030 }
00031 void displayPlayerLocation(){
00032     getXPos();
00033     
00034     if( xPos <= 4)
00035     {
00036         uBit.display.image.setPixelValue(xPos - 1, 4, 0);
00037     }
00038     if(xPos >= 0) {
00039             uBit.display.image.setPixelValue(xPos + 1, 4, 0);
00040     }
00041     
00042     if (xPos < 0){
00043         xPos = 0;
00044     } else if(xPos > 4) {
00045         xPos = 4;
00046     }
00047     
00048     checkCollision();
00049     uBit.display.image.setPixelValue(xPos, 4, 255);
00050 }
00051 void displayObs(){
00052     obstacleX = uBit.random(4);
00053     for (y = 1; y  >= 4; y++){
00054         uBit.display.image.setPixelValue(obstacleX, y, 255);
00055         if (obstacleX > 0)
00056         {
00057             uBit.display.image.setPixelValue(obstacleX - 1, y, 255);
00058         }
00059         if (y > 0)
00060         {
00061             uBit.display.image.setPixelValue(obstacleX, y - 1, 0);
00062         }
00063         if (obstacleX > 0 && y > 0)
00064         {
00065             uBit.display.image.setPixelValue(obstacleX - 1, y - 1, 0);
00066         }
00067         if (score > 0)
00068         {
00069             uBit.sleep(500 - score);
00070         }else if (score >= 500)
00071         {
00072             uBit.sleep(0);
00073         }else{
00074             uBit.sleep(500);
00075         }
00076     }
00077         checkCollision();
00078         displayPlayerLocation();
00079         uBit.display.image.setPixelValue(obstacleX, y, 0);
00080         if (obstacleX > 0){
00081             uBit.display.image.setPixelValue(obstacleX - 1, y, 0);    
00082         }
00083     y = 0;
00084     score += 5;
00085 }
00086 
00087 
00088 
00089 
00090 int main(){
00091     uBit.init();
00092     while(gameOver == false){
00093         displayObs();
00094     }
00095     uBit.display.scroll("Gsme Over, Score: ");
00096     uBit.display.scroll(score);
00097 }
00098 
00099 
00100 
00101 
00102 /*
00103 void drawTheDot(){
00104     int pointX = uBit.random(4);
00105     int pointY = uBit.random(4);
00106     
00107     uBit.display.image.setPixelValue(pointX, pointY, 255);
00108   }
00109   
00110   int main() {
00111     uBit.init();
00112     
00113     //int pointX = uBit.random(4);
00114    // int pointY = uBit.random(4);
00115    // uBit.display.scroll("Test");
00116   //  uBit.display.image.setPixelValue(pointX, pointY, 255);
00117     drawTheDot();
00118     release_fiber();
00119    // uBit.display.image.setPixelValue(2,2, 255);
00120 }
00121 */