Ahmed Hedait / Mbed 2 deprecated el16ah

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
ahmedhedait
Date:
Sat May 05 19:34:26 2018 +0000
Parent:
9:0f3c1b0d053f
Child:
11:80e9477d9330
Commit message:
I have created a for loop in which it helps me create easily a code that helps the ball not to go through it from both directions neither does it let the ball pass it from either bottom or the top.;

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Mon Apr 30 21:02:35 2018 +0000
+++ b/main.cpp	Sat May 05 19:34:26 2018 +0000
@@ -17,6 +17,10 @@
 
     float circy = 5;
     float circx = 5;
+    int a;
+    int b;
+    int c;
+    int d;
 
 
     while(1) {
@@ -28,25 +32,25 @@
         float mag = joystick.get_mag();
         float angle = joystick.get_angle();
 
-        Direction d = joystick.get_direction();
-        printf("direction %i\n", d);
+        Direction dir = joystick.get_direction();
+        //printf("direction %i\n", dir);
 
         int speed = 1;
 
-        if (d == N) {
+        if (dir == N) {
             circy -= speed;
-        } else if (d == S) {
+        } else if (dir == S) {
             circy += speed;
         }
 
-        if (d == W) {
+        if (dir == W) {
             circx -= speed;
-        } else if (d == E) {
+        } else if (dir == E) {
             circx += speed;
         }
 
 
-
+        // drawing maze
         lcd.printString("           scr",0,0);
         lcd.drawRect(0,0,65,48,FILL_TRANSPARENT);
         lcd.drawRect(10,0,1,39,FILL_BLACK); // first rectangle next to the ball
@@ -59,21 +63,18 @@
         lcd.drawRect(45,0,1,11,FILL_BLACK);
         lcd.drawRect(55,6,1,45,FILL_BLACK); // i finished drawing the very basic simple maze.
 
-        /*lcd.drawRect(10,10,15,2,FILL_BLACK);
-        lcd.drawRect(25,0,2,12,FILL_BLACK);
-        lcd.drawRect(35,8,2,6,FILL_BLACK);
-        lcd.drawRect(35,6,18,2,FILL_BLACK);*/
-
         lcd.setPixel(64,39,false);
         lcd.setPixel(64,40,false);
         lcd.setPixel(64,41,false);
         lcd.setPixel(64,42,false);
         lcd.setPixel(64,43,false);
-        lcd.setPixel(64,44,false);
+        lcd.setPixel(64,44,false); // i took reduced some pixels from the right bottom so that to open a small hole for the ball to pass through.
 
-        printf("X = %f Y = %f", circx, circy);
+
+        printf("X = %f Y = %f\n", circx, circy);
         lcd.drawCircle(circx,circy,2,FILL_BLACK);
 
+        // keep ball on screen
         if (circy < 3) {
             circy = 3;
         }
@@ -86,54 +87,139 @@
             circx = 3;
         }
 
+        //lcd.drawRect(10,0,1,39,FILL_BLACK);
+        //test 2
+        /*
+        if (
+            (circy >= 3) && //top
+            (circy <= 1 + 39) && //bottom
+            (circx >= 10 - 3) && //left
+            (circx <= 10 + 3)  //right
+        ) {
+            // if it has, fix position and reflect x velocity
+            //left
+            if (circx <= 10-2) {
+                if(circx >= 10 - 3) {
+                    circx = 10 - 3;
+                }
+            }
 
-        if (circy <= 27) {
-            if (circx > WIDTH) {
-                circx = WIDTH;
+            //right
+            if(circx >= 10 + 2) {
+                if(circx <= 10 + 3) {
+                    circx = 10 + 3;
+                }
             }
-        } else if (circx > 15) {
-            circx = 15;
+
+            if(circx >= 10 - 2 && circx <= 10 + 2) {
+                if(circy < 2 + 39) {
+                    circy = 2 + 39;
+                }
+            }
         }
 
-
-        if (circy > 40 & circy < 45) {
-            if (circx > WIDTH) {
-                circx = WIDTH;
-            }
-        } else if (circx > 7) {
-            circx = 7;
-        }                               // code for the 1st rectangle
-
+        */
+        //test 3
+        //lcd.drawRect(10,0,1,39,FILL_BLACK);
+        //lcd.drawRect(18,32,1,15,FILL_BLACK);
 
-
-        /*if ( 10 > circx | circx > 27) {
-            if (circy > HEIGHT) {
-                circy = HEIGHT;
-            }
-        } else if (circy < 14) {
-            circy = 14;
-        }                               // code for the 2nd rectangle
-
+        for (int i = 0; i <= 2; i++) {
+            printf(" Stage  %d\n", i);
+            if(i == 0) {
+                a = 10;
+                b = 0;
+                c = 1;
+                d = 39;
+            } else if (i == 1) {
+                a = 18;
+                b = 32;
+                c = 1;
+                d = 15;
+            } 
+            printf("A = %d B = %d\n", a, b);
+            if (
+                (circy >= b - 2) && //top
+                (circy <= 2 + b + d) && //bottom
+                (circx >= a - 2) && //left
+                (circx <= a + 2)  //right
+            ) {
+                printf("ahh");
+                //left
+                if (circx <= a - 2) {
+                    if(circx >= a - 3) {
+                        circx = a - 3;
+                    }
+                }
 
-
-        if (circy > 10 | circy < 6){
-            if (circx > WIDTH) {
-                circx = WIDTH;
+                //right
+                if(circx >= a + 2) {
+                    if(circx <= a + 3) {
+                        circx = a + 3;
+                    }
+                }
+                
+                //top
+                if(circy <= b - 2) {
+                    if(circy >= b - 3)
+                        circy = b - 3;
+                }
+                
+                //bottom
+                if(circy >= b + d) {
+                    if(circy <= 1 + b + d)
+                        (circy = 1 + b + d);
+                }
             }
-        } else if (circx < 29) {
-            circx = 29;
-        }                               // code for the 1st rectangle
-
+        }
+        //test 4
+        //lcd.drawRect(10,0,1,39,FILL_BLACK);
+        //lcd.drawRect(18,32,1,15,FILL_BLACK);
+        /*
+        a = 18;
+        b = 32;
+        c = 1;
+        d = 15;
+        if (
+            (circy >= b - 2) && //top
+            (circy <= 1 + b + d) && //bottom
+            (circx >= a - 2) && //left
+            (circx <= a + 2)  //right
+        ) {
+            //left
+            if (circx <= a - 2) {
+                if(circx >= a - 3) {
+                    circx = a - 3;
+                }
+            }
 
-        if (circy == 41) {
-            if (circx > WIDTH) {
-                circx = WIDTH;
+            //right
+            if(circx >= a + 2) {
+                if(circx <= a + 3) {
+                    circx = a + 3;
+                }
+            }
+
+            if(circy <= b - 2) {
+                if(circy >= b - 3)
+                    circy = b - 3;
             }
-        } else if (circx > 61) {
-            circx = 61;
-        }                */               // code for the ball to pass through the opening
+
+            if(circy >= 1 + b + d) {
+                if(circy <= 1 + b + d)
+                    (circy = 1 + b + d);
+            }
+        }*/
 
+        /*
+                if (circy == 41) {
+                    if (circx > WIDTH) {
+                        circx = WIDTH;
+                    }
+                } else if (circx > 61) {
+                    circx = 61; // code for the ball to pass through the opening
+                }
 
+        */
 
         if (circx > 63 & circy == 41) {
             lcd.printString("  EndGame ",3,2);