Linyan Shi
/
cn17lys
N5110
main.cpp@0:90b7f20b3a8d, 2020-04-28 (annotated)
- Committer:
- aileen
- Date:
- Tue Apr 28 01:39:30 2020 +0000
- Revision:
- 0:90b7f20b3a8d
N5110
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
aileen | 0:90b7f20b3a8d | 1 | ///////// pre-processor directives //////// |
aileen | 0:90b7f20b3a8d | 2 | #include "mbed.h" |
aileen | 0:90b7f20b3a8d | 3 | #include "Gamepad.h" |
aileen | 0:90b7f20b3a8d | 4 | #include "N5110.h" |
aileen | 0:90b7f20b3a8d | 5 | #include "PongEngine.h" |
aileen | 0:90b7f20b3a8d | 6 | |
aileen | 0:90b7f20b3a8d | 7 | #ifdef WITH_TESTING |
aileen | 0:90b7f20b3a8d | 8 | # include "tests.h" |
aileen | 0:90b7f20b3a8d | 9 | #endif |
aileen | 0:90b7f20b3a8d | 10 | |
aileen | 0:90b7f20b3a8d | 11 | #define PADDLE_WIDTH 2 |
aileen | 0:90b7f20b3a8d | 12 | #define PADDLE_HEIGHT 8 |
aileen | 0:90b7f20b3a8d | 13 | #define BALL_SIZE 2 |
aileen | 0:90b7f20b3a8d | 14 | #define BALL_SPEED 3 |
aileen | 0:90b7f20b3a8d | 15 | |
aileen | 0:90b7f20b3a8d | 16 | /////////////// structs ///////////////// |
aileen | 0:90b7f20b3a8d | 17 | struct UserInput { |
aileen | 0:90b7f20b3a8d | 18 | Direction d; |
aileen | 0:90b7f20b3a8d | 19 | float mag; |
aileen | 0:90b7f20b3a8d | 20 | }; |
aileen | 0:90b7f20b3a8d | 21 | /////////////// objects /////////////// |
aileen | 0:90b7f20b3a8d | 22 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
aileen | 0:90b7f20b3a8d | 23 | Gamepad pad; |
aileen | 0:90b7f20b3a8d | 24 | PongEngine pong; |
aileen | 0:90b7f20b3a8d | 25 | |
aileen | 0:90b7f20b3a8d | 26 | ///////////// prototypes /////////////// |
aileen | 0:90b7f20b3a8d | 27 | void init(); |
aileen | 0:90b7f20b3a8d | 28 | void update_game(UserInput input); |
aileen | 0:90b7f20b3a8d | 29 | void render(); |
aileen | 0:90b7f20b3a8d | 30 | void welcome(); |
aileen | 0:90b7f20b3a8d | 31 | |
aileen | 0:90b7f20b3a8d | 32 | ///////////// functions //////////////// |
aileen | 0:90b7f20b3a8d | 33 | int main() |
aileen | 0:90b7f20b3a8d | 34 | { |
aileen | 0:90b7f20b3a8d | 35 | #ifdef WITH_TESTING |
aileen | 0:90b7f20b3a8d | 36 | int number_of_failures = run_all_tests(); |
aileen | 0:90b7f20b3a8d | 37 | |
aileen | 0:90b7f20b3a8d | 38 | if(number_of_failures > 0) return number_of_failures; |
aileen | 0:90b7f20b3a8d | 39 | #endif |
aileen | 0:90b7f20b3a8d | 40 | |
aileen | 0:90b7f20b3a8d | 41 | int fps = 8; // frames per second |
aileen | 0:90b7f20b3a8d | 42 | |
aileen | 0:90b7f20b3a8d | 43 | init(); // initialise and then display welcome screen... |
aileen | 0:90b7f20b3a8d | 44 | welcome(); // waiting for the user to start |
aileen | 0:90b7f20b3a8d | 45 | |
aileen | 0:90b7f20b3a8d | 46 | render(); // first draw the initial frame |
aileen | 0:90b7f20b3a8d | 47 | wait(1.0f/fps); // and wait for one frame period |
aileen | 0:90b7f20b3a8d | 48 | |
aileen | 0:90b7f20b3a8d | 49 | |
aileen | 0:90b7f20b3a8d | 50 | // game loop - read input, update the game state and render the display |
aileen | 0:90b7f20b3a8d | 51 | while (1) { |
aileen | 0:90b7f20b3a8d | 52 | pong.read_input(pad); |
aileen | 0:90b7f20b3a8d | 53 | pong.update(pad); |
aileen | 0:90b7f20b3a8d | 54 | render(); |
aileen | 0:90b7f20b3a8d | 55 | wait(1.0f/fps); |
aileen | 0:90b7f20b3a8d | 56 | } |
aileen | 0:90b7f20b3a8d | 57 | } |
aileen | 0:90b7f20b3a8d | 58 | |
aileen | 0:90b7f20b3a8d | 59 | // initialies all classes and libraries |
aileen | 0:90b7f20b3a8d | 60 | void init() |
aileen | 0:90b7f20b3a8d | 61 | { |
aileen | 0:90b7f20b3a8d | 62 | // need to initialise LCD and Gamepad |
aileen | 0:90b7f20b3a8d | 63 | lcd.init(); |
aileen | 0:90b7f20b3a8d | 64 | pad.init(); |
aileen | 0:90b7f20b3a8d | 65 | |
aileen | 0:90b7f20b3a8d | 66 | // initialise the game with correct ball and paddle sizes |
aileen | 0:90b7f20b3a8d | 67 | pong.init(PADDLE_WIDTH,PADDLE_HEIGHT,BALL_SIZE,BALL_SPEED); |
aileen | 0:90b7f20b3a8d | 68 | |
aileen | 0:90b7f20b3a8d | 69 | } |
aileen | 0:90b7f20b3a8d | 70 | |
aileen | 0:90b7f20b3a8d | 71 | // this function draws each frame on the LCD |
aileen | 0:90b7f20b3a8d | 72 | void render() |
aileen | 0:90b7f20b3a8d | 73 | { |
aileen | 0:90b7f20b3a8d | 74 | // clear screen, re-draw and refresh |
aileen | 0:90b7f20b3a8d | 75 | lcd.clear(); |
aileen | 0:90b7f20b3a8d | 76 | pong.draw(lcd); |
aileen | 0:90b7f20b3a8d | 77 | lcd.refresh(); |
aileen | 0:90b7f20b3a8d | 78 | } |
aileen | 0:90b7f20b3a8d | 79 | |
aileen | 0:90b7f20b3a8d | 80 | // simple splash screen displayed on start-up |
aileen | 0:90b7f20b3a8d | 81 | void welcome() { |
aileen | 0:90b7f20b3a8d | 82 | |
aileen | 0:90b7f20b3a8d | 83 | lcd.printString(" Pong! ",0,1); |
aileen | 0:90b7f20b3a8d | 84 | lcd.printString(" Press Start ",0,4); |
aileen | 0:90b7f20b3a8d | 85 | lcd.refresh(); |
aileen | 0:90b7f20b3a8d | 86 | |
aileen | 0:90b7f20b3a8d | 87 | // wait flashing LEDs until start button is pressed |
aileen | 0:90b7f20b3a8d | 88 | while ( pad.check_event(Gamepad::START_PRESSED) == false) { |
aileen | 0:90b7f20b3a8d | 89 | pad.leds_on(); |
aileen | 0:90b7f20b3a8d | 90 | wait(0.1); |
aileen | 0:90b7f20b3a8d | 91 | pad.leds_off(); |
aileen | 0:90b7f20b3a8d | 92 | wait(0.1); |
aileen | 0:90b7f20b3a8d | 93 | } |
aileen | 0:90b7f20b3a8d | 94 | |
aileen | 0:90b7f20b3a8d | 95 | } |