Challenge number 1

Dependencies:   IoTChallenge1

Revision:
0:8680a1ea7711
Child:
1:a308877d2475
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Mon Feb 11 12:13:52 2019 +0000
@@ -0,0 +1,100 @@
+#include "MicroBit.h"
+ 
+int pLocationX;
+int pLocationY;
+int xPos = 2;
+bool gameOver = false;
+int y;
+int obstacleX;
+int score = 0;
+int playX;
+int direction = 0;
+MicroBit uBit;
+
+void getXPos(){
+    playX = uBit.accelerometer.getX();
+    if(playX > 250){
+        xPos++;
+        direction = 1;
+    }else if (playX < -250) {
+        xPos--;
+        direction = 0;
+    }
+    
+}
+    
+void checkCollision(){
+    if (xPos == obstacleX || xPos == obstacleX - 1 && y == 4){
+        gameOver = true;    
+    }else{
+            
+        gameOver = false;
+        }
+        
+}
+void displayPlayerLocation(){
+    getXPos();
+    if (xPos < 0){
+        xPos = 0;
+    } else if(xPos > 4) {
+        xPos = 4;
+    } 
+    if(direction == 1) {
+        uBit.display.image.setPixelValue(xPos - 1, 4, 0);
+    } else if(direction == 0) {
+        uBit.display.image.setPixelValue(xPos + 1, 4, 0);
+    }
+    
+    checkCollision();
+    uBit.display.image.setPixelValue(xPos, 4, 255);
+}
+void displayObs(){
+    uBit.display.image.setPixelValue(obstacleX, y, 0);
+    obstacleX = uBit.random(5);
+
+    for (y = 0; y  < 5; y++){ // descends the obstacles based on y value
+        uBit.display.image.setPixelValue(obstacleX, y, 255);
+        if (obstacleX > 0)
+        {
+            uBit.display.image.setPixelValue(obstacleX - 1, y, 255); 
+        }
+        if (y > 0)
+        {
+            uBit.display.image.setPixelValue(obstacleX, y - 1, 0);
+        }
+        if (obstacleX > 0 && y > 0)
+        {
+            uBit.display.image.setPixelValue(obstacleX - 1, y - 1, 0);
+        }
+        if (score > 0)
+        {
+            uBit.sleep(500 - score);
+        }else if (score >= 500)
+        {
+            uBit.sleep(0);
+        }else{
+            uBit.sleep(500);
+        }
+        checkCollision();
+        displayPlayerLocation();
+    }
+    y = 4;
+    uBit.display.image.setPixelValue(obstacleX, y, 0);
+    uBit.display.image.setPixelValue(obstacleX - 1, y, 0);    
+    score += 5;
+}
+
+
+
+
+int main(){
+    uBit.init();
+    while(gameOver == false){
+        displayObs();
+    }
+    uBit.display.scroll("Game Over, Score: ");
+    uBit.display.scroll(score);
+}
+
+
+