Zhang Xin yu
/
ZhangXinyu201090208
zhangxinyu01text
main.cpp@9:6e8964c19459, 2018-04-16 (annotated)
- Committer:
- valavanisalex
- Date:
- Mon Apr 16 17:53:30 2018 +0000
- Revision:
- 9:6e8964c19459
- Parent:
- 8:d8e9de797e90
Added unit test example
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
eencae | 0:e1442f3aa3c7 | 1 | ///////// pre-processor directives //////// |
eencae | 0:e1442f3aa3c7 | 2 | #include "mbed.h" |
eencae | 0:e1442f3aa3c7 | 3 | #include "Gamepad.h" |
eencae | 0:e1442f3aa3c7 | 4 | #include "N5110.h" |
eencae | 0:e1442f3aa3c7 | 5 | #include "PongEngine.h" |
eencae | 0:e1442f3aa3c7 | 6 | |
valavanisalex | 9:6e8964c19459 | 7 | #ifdef WITH_TESTING |
valavanisalex | 9:6e8964c19459 | 8 | # include "tests.h" |
valavanisalex | 9:6e8964c19459 | 9 | #endif |
valavanisalex | 9:6e8964c19459 | 10 | |
eencae | 0:e1442f3aa3c7 | 11 | #define PADDLE_WIDTH 2 |
eencae | 0:e1442f3aa3c7 | 12 | #define PADDLE_HEIGHT 10 |
eencae | 0:e1442f3aa3c7 | 13 | #define BALL_RADIUS 2 |
eencae | 0:e1442f3aa3c7 | 14 | #define BALL_SPEED 3 |
eencae | 1:25a839625a1e | 15 | |
eencae | 0:e1442f3aa3c7 | 16 | /////////////// structs ///////////////// |
eencae | 0:e1442f3aa3c7 | 17 | struct UserInput { |
eencae | 0:e1442f3aa3c7 | 18 | Direction d; |
eencae | 0:e1442f3aa3c7 | 19 | float mag; |
eencae | 0:e1442f3aa3c7 | 20 | }; |
valavanisalex | 7:75ba0b323011 | 21 | |
valavanisalex | 7:75ba0b323011 | 22 | /////////////// Hardware objects /////////////// |
eencae | 0:e1442f3aa3c7 | 23 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
eencae | 0:e1442f3aa3c7 | 24 | Gamepad pad; |
valavanisalex | 7:75ba0b323011 | 25 | |
eencae | 0:e1442f3aa3c7 | 26 | ///////////// prototypes /////////////// |
eencae | 0:e1442f3aa3c7 | 27 | void init(); |
eencae | 0:e1442f3aa3c7 | 28 | void update_game(UserInput input); |
valavanisalex | 7:75ba0b323011 | 29 | void render(PongEngine &pong); |
valavanisalex | 7:75ba0b323011 | 30 | int get_random_number_from_pad(); |
valavanisalex | 7:75ba0b323011 | 31 | |
eencae | 0:e1442f3aa3c7 | 32 | ///////////// functions //////////////// |
eencae | 0:e1442f3aa3c7 | 33 | int main() |
eencae | 0:e1442f3aa3c7 | 34 | { |
valavanisalex | 9:6e8964c19459 | 35 | #ifdef WITH_TESTING |
valavanisalex | 9:6e8964c19459 | 36 | int n_failures = run_all_tests(); |
valavanisalex | 9:6e8964c19459 | 37 | |
valavanisalex | 9:6e8964c19459 | 38 | if(n_failures) return n_failures; |
valavanisalex | 9:6e8964c19459 | 39 | #endif |
valavanisalex | 9:6e8964c19459 | 40 | |
eencae | 1:25a839625a1e | 41 | int fps = 8; |
eencae | 0:e1442f3aa3c7 | 42 | |
valavanisalex | 7:75ba0b323011 | 43 | // Initialise hardware |
eencae | 1:25a839625a1e | 44 | init(); |
valavanisalex | 7:75ba0b323011 | 45 | |
valavanisalex | 7:75ba0b323011 | 46 | int seed = get_random_number_from_pad(); |
valavanisalex | 7:75ba0b323011 | 47 | |
valavanisalex | 7:75ba0b323011 | 48 | // Now create the game engine |
valavanisalex | 7:75ba0b323011 | 49 | PongEngine pong(PADDLE_WIDTH, |
valavanisalex | 7:75ba0b323011 | 50 | PADDLE_HEIGHT, |
valavanisalex | 7:75ba0b323011 | 51 | BALL_RADIUS, |
valavanisalex | 7:75ba0b323011 | 52 | BALL_SPEED, |
valavanisalex | 7:75ba0b323011 | 53 | seed); |
valavanisalex | 7:75ba0b323011 | 54 | |
valavanisalex | 7:75ba0b323011 | 55 | render(pong); // draw initial frame |
eencae | 1:25a839625a1e | 56 | wait(1.0f/fps); |
eencae | 0:e1442f3aa3c7 | 57 | |
eencae | 0:e1442f3aa3c7 | 58 | while (1) { |
eencae | 1:25a839625a1e | 59 | pong.read_input(pad); |
eencae | 1:25a839625a1e | 60 | pong.update(pad); |
valavanisalex | 7:75ba0b323011 | 61 | render(pong); |
eencae | 0:e1442f3aa3c7 | 62 | wait(1.0f/fps); |
eencae | 0:e1442f3aa3c7 | 63 | } |
eencae | 0:e1442f3aa3c7 | 64 | } |
eencae | 0:e1442f3aa3c7 | 65 | |
valavanisalex | 7:75ba0b323011 | 66 | /** |
valavanisalex | 7:75ba0b323011 | 67 | * @brief create some random number from ADC pins to use in srand |
valavanisalex | 7:75ba0b323011 | 68 | * @returns a random integer |
valavanisalex | 7:75ba0b323011 | 69 | */ |
valavanisalex | 7:75ba0b323011 | 70 | int get_random_number_from_pad() |
valavanisalex | 7:75ba0b323011 | 71 | { |
valavanisalex | 7:75ba0b323011 | 72 | float rand1 = pad.read_pot(); |
valavanisalex | 7:75ba0b323011 | 73 | Vector2D rand2 = pad.get_coord(); |
valavanisalex | 7:75ba0b323011 | 74 | int seed = int(1.0e6f*(rand1 + (rand2.x/rand2.y))); |
valavanisalex | 7:75ba0b323011 | 75 | |
valavanisalex | 7:75ba0b323011 | 76 | return seed; |
valavanisalex | 8:d8e9de797e90 | 77 | } |
valavanisalex | 7:75ba0b323011 | 78 | |
eencae | 0:e1442f3aa3c7 | 79 | void init() |
eencae | 0:e1442f3aa3c7 | 80 | { |
eencae | 0:e1442f3aa3c7 | 81 | lcd.init(); |
eencae | 0:e1442f3aa3c7 | 82 | pad.init(); |
eencae | 0:e1442f3aa3c7 | 83 | } |
eencae | 0:e1442f3aa3c7 | 84 | |
valavanisalex | 7:75ba0b323011 | 85 | void render(PongEngine &pong) |
eencae | 0:e1442f3aa3c7 | 86 | { |
eencae | 0:e1442f3aa3c7 | 87 | lcd.clear(); // clear screen, re-draw and refresh |
eencae | 1:25a839625a1e | 88 | pong.draw(lcd); |
eencae | 0:e1442f3aa3c7 | 89 | lcd.refresh(); |
eencae | 0:e1442f3aa3c7 | 90 | } |