Wang Lin 201090174
Dependencies: mbed Gamepad N5110 FXOS8700Q
Diff: main.cpp
- Revision:
- 0:e1442f3aa3c7
- Child:
- 1:25a839625a1e
diff -r 000000000000 -r e1442f3aa3c7 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Wed Feb 08 19:51:44 2017 +0000 @@ -0,0 +1,83 @@ +///////// pre-processor directives //////// +#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; + float mag; +}; +/////////////// 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 = 3; + + while (1) { + UserInput input = read_input(); + update_game(input); + render(); + wait(1.0f/fps); + } +} + +void init() +{ + 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 +} + +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); + lcd.refresh(); +} \ No newline at end of file