Ahmed Hedait / Mbed 2 deprecated el16ah

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
ahmedhedait
Date:
Mon May 07 19:41:45 2018 +0000
Parent:
14:a6a905f2c430
Child:
16:d120927b70ef
Commit message:
Added some comments to make it easier and more understandable .

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Mon May 07 18:49:57 2018 +0000
+++ b/main.cpp	Mon May 07 19:41:45 2018 +0000
@@ -13,37 +13,38 @@
 {
     // first need to initialise display
     joystick.init();
-
     lcd.init();
     lcd.setContrast(0.4);
     lcd.normalMode();      // normal colour mode
     lcd.setBrightness(0.5); // put LED backlight on 50%
+    
 
-
+    // INTIALISING THE STARTING POSITION OF THE BALL IN SCREEN AND THE LETTERS. 
     float circy = 5;
     float circx = 5;
     int a;
     int b;
     int c;
-    int d; 
+    int d;
 
 
 
 
     while(1) {
 
-        // these are default settings so not strictly needed
+        // I NEEDED TO MAKE SURE THE THE LCD SCREEN IS CLEAR IN WHICH I START MY CODING.
 
         lcd.clear();
 
 
-        float mag = joystick.get_mag();
-        float angle = joystick.get_angle();
-
+        /* SINCE MY GAME IS IN 2D, SO NO MORE THE SIMPLE DIRECTIONS IS NEEDED. THATS WHY THIS METHOD IS USED TO OBTIAN THE JOYSTICK DIRECTIONS IN TERMS OF 
+        THE COMPASS HEADINGS WHICH ARE NORTH, NORTH-EAST, EAST, SOUTH-EAST, SOUTH, SOUTH-WEST, WEST, NORTH-WEST. AS WELL AS, CENTRE IF THE JOY-STICK IS NOT MOVING. */
+ 
+        Direction dir = joystick.get_direction();
+        printf("direction %i\n", dir);
 
-        Direction dir = joystick.get_direction();
-        //printf("direction %i\n", dir);
-
+        // GIVE THE BALL A CERTAIN VELOCITY. 
+        
         int speed = 1;
 
         if (dir == N) {
@@ -59,10 +60,10 @@
         }
 
 
-        // drawing maze
-        //lcd.printString("           scr",0,0);
-        lcd.drawRect(0,0,84,48,FILL_TRANSPARENT); // orig ws 65
-        lcd.drawRect(10,0,1,39,FILL_BLACK); // first rectangle next to the ball
+        // I HAVE DRAWN THE WALLS OF THE MAZE AS RECTANGLES AND JOINED THEM UP IN WHICH TO CREATE THE WALLS OF THE MAZE.
+
+        lcd.drawRect(0,0,84,48,FILL_TRANSPARENT);
+        lcd.drawRect(10,0,1,39,FILL_BLACK);
         lcd.drawRect(18,32,1,15,FILL_BLACK);
         lcd.drawRect(18,25,18,1,FILL_BLACK);
         lcd.drawRect(36,25,1,25,FILL_BLACK);
@@ -70,19 +71,14 @@
         lcd.drawRect(18,18,27,1,FILL_BLACK);
         lcd.drawRect(18,10,27,1,FILL_BLACK);
         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(55,6,1,45,FILL_BLACK);
         lcd.drawRect(64,0,1,20,FILL_BLACK);
-         lcd.drawRect(64,27,1,13,FILL_BLACK);
-          lcd.drawRect(72,10,1,30,FILL_BLACK);
-          
-           lcd.drawRect(64,40,20,1,FILL_BLACK);
-        
-        
-        
-        
-        
-        
+        lcd.drawRect(64,27,1,13,FILL_BLACK);
+        lcd.drawRect(72,10,1,30,FILL_BLACK);
+        lcd.drawRect(64,40,20,1,FILL_BLACK);
+
+
+        // I REMOVED SOME PIXELS FROM THE RIGHT SIDE OF THE MAZE TO CREATE THE OPENING IN WHICH THE BALL SHOULD GO THROUGH.
 
         lcd.setPixel(83,24,false);
         lcd.setPixel(83,25,false);
@@ -90,13 +86,16 @@
         lcd.setPixel(83,27,false);
         lcd.setPixel(83,28,false);
         lcd.setPixel(83,29,false);
-        lcd.setPixel(83,30,false); // i took reduced some pixels from the right bottom so that to open a small hole for the ball to pass through.
+        lcd.setPixel(83,30,false);
 
 
-        //printf("X = %f Y = %f\n", circx, circy);
+       // VERY SIMPLE CODE IN WHCIH I DREW THE BALL OF THE MAZE. 
+       
         lcd.drawCircle(circx,circy,2,FILL_BLACK);
 
-        // keep ball on screen
+
+        // THIS CODE IS NEEDED TO MAKE SURE THAT THE BALL DOES NOT OFF THE DIMENSIONS OF THE LCD SCREEN. 
+        
         if (circy < 3) {
             circy = 3;
         }
@@ -108,18 +107,16 @@
         if (circx < 3) {
             circx = 3;
         }
-
-        //test 3
-        //lcd.drawRect(10,0,1,39,FILL_BLACK);
-        //lcd.drawRect(18,32,1,15,FILL_BLACK);
-        //lcd.drawRect(36,25,1,25,FILL_BLACK);
-        //lcd.drawRect(45,18,1,30,FILL_BLACK);
-        //lcd.drawRect(64,0,1,20,FILL_BLACK);
-         //lcd.drawRect(64,27,1,13,FILL_BLACK);
-          //lcd.drawRect(72,10,1,30,FILL_BLACK);
-
+        
+        /* THE FOLLOWING IS A FOR LOOP I CREATED IN WHICH IT WOULD BE MORE SYSTAMATIC AND EASIER FOR ME TO IMPLEMENT MY CODE WITH NO STRUGGLE.
+        I CREATED TO THOSE WHERE THEIR WIDTH IS JUST 1, BY OTHER WORDS, THE VERTICAL WALLS OF THE MAZE. DEPENDING ON THE POSITION OF THE BALL
+        ON SCREEN, THE LOOP WILL WORK AND TAKE IN THE NUMBERS TO SEE WHICH i NEEDED FOR THE GIVING POSTION. I ALSO GAVE FOR ALL THE 9 VERTICAL 
+        WALLS CONDITIONS THAT IF ITS MET, THE CODE SHOULD DO THE FOLLOWING ACTION. THE CONDITIONS WERE FROM i = 0 TILL i = 8 ( SINCE THERE IS 9 
+        VERTICAL RECTANGLES I DREW INSIDE THE MAZE ). THE CODE WILL THEN START BY THE FIRST i AND SEE IF THE BALL IS IN THE FOLLOWING DIMENSIONS
+        OF THE RECTANGLE, IF ITS NOT, IT WILL GO THE 2ND i AND SO ON. HOWEVER, IF ITS MET AND IT IS THE i OR RECTANGLE NEEDED, THEN THE CODE WILL 
+        SIMULTANEOUSLY WORK IN JUST MICRO-SECONDS, TELLING THE BALL THAT THERE IS A WALL THAT THE BALL CAN NOT PASS. */
+        
         for (int i = 0; i <= 8; i++) {
-            //printf(" Stage  %d\n", i);
 
             if(i == 0) {
                 a = 10;
@@ -161,21 +158,21 @@
                 c = 1;
                 d = 45;
             }
-            
+
             else if (i == 6) {
                 a = 64;
                 b = 0;
                 c = 1;
-                d = 20;              
+                d = 20;
             }
-            
+
             else if (i == 7) {
                 a = 64;
                 b = 27;
                 c = 1;
                 d = 13;
             }
-            
+
             else if (i == 8) {
                 a = 72;
                 b = 10;
@@ -217,12 +214,11 @@
                 }
             }
         }
-
+        
 
-//lcd.drawRect(18,25,18,1,FILL_BLACK);
-// lcd.drawRect(18,18,27,1,FILL_BLACK);
-// lcd.drawRect(18,10,27,1,FILL_BLACK);
-// lcd.drawRect(64,40,20,1,FILL_BLACK);
+        /* THIS IS YET ANOTHER FOR LOOP BUT FOR THE REMAINING HORIZENTAL LINES OF THE MAZE. IT WORKS EXACTLY THE SAME AS THE FIRST FOR LOOP
+         BUT IN WHICH I GAVE HERE FROM i = 0 TILL i = 3 ( SINCE THERE IS ONLY 4 HORIZENTAL LINES THIS TIME ). I JUST RE-ADJUSTED THE MATHS A 
+         LITTLE BIT SO THAT IT DETECTS THE RESTRICTION FROM UP AND DOWN RATHER LEFT OR RIGHT. OTHER THAN THAT, IT IS AS TYPICAL AS THE FIRST ONE. */ 
 
         for (int i = 0; i <= 3; i++) {
             //printf(" Stage  %d\n", i);
@@ -245,7 +241,7 @@
                 c = 27;
                 d = 1;
             }
-            
+
             else if (i == 3) {
                 a = 64;
                 b = 40;
@@ -280,7 +276,7 @@
                     if(circy >= b - 3)
                         circy = b - 3;
                 }
-                
+
 
                 //bottom
                 if(circy >= b ) {
@@ -291,24 +287,27 @@
         }
 
 
+        /* WHEN THE BALL REACHES THE Y-AXIS NEEDED WHICH IS 27, THEN THE JOYSTICK FREELY MOVE THE BALL RIGHT THROUGH THE OPENING OF THE SMAZE WALL,
+           HOWEVER, IF THE BALL IS NOT EQUAL TO THE Y-AXIS NEEDED, THEN THE BALL MUST BE RESTRICTED TO MOVING SO THAT IT DOES NOT PASS THE WALLS. */
 
         if (circy == 27) {
             if (circx > WIDTH) {
                 circx = WIDTH;
             }
         } else if (circx > 80) {
-            circx = 80; // code for the ball to pass through the opening
+            circx = 80;
         }
 
 
+        /* HERE IS A SIMPLE CODE THAT WHEN THE BALL PASS THROUGH THE OPENING THEN THE SCREEN SHOULD BE CLEARED IN WHICH BRAVO IS PRINTED TO
+            TELL THE USER THE GAME IS FINISHED. */
 
         if (circx > 83 & circy == 27) {
-            lcd.clear(); 
-            lcd.printString("  Bravo!  ",3,2);
-            
+            lcd.clear();
+            lcd.printString("  Bravo!  ",12,2);
         }
-        
-        wait(.05);
+
+        wait(.035);
         lcd.refresh();
     }
 }