zhangxinyu01text

Dependencies:   mbed

Revision:
1:25a839625a1e
Parent:
0:e1442f3aa3c7
Child:
3:910d7e87f367
Child:
7:75ba0b323011
--- a/main.cpp	Wed Feb 08 19:51:44 2017 +0000
+++ b/main.cpp	Wed Mar 01 13:55:16 2017 +0000
@@ -2,16 +2,13 @@
 #include "mbed.h"
 #include "Gamepad.h"
 #include "N5110.h"
-#include "Paddle.h"
-#include "Ball.h"
 #include "PongEngine.h"
 
 #define PADDLE_WIDTH 2
 #define PADDLE_HEIGHT 10
-#define P1_X 1
-#define P2_X WIDTH - PADDLE_WIDTH - P1_X
 #define BALL_RADIUS 2
 #define BALL_SPEED 3
+
 /////////////// structs /////////////////
 struct UserInput {
     Direction d;
@@ -20,25 +17,24 @@
 /////////////// objects ///////////////
 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
 Gamepad pad;
-Paddle p1;
-Paddle p2;
-Ball ball;
 PongEngine pong;
 ///////////// prototypes ///////////////
 void init();
-UserInput read_input();
 void update_game(UserInput input);
 void render();
 ///////////// functions ////////////////
 int main()
 {
-    init();
+    int fps = 8;
 
-    int fps = 3;
+    init();
+    
+    render();  // draw initial frame 
+    wait(1.0f/fps);
 
     while (1) {
-        UserInput input = read_input();
-        update_game(input);
+        pong.read_input(pad);
+        pong.update(pad);
         render();
         wait(1.0f/fps);
     }
@@ -49,35 +45,18 @@
     lcd.init();
     pad.init();
     
-    p1.init(P1_X,PADDLE_HEIGHT,PADDLE_WIDTH);
-    p2.init(P2_X,PADDLE_HEIGHT,PADDLE_WIDTH);
-    ball.init(BALL_RADIUS,BALL_SPEED,pad);
-    pong.init(); // does nothing yet
-}
+    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);
 
-UserInput read_input()
-{
-    Direction d = pad.get_direction();
-    float mag = pad.get_mag();
-    UserInput input = {d,mag};
-    return input;
-}
-
-void update_game(UserInput input)
-{
-    // important to update paddles and ball before checking collisions so can
-    // correct for it before updating the display
-    p1.update(input.d,input.mag);
-    p2.update(input.d,input.mag);
-    ball.update();
-    pong.check_collisions(ball,p1,p2);
 }
 
 void render()
 {
     lcd.clear();  // clear screen, re-draw and refresh
-    p1.draw(lcd);
-    p2.draw(lcd);
-    ball.draw(lcd);
+    pong.draw(lcd);
     lcd.refresh();
 }
\ No newline at end of file