![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Final Submission. I have read and agreed with Statement of Academic Integrity.
Dependencies: mbed Gamepad FLAPPY_BIRD
main.cpp@0:bfd3317d7773, 2019-05-05 (annotated)
- Committer:
- 877963042
- Date:
- Sun May 05 14:57:07 2019 +0000
- Revision:
- 0:bfd3317d7773
Final Submission. I have read and agreed with Statement of Academic Integrity.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
877963042 | 0:bfd3317d7773 | 1 | #include "Bird.h" |
877963042 | 0:bfd3317d7773 | 2 | |
877963042 | 0:bfd3317d7773 | 3 | |
877963042 | 0:bfd3317d7773 | 4 | |
877963042 | 0:bfd3317d7773 | 5 | |
877963042 | 0:bfd3317d7773 | 6 | // Objects |
877963042 | 0:bfd3317d7773 | 7 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
877963042 | 0:bfd3317d7773 | 8 | Gamepad pad; |
877963042 | 0:bfd3317d7773 | 9 | Bird bird; |
877963042 | 0:bfd3317d7773 | 10 | |
877963042 | 0:bfd3317d7773 | 11 | // Prototypes |
877963042 | 0:bfd3317d7773 | 12 | void init(); |
877963042 | 0:bfd3317d7773 | 13 | void welcome(); |
877963042 | 0:bfd3317d7773 | 14 | void mode1(); |
877963042 | 0:bfd3317d7773 | 15 | void start(int fps); |
877963042 | 0:bfd3317d7773 | 16 | void play(int fps); |
877963042 | 0:bfd3317d7773 | 17 | void failure(); |
877963042 | 0:bfd3317d7773 | 18 | |
877963042 | 0:bfd3317d7773 | 19 | |
877963042 | 0:bfd3317d7773 | 20 | |
877963042 | 0:bfd3317d7773 | 21 | int main() |
877963042 | 0:bfd3317d7773 | 22 | { |
877963042 | 0:bfd3317d7773 | 23 | // set the frame per second |
877963042 | 0:bfd3317d7773 | 24 | int fps = 5; |
877963042 | 0:bfd3317d7773 | 25 | // first need to initialise the display |
877963042 | 0:bfd3317d7773 | 26 | init(); |
877963042 | 0:bfd3317d7773 | 27 | // these are default settings so not strictly needed |
877963042 | 0:bfd3317d7773 | 28 | lcd.normalMode(); // normal colour mode |
877963042 | 0:bfd3317d7773 | 29 | lcd.setBrightness(0.6); // put LED backlight on 60% |
877963042 | 0:bfd3317d7773 | 30 | // change set contrast in range 0.0 to 1.0 |
877963042 | 0:bfd3317d7773 | 31 | // 0.5 appears to be a good starting point |
877963042 | 0:bfd3317d7773 | 32 | lcd.setContrast(0.5); |
877963042 | 0:bfd3317d7773 | 33 | lcd.clear(); |
877963042 | 0:bfd3317d7773 | 34 | |
877963042 | 0:bfd3317d7773 | 35 | // welcome screen that waiting the player to start |
877963042 | 0:bfd3317d7773 | 36 | welcome(); |
877963042 | 0:bfd3317d7773 | 37 | // Mode choosing, to determine the barrier speed |
877963042 | 0:bfd3317d7773 | 38 | mode1(); |
877963042 | 0:bfd3317d7773 | 39 | |
877963042 | 0:bfd3317d7773 | 40 | // game start, draw the bird and the barrier |
877963042 | 0:bfd3317d7773 | 41 | start(fps); |
877963042 | 0:bfd3317d7773 | 42 | |
877963042 | 0:bfd3317d7773 | 43 | while(1){ |
877963042 | 0:bfd3317d7773 | 44 | // playing procedure |
877963042 | 0:bfd3317d7773 | 45 | play(fps); |
877963042 | 0:bfd3317d7773 | 46 | |
877963042 | 0:bfd3317d7773 | 47 | // failure screen, to ask user to play again |
877963042 | 0:bfd3317d7773 | 48 | failure(); |
877963042 | 0:bfd3317d7773 | 49 | |
877963042 | 0:bfd3317d7773 | 50 | } |
877963042 | 0:bfd3317d7773 | 51 | |
877963042 | 0:bfd3317d7773 | 52 | |
877963042 | 0:bfd3317d7773 | 53 | } |
877963042 | 0:bfd3317d7773 | 54 | |
877963042 | 0:bfd3317d7773 | 55 | void init() |
877963042 | 0:bfd3317d7773 | 56 | { |
877963042 | 0:bfd3317d7773 | 57 | // initialise lcd and gamepad |
877963042 | 0:bfd3317d7773 | 58 | lcd.init(); |
877963042 | 0:bfd3317d7773 | 59 | pad.init(); |
877963042 | 0:bfd3317d7773 | 60 | bird.init(); |
877963042 | 0:bfd3317d7773 | 61 | } |
877963042 | 0:bfd3317d7773 | 62 | |
877963042 | 0:bfd3317d7773 | 63 | void welcome() |
877963042 | 0:bfd3317d7773 | 64 | { |
877963042 | 0:bfd3317d7773 | 65 | // print welcome screen |
877963042 | 0:bfd3317d7773 | 66 | bird.welcome(lcd); |
877963042 | 0:bfd3317d7773 | 67 | lcd.refresh(); |
877963042 | 0:bfd3317d7773 | 68 | |
877963042 | 0:bfd3317d7773 | 69 | // move to game while start button is pressed |
877963042 | 0:bfd3317d7773 | 70 | while ( pad.check_event(Gamepad::START_PRESSED) == false) { |
877963042 | 0:bfd3317d7773 | 71 | pad.leds_on(); |
877963042 | 0:bfd3317d7773 | 72 | wait(0.1); |
877963042 | 0:bfd3317d7773 | 73 | pad.leds_off(); |
877963042 | 0:bfd3317d7773 | 74 | wait(0.1); |
877963042 | 0:bfd3317d7773 | 75 | } |
877963042 | 0:bfd3317d7773 | 76 | } |
877963042 | 0:bfd3317d7773 | 77 | |
877963042 | 0:bfd3317d7773 | 78 | void mode1() |
877963042 | 0:bfd3317d7773 | 79 | { |
877963042 | 0:bfd3317d7773 | 80 | // choosing difficulty |
877963042 | 0:bfd3317d7773 | 81 | while (pad.check_event(Gamepad::A_PRESSED) == false) { |
877963042 | 0:bfd3317d7773 | 82 | bird.mode(lcd, pad); |
877963042 | 0:bfd3317d7773 | 83 | } |
877963042 | 0:bfd3317d7773 | 84 | } |
877963042 | 0:bfd3317d7773 | 85 | |
877963042 | 0:bfd3317d7773 | 86 | void start(int fps) |
877963042 | 0:bfd3317d7773 | 87 | { |
877963042 | 0:bfd3317d7773 | 88 | // draw the first bird and barrier, |
877963042 | 0:bfd3317d7773 | 89 | // and giving the last 3 seconds to prepare |
877963042 | 0:bfd3317d7773 | 90 | bird.ready(lcd); |
877963042 | 0:bfd3317d7773 | 91 | |
877963042 | 0:bfd3317d7773 | 92 | } |
877963042 | 0:bfd3317d7773 | 93 | |
877963042 | 0:bfd3317d7773 | 94 | void play(int fps) |
877963042 | 0:bfd3317d7773 | 95 | { |
877963042 | 0:bfd3317d7773 | 96 | lcd.clear(); |
877963042 | 0:bfd3317d7773 | 97 | if(pad.check_event(Gamepad::A_PRESSED)){ |
877963042 | 0:bfd3317d7773 | 98 | bird.flyup(lcd); |
877963042 | 0:bfd3317d7773 | 99 | } |
877963042 | 0:bfd3317d7773 | 100 | else {bird.flydown(lcd);} |
877963042 | 0:bfd3317d7773 | 101 | bird.barrier(lcd); |
877963042 | 0:bfd3317d7773 | 102 | bird.score(lcd); |
877963042 | 0:bfd3317d7773 | 103 | bird.pause(lcd,pad); |
877963042 | 0:bfd3317d7773 | 104 | lcd.refresh(); |
877963042 | 0:bfd3317d7773 | 105 | wait(1.0/fps); |
877963042 | 0:bfd3317d7773 | 106 | } |
877963042 | 0:bfd3317d7773 | 107 | |
877963042 | 0:bfd3317d7773 | 108 | void failure() |
877963042 | 0:bfd3317d7773 | 109 | { |
877963042 | 0:bfd3317d7773 | 110 | bird.fail(lcd, pad); |
877963042 | 0:bfd3317d7773 | 111 | } |