5-level Ball Game. Avoid hitting the lines by controlling the ball. See more info at: https://developer.mbed.org/users/shurjo_1234/notebook/lsm303d-3d-compass-and-accelerometer/

Dependencies:   4DGL-uLCD-SE mbed LSM303D_SPI

Dependents:   LSM303D_SPI

Revision:
0:684294feee71
Child:
1:bad5121a35c7
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Oct 21 16:49:53 2014 +0000
@@ -0,0 +1,153 @@
+#include "mbed.h"
+#include "LSM303D.h"
+ 
+SPI spi(p5, p6, p7); // mosi, miso, sclk
+
+LSM303D sensor1(spi,p15);
+LSM303D sensor2(spi,p16);
+LSM303D sensor3(spi,p17);
+LSM303D sensor4(spi,p18);
+
+#include "uLCD_4DGL.h"
+
+uLCD_4DGL uLCD(p9,p10,p11); // serial tx, serial rx, reset pin;
+ 
+int main() {
+    // Chip must be deselected
+    sensor1.initialize();
+    float vx_old = sensor1.magnitometer(2);
+    
+    float fx=63.0,fy=2.0,vx= 0.0,vy=0.4;
+    int x=63,y=2,radius=2;
+    uLCD.background_color(BLACK);
+    uLCD.cls();
+    
+    int pts[20];
+    for (int level=1; level<=5; level++)
+    {   
+        if (level == 1)
+        {
+            pts[0] = 30; pts[1] = 63;
+            pts[2] = 96; pts[3] = 63;            
+        }
+        else if (level == 2)
+        {
+            pts[0] = 0; pts[1] = 40;
+            pts[2] = 50; pts[3] = 40;
+            
+            pts[4] = 76; pts[5] = 80;
+            pts[6] = 126; pts[7] = 80;
+            
+        }        
+        else if (level == 3)
+        {
+            pts[0] = 0; pts[1] = 30;
+            pts[2] = 50; pts[3] = 30;
+            
+            pts[4] = 76; pts[5] = 60;
+            pts[6] = 126; pts[7] = 60;
+            
+            pts[8] = 76; pts[9] = 90;
+            pts[10] = 126; pts[11] = 90;
+            
+        }        
+        else if (level == 4)
+        {
+            pts[0] = 0; pts[1] = 28;
+            pts[2] = 50; pts[3] = 28;
+            
+            pts[4] = 76; pts[5] = 56;
+            pts[6] = 126; pts[7] = 56;
+            
+            pts[8] = 76; pts[9] = 84;
+            pts[10] = 126; pts[11] = 84;
+            
+            pts[12] = 76; pts[13] = 112;
+            pts[14] = 126; pts[15] = 112;            
+        }        
+        else 
+        {
+            pts[0] = 0; pts[1] = 20;
+            pts[2] = 50; pts[3] = 20;
+            
+            pts[4] = 76; pts[5] = 40;
+            pts[6] = 126; pts[7] = 40;
+            
+            pts[8] = 76; pts[9] = 60;
+            pts[10] = 126; pts[11] = 60;
+            
+            pts[12] = 76; pts[13] = 80;
+            pts[14] = 126; pts[15] = 80;
+            
+            pts[16] = 76; pts[17] = 100;
+            pts[18] = 126; pts[19] = 100;            
+        }               
+        
+        uLCD.cls();
+        for (int i=0; i<level; i++)
+        {      
+            uLCD.line(pts[4*i], pts[4*i+1], pts[4*i+2], pts[4*i+3], WHITE);
+        }
+      
+        y = 2;
+        fy = 2; 
+        
+        while (y<126)
+        {           
+            vx = (sensor1.magnitometer(2)-vx_old)/100;
+            //draw ball
+            uLCD.filled_circle(x, y, radius, RED);
+            //bounce off edge walls and slow down a bit?
+            
+            for (int i=0; i<level; i++)
+            {      
+                if (x>pts[4*i] && x<pts[4*i+2] && y>pts[4*i+1]-1 && y<pts[4*i+3]+1)
+                {
+                    uLCD.cls();
+                    uLCD.text_width(4); //4X size text
+                    uLCD.text_height(4);
+                    uLCD.color(RED);  
+                    uLCD.locate(0,0);
+                    uLCD.printf("GAME    OVER");
+                    return 0;                    
+                }   
+            }
+            
+            uLCD.filled_circle(x, y, radius, BLACK);
+            //move ball
+            fx=fx+vx;
+            fy=fy+vy;
+            x=(int)fx;
+            y=(int)fy;
+            if (x<0)
+            {
+                x += 126;
+                fx = x;
+            }
+            if (x>126)
+            {
+                x -= 126;
+                fx = x;
+            }
+            
+        }
+        uLCD.cls();
+        uLCD.text_width(4); //4X size text
+        uLCD.text_height(4);
+        uLCD.color(RED);
+        uLCD.locate(1,1);
+    
+        if (level <= 4)
+                uLCD.printf("%2D",level+1);
+        else
+        {
+                uLCD.locate(1,1);
+                uLCD.printf("YOU WIN");
+        }
+        wait(1);
+        
+    }
+       
+
+    
+}
\ No newline at end of file