Ahmed Hedait / Mbed 2 deprecated el16ah

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
ahmedhedait
Date:
Tue May 08 11:57:19 2018 +0000
Parent:
19:c6ebd1394bda
Child:
21:bcc84d5cb068
Commit message:
The ball is now capable of moving through the screen with the joystick.

Changed in this revision

Ball/Ball.cpp Show annotated file Show diff for this revision Revisions of this file
Ball/Ball.h Show annotated file Show diff for this revision Revisions of this file
MazeEngine/MazeEngine.cpp Show annotated file Show diff for this revision Revisions of this file
MazeEngine/MazeEngine.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/Ball/Ball.cpp	Tue May 08 11:32:13 2018 +0000
+++ b/Ball/Ball.cpp	Tue May 08 11:57:19 2018 +0000
@@ -9,4 +9,47 @@
 Ball::~Ball()
 {
 
+}
+
+void Ball::init()
+{
+    _circy = 5;
+    _circx = 5;
+    _speed = 1;
+
+}
+
+void Ball::draw(N5110 &lcd)
+{
+    // VERY SIMPLE CODE IN WHCIH I DREW THE BALL OF THE MAZE.
+    lcd.drawCircle(_circx,_circy,2,FILL_BLACK);
+}
+
+void Ball::update(Direction dir)
+{
+    if (dir == N) {
+        _circy -= _speed;
+    } else if (dir == S) {
+        _circy += _speed;
+    }
+
+    if (dir == W) {
+        _circx -= _speed;
+    } else if (dir == E) {
+        _circx += _speed;
+    }
+
+    // THIS CODE IS NEEDED TO MAKE SURE THAT THE BALL DOES NOT OFF THE DIMENSIONS OF THE LCD SCREEN.
+    if (_circy < 3) {
+        _circy = 3;
+    }
+
+    if (_circy > HEIGHT - 4) {
+        _circy = HEIGHT - 4;
+    }
+
+    if (_circx < 3) {
+        _circx = 3;
+    }
+
 }
\ No newline at end of file
--- a/Ball/Ball.h	Tue May 08 11:32:13 2018 +0000
+++ b/Ball/Ball.h	Tue May 08 11:57:19 2018 +0000
@@ -11,8 +11,15 @@
 
     Ball();
     ~Ball();
-    
+
+    void init();
+    void draw(N5110 &lcd);
+    void update(Direction dir);
+
 private:
 
+    int _circx;
+    int _circy;
+    int _speed;
 };
 #endif
\ No newline at end of file
--- a/MazeEngine/MazeEngine.cpp	Tue May 08 11:32:13 2018 +0000
+++ b/MazeEngine/MazeEngine.cpp	Tue May 08 11:57:19 2018 +0000
@@ -12,12 +12,17 @@
 
 void MazeEngine::init()
 {
-
+    _ball.init();
 }
 
 void MazeEngine::read_input(Gamepad &pad)
 {
-    _d = pad.get_direction();
+    _dir = pad.get_direction();
+}
+
+void MazeEngine::update(Gamepad &pad)
+{
+    _ball.update(_dir);
 }
 
 void MazeEngine::draw(N5110 &lcd)
@@ -25,4 +30,7 @@
     // draw the elements in the LCD buffer
     // maze
     _maze.draw(lcd);
+
+    // ball
+    _ball.draw(lcd);
 }
\ No newline at end of file
--- a/MazeEngine/MazeEngine.h	Tue May 08 11:32:13 2018 +0000
+++ b/MazeEngine/MazeEngine.h	Tue May 08 11:57:19 2018 +0000
@@ -17,11 +17,13 @@
     void init();
     void read_input(Gamepad &pad);
     void draw(N5110 &lcd);
+    void update(Gamepad &pad);
     
 private:
 
     Maze _maze;
-    Direction _d;
+    Direction _dir;
+    Ball _ball;
 
 };
 #endif
\ No newline at end of file
--- a/main.cpp	Tue May 08 11:32:13 2018 +0000
+++ b/main.cpp	Tue May 08 11:57:19 2018 +0000
@@ -49,7 +49,7 @@
     // game loop - read input, update the game state and render the display
     while (1) {
         maze.read_input(pad);
-        //maze.update(pad);
+        maze.update(pad);
         render();
         wait(.035);
     }
@@ -57,46 +57,10 @@
     /*
         while(1) {
 
-            lcd.clear();
-
-
             Direction dir = pad.get_direction();
             // printf("direction %i\n", dir);
 
-
-            // GIVE THE BALL A CERTAIN VELOCITY.
-            int speed = 1;
-
-            if (dir == N) {
-                circy -= speed;
-            } else if (dir == S) {
-                circy += speed;
-            }
-
-            if (dir == W) {
-                circx -= speed;
-            } else if (dir == E) {
-                circx += speed;
-            }
-
-
-            // VERY SIMPLE CODE IN WHCIH I DREW THE BALL OF THE MAZE.
-            lcd.drawCircle(circx,circy,2,FILL_BLACK);
-
-
-            // THIS CODE IS NEEDED TO MAKE SURE THAT THE BALL DOES NOT OFF THE DIMENSIONS OF THE LCD SCREEN.
-            if (circy < 3) {
-                circy = 3;
-            }
-
-            if (circy > HEIGHT - 4) {
-                circy = HEIGHT - 4;
-            }
-
-            if (circx < 3) {
-                circx = 3;
-            }
-
+          
             // FOR LOOP TO STOP BALL FROM GOING THROUGH WALLS
             for (int i = 0; i <= 12; i++) {
                 //printf(" Stage  %d\n", i);