Example Pong game for mbed.

Dependencies:   mbed

Revision:
7:75ba0b323011
Parent:
1:25a839625a1e
Child:
8:d8e9de797e90
--- a/main.cpp	Wed Mar 01 13:57:15 2017 +0000
+++ b/main.cpp	Sun Mar 05 16:45:35 2017 +0000
@@ -14,47 +14,65 @@
     Direction d;
     float mag;
 };
-/////////////// objects ///////////////
+
+/////////////// Hardware objects ///////////////
 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
 Gamepad pad;
-PongEngine pong;
+
 ///////////// prototypes ///////////////
 void init();
 void update_game(UserInput input);
-void render();
+void render(PongEngine &pong);
+int get_random_number_from_pad();
+
 ///////////// functions ////////////////
 int main()
 {
     int fps = 8;
 
+    // Initialise hardware
     init();
-    
-    render();  // draw initial frame 
+
+    int seed = get_random_number_from_pad();
+
+    // Now create the game engine
+    PongEngine pong(PADDLE_WIDTH,
+                    PADDLE_HEIGHT,
+                    BALL_RADIUS,
+                    BALL_SPEED,
+                    seed);
+
+    render(pong);  // draw initial frame
     wait(1.0f/fps);
 
     while (1) {
         pong.read_input(pad);
         pong.update(pad);
-        render();
+        render(pong);
         wait(1.0f/fps);
     }
 }
 
+/**
+ * @brief create some random number from ADC pins to use in srand
+ * @returns a random integer
+ */
+int get_random_number_from_pad()
+{
+    float rand1 = pad.read_pot();
+    Vector2D rand2 = pad.get_coord();
+    int seed = int(1.0e6f*(rand1 + (rand2.x/rand2.y)));
+
+    return seed;
+    }
+
 void init()
 {
     lcd.init();
     pad.init();
-    
-    float rand1 = pad.read_pot();
-    Vector2D rand2 = pad.get_coord();
-    int seed = int(1.0e6f*(rand1 + (rand2.x/rand2.y)));
-    // create some random number from ADC pins to use in srand
-    
-    pong.init(PADDLE_WIDTH,PADDLE_HEIGHT,BALL_RADIUS,BALL_SPEED,seed);
-
 }
 
-void render()
+void render(PongEngine &pong)
 {
     lcd.clear();  // clear screen, re-draw and refresh
     pong.draw(lcd);