![](/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
Bird/Bird.h@0:bfd3317d7773, 2019-05-05 (annotated)
- Committer:
- 877963042
- Date:
- Sun May 05 14:57:07 2019 +0000
- Revision:
- 0:bfd3317d7773
- Child:
- 2:6e82af30ae91
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 "mbed.h" |
877963042 | 0:bfd3317d7773 | 2 | #include "Gamepad.h" |
877963042 | 0:bfd3317d7773 | 3 | #include "N5110.h" |
877963042 | 0:bfd3317d7773 | 4 | |
877963042 | 0:bfd3317d7773 | 5 | |
877963042 | 0:bfd3317d7773 | 6 | // number of pixels on display |
877963042 | 0:bfd3317d7773 | 7 | #define WIDTH 84 |
877963042 | 0:bfd3317d7773 | 8 | #define HEIGHT 48 |
877963042 | 0:bfd3317d7773 | 9 | #define BANKS 6 |
877963042 | 0:bfd3317d7773 | 10 | |
877963042 | 0:bfd3317d7773 | 11 | /** Bird Class |
877963042 | 0:bfd3317d7773 | 12 | @brief Library for the function used in the Game |
877963042 | 0:bfd3317d7773 | 13 | @brief Under the foundation of the LCD(N5110) and Gamepad |
877963042 | 0:bfd3317d7773 | 14 | @brief Display welcome, mode choosing and playing screen |
877963042 | 0:bfd3317d7773 | 15 | @brief Refresh for the speed at 1.0/fps |
877963042 | 0:bfd3317d7773 | 16 | |
877963042 | 0:bfd3317d7773 | 17 | @brief Revision 1.0 |
877963042 | 0:bfd3317d7773 | 18 | |
877963042 | 0:bfd3317d7773 | 19 | @author Junyao. Yang |
877963042 | 0:bfd3317d7773 | 20 | @date 6th May 2019 |
877963042 | 0:bfd3317d7773 | 21 | |
877963042 | 0:bfd3317d7773 | 22 | @code |
877963042 | 0:bfd3317d7773 | 23 | #include "Bird.h" |
877963042 | 0:bfd3317d7773 | 24 | |
877963042 | 0:bfd3317d7773 | 25 | |
877963042 | 0:bfd3317d7773 | 26 | |
877963042 | 0:bfd3317d7773 | 27 | |
877963042 | 0:bfd3317d7773 | 28 | // Objects |
877963042 | 0:bfd3317d7773 | 29 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
877963042 | 0:bfd3317d7773 | 30 | Gamepad pad; |
877963042 | 0:bfd3317d7773 | 31 | Bird bird; |
877963042 | 0:bfd3317d7773 | 32 | |
877963042 | 0:bfd3317d7773 | 33 | // Prototypes |
877963042 | 0:bfd3317d7773 | 34 | void init(); |
877963042 | 0:bfd3317d7773 | 35 | void welcome(); |
877963042 | 0:bfd3317d7773 | 36 | void mode1(); |
877963042 | 0:bfd3317d7773 | 37 | void start(int fps); |
877963042 | 0:bfd3317d7773 | 38 | void play(int fps); |
877963042 | 0:bfd3317d7773 | 39 | void failure(); |
877963042 | 0:bfd3317d7773 | 40 | |
877963042 | 0:bfd3317d7773 | 41 | |
877963042 | 0:bfd3317d7773 | 42 | |
877963042 | 0:bfd3317d7773 | 43 | int main() |
877963042 | 0:bfd3317d7773 | 44 | { |
877963042 | 0:bfd3317d7773 | 45 | // set the frame per second |
877963042 | 0:bfd3317d7773 | 46 | int fps = 5; |
877963042 | 0:bfd3317d7773 | 47 | // first need to initialise the display |
877963042 | 0:bfd3317d7773 | 48 | init(); |
877963042 | 0:bfd3317d7773 | 49 | // these are default settings so not strictly needed |
877963042 | 0:bfd3317d7773 | 50 | lcd.normalMode(); // normal colour mode |
877963042 | 0:bfd3317d7773 | 51 | lcd.setBrightness(0.6); // put LED backlight on 60% |
877963042 | 0:bfd3317d7773 | 52 | // change set contrast in range 0.0 to 1.0 |
877963042 | 0:bfd3317d7773 | 53 | // 0.5 appears to be a good starting point |
877963042 | 0:bfd3317d7773 | 54 | lcd.setContrast(0.5); |
877963042 | 0:bfd3317d7773 | 55 | lcd.clear(); |
877963042 | 0:bfd3317d7773 | 56 | |
877963042 | 0:bfd3317d7773 | 57 | // welcome screen that waiting the player to start |
877963042 | 0:bfd3317d7773 | 58 | welcome(); |
877963042 | 0:bfd3317d7773 | 59 | // Mode choosing, to determine the barrier speed |
877963042 | 0:bfd3317d7773 | 60 | mode1(); |
877963042 | 0:bfd3317d7773 | 61 | |
877963042 | 0:bfd3317d7773 | 62 | // game start, draw the bird and the barrier |
877963042 | 0:bfd3317d7773 | 63 | start(fps); |
877963042 | 0:bfd3317d7773 | 64 | |
877963042 | 0:bfd3317d7773 | 65 | while(1){ |
877963042 | 0:bfd3317d7773 | 66 | // playing procedure |
877963042 | 0:bfd3317d7773 | 67 | play(fps); |
877963042 | 0:bfd3317d7773 | 68 | |
877963042 | 0:bfd3317d7773 | 69 | // failure screen, to ask user to play again |
877963042 | 0:bfd3317d7773 | 70 | failure(); |
877963042 | 0:bfd3317d7773 | 71 | |
877963042 | 0:bfd3317d7773 | 72 | } |
877963042 | 0:bfd3317d7773 | 73 | |
877963042 | 0:bfd3317d7773 | 74 | |
877963042 | 0:bfd3317d7773 | 75 | } |
877963042 | 0:bfd3317d7773 | 76 | |
877963042 | 0:bfd3317d7773 | 77 | void init() |
877963042 | 0:bfd3317d7773 | 78 | { |
877963042 | 0:bfd3317d7773 | 79 | // initialise lcd and gamepad |
877963042 | 0:bfd3317d7773 | 80 | lcd.init(); |
877963042 | 0:bfd3317d7773 | 81 | pad.init(); |
877963042 | 0:bfd3317d7773 | 82 | bird.init(); |
877963042 | 0:bfd3317d7773 | 83 | } |
877963042 | 0:bfd3317d7773 | 84 | |
877963042 | 0:bfd3317d7773 | 85 | void welcome() |
877963042 | 0:bfd3317d7773 | 86 | { |
877963042 | 0:bfd3317d7773 | 87 | // print welcome screen |
877963042 | 0:bfd3317d7773 | 88 | bird.welcome(lcd); |
877963042 | 0:bfd3317d7773 | 89 | lcd.refresh(); |
877963042 | 0:bfd3317d7773 | 90 | |
877963042 | 0:bfd3317d7773 | 91 | // move to game while start button is pressed |
877963042 | 0:bfd3317d7773 | 92 | while ( pad.check_event(Gamepad::START_PRESSED) == false) { |
877963042 | 0:bfd3317d7773 | 93 | pad.leds_on(); |
877963042 | 0:bfd3317d7773 | 94 | wait(0.1); |
877963042 | 0:bfd3317d7773 | 95 | pad.leds_off(); |
877963042 | 0:bfd3317d7773 | 96 | wait(0.1); |
877963042 | 0:bfd3317d7773 | 97 | } |
877963042 | 0:bfd3317d7773 | 98 | } |
877963042 | 0:bfd3317d7773 | 99 | |
877963042 | 0:bfd3317d7773 | 100 | void mode1() |
877963042 | 0:bfd3317d7773 | 101 | { |
877963042 | 0:bfd3317d7773 | 102 | // choosing difficulty |
877963042 | 0:bfd3317d7773 | 103 | while (pad.check_event(Gamepad::A_PRESSED) == false) { |
877963042 | 0:bfd3317d7773 | 104 | bird.mode(lcd, pad); |
877963042 | 0:bfd3317d7773 | 105 | } |
877963042 | 0:bfd3317d7773 | 106 | } |
877963042 | 0:bfd3317d7773 | 107 | |
877963042 | 0:bfd3317d7773 | 108 | void start(int fps) |
877963042 | 0:bfd3317d7773 | 109 | { |
877963042 | 0:bfd3317d7773 | 110 | // draw the first bird and barrier, |
877963042 | 0:bfd3317d7773 | 111 | // and giving the last 3 seconds to prepare |
877963042 | 0:bfd3317d7773 | 112 | bird.ready(lcd); |
877963042 | 0:bfd3317d7773 | 113 | |
877963042 | 0:bfd3317d7773 | 114 | } |
877963042 | 0:bfd3317d7773 | 115 | |
877963042 | 0:bfd3317d7773 | 116 | void play(int fps) |
877963042 | 0:bfd3317d7773 | 117 | { |
877963042 | 0:bfd3317d7773 | 118 | lcd.clear(); |
877963042 | 0:bfd3317d7773 | 119 | if(pad.check_event(Gamepad::A_PRESSED)){ |
877963042 | 0:bfd3317d7773 | 120 | bird.flyup(lcd); |
877963042 | 0:bfd3317d7773 | 121 | } |
877963042 | 0:bfd3317d7773 | 122 | else {bird.flydown(lcd);} |
877963042 | 0:bfd3317d7773 | 123 | bird.barrier(lcd); |
877963042 | 0:bfd3317d7773 | 124 | bird.score(lcd); |
877963042 | 0:bfd3317d7773 | 125 | bird.pause(lcd,pad); |
877963042 | 0:bfd3317d7773 | 126 | lcd.refresh(); |
877963042 | 0:bfd3317d7773 | 127 | wait(1.0/fps); |
877963042 | 0:bfd3317d7773 | 128 | } |
877963042 | 0:bfd3317d7773 | 129 | |
877963042 | 0:bfd3317d7773 | 130 | void failure() |
877963042 | 0:bfd3317d7773 | 131 | { |
877963042 | 0:bfd3317d7773 | 132 | bird.fail(lcd, pad); |
877963042 | 0:bfd3317d7773 | 133 | } |
877963042 | 0:bfd3317d7773 | 134 | |
877963042 | 0:bfd3317d7773 | 135 | |
877963042 | 0:bfd3317d7773 | 136 | @endcode |
877963042 | 0:bfd3317d7773 | 137 | */ |
877963042 | 0:bfd3317d7773 | 138 | |
877963042 | 0:bfd3317d7773 | 139 | class Bird |
877963042 | 0:bfd3317d7773 | 140 | { |
877963042 | 0:bfd3317d7773 | 141 | |
877963042 | 0:bfd3317d7773 | 142 | public: |
877963042 | 0:bfd3317d7773 | 143 | |
877963042 | 0:bfd3317d7773 | 144 | /** Initialize all the parameter |
877963042 | 0:bfd3317d7773 | 145 | * |
877963042 | 0:bfd3317d7773 | 146 | * initialise each time the procedure run |
877963042 | 0:bfd3317d7773 | 147 | */ |
877963042 | 0:bfd3317d7773 | 148 | void init(); |
877963042 | 0:bfd3317d7773 | 149 | |
877963042 | 0:bfd3317d7773 | 150 | /** Welcome |
877963042 | 0:bfd3317d7773 | 151 | * |
877963042 | 0:bfd3317d7773 | 152 | * Print welcome screen |
877963042 | 0:bfd3317d7773 | 153 | * wait until start button is pressed |
877963042 | 0:bfd3317d7773 | 154 | */ |
877963042 | 0:bfd3317d7773 | 155 | void welcome(N5110 &lcd); |
877963042 | 0:bfd3317d7773 | 156 | |
877963042 | 0:bfd3317d7773 | 157 | /** Difficulty choosing |
877963042 | 0:bfd3317d7773 | 158 | * |
877963042 | 0:bfd3317d7773 | 159 | * Choose difficulty |
877963042 | 0:bfd3317d7773 | 160 | * determine the speed of barrier vb |
877963042 | 0:bfd3317d7773 | 161 | */ |
877963042 | 0:bfd3317d7773 | 162 | void mode(N5110 &lcd, Gamepad &pad); |
877963042 | 0:bfd3317d7773 | 163 | |
877963042 | 0:bfd3317d7773 | 164 | /** Display score |
877963042 | 0:bfd3317d7773 | 165 | * |
877963042 | 0:bfd3317d7773 | 166 | * start at 0 |
877963042 | 0:bfd3317d7773 | 167 | * plus 1 each time pass the barrier |
877963042 | 0:bfd3317d7773 | 168 | */ |
877963042 | 0:bfd3317d7773 | 169 | void score(N5110 &lcd); |
877963042 | 0:bfd3317d7773 | 170 | |
877963042 | 0:bfd3317d7773 | 171 | /** Pause screen |
877963042 | 0:bfd3317d7773 | 172 | * |
877963042 | 0:bfd3317d7773 | 173 | * run if button back is pressed |
877963042 | 0:bfd3317d7773 | 174 | * return game if press again |
877963042 | 0:bfd3317d7773 | 175 | */ |
877963042 | 0:bfd3317d7773 | 176 | void pause(N5110 &lcd, Gamepad &pad); |
877963042 | 0:bfd3317d7773 | 177 | |
877963042 | 0:bfd3317d7773 | 178 | /** Game start structure |
877963042 | 0:bfd3317d7773 | 179 | * |
877963042 | 0:bfd3317d7773 | 180 | * draw out the basic structure |
877963042 | 0:bfd3317d7773 | 181 | */ |
877963042 | 0:bfd3317d7773 | 182 | void stru(N5110 &lcd); |
877963042 | 0:bfd3317d7773 | 183 | |
877963042 | 0:bfd3317d7773 | 184 | /** Random barrier |
877963042 | 0:bfd3317d7773 | 185 | * |
877963042 | 0:bfd3317d7773 | 186 | * random barrier produced |
877963042 | 0:bfd3317d7773 | 187 | * move in the speed be given |
877963042 | 0:bfd3317d7773 | 188 | */ |
877963042 | 0:bfd3317d7773 | 189 | void barrier(N5110 &lcd); |
877963042 | 0:bfd3317d7773 | 190 | |
877963042 | 0:bfd3317d7773 | 191 | /** Bird fly up |
877963042 | 0:bfd3317d7773 | 192 | * |
877963042 | 0:bfd3317d7773 | 193 | * run as button A is pressed |
877963042 | 0:bfd3317d7773 | 194 | * bird fly up 8 pixel each time |
877963042 | 0:bfd3317d7773 | 195 | */ |
877963042 | 0:bfd3317d7773 | 196 | void flyup(N5110 &lcd); |
877963042 | 0:bfd3317d7773 | 197 | |
877963042 | 0:bfd3317d7773 | 198 | /** Bird fly down |
877963042 | 0:bfd3317d7773 | 199 | * |
877963042 | 0:bfd3317d7773 | 200 | * run each time screen refresh |
877963042 | 0:bfd3317d7773 | 201 | * drop 2 pixel each time |
877963042 | 0:bfd3317d7773 | 202 | */ |
877963042 | 0:bfd3317d7773 | 203 | void flydown(N5110 &lcd); |
877963042 | 0:bfd3317d7773 | 204 | |
877963042 | 0:bfd3317d7773 | 205 | /** Random number |
877963042 | 0:bfd3317d7773 | 206 | * |
877963042 | 0:bfd3317d7773 | 207 | * random number between 1-4 |
877963042 | 0:bfd3317d7773 | 208 | * to determine different barrier type |
877963042 | 0:bfd3317d7773 | 209 | */ |
877963042 | 0:bfd3317d7773 | 210 | void check(); |
877963042 | 0:bfd3317d7773 | 211 | |
877963042 | 0:bfd3317d7773 | 212 | /** Failure screen |
877963042 | 0:bfd3317d7773 | 213 | * |
877963042 | 0:bfd3317d7773 | 214 | * show failure as collided |
877963042 | 0:bfd3317d7773 | 215 | * ask if play again |
877963042 | 0:bfd3317d7773 | 216 | */ |
877963042 | 0:bfd3317d7773 | 217 | void fail(N5110 &lcd, Gamepad &pad); |
877963042 | 0:bfd3317d7773 | 218 | |
877963042 | 0:bfd3317d7773 | 219 | /** Ready screen |
877963042 | 0:bfd3317d7773 | 220 | * |
877963042 | 0:bfd3317d7773 | 221 | * give three seconds to prepare |
877963042 | 0:bfd3317d7773 | 222 | * play game later |
877963042 | 0:bfd3317d7773 | 223 | */ |
877963042 | 0:bfd3317d7773 | 224 | void ready(N5110 &lcd); |
877963042 | 0:bfd3317d7773 | 225 | |
877963042 | 0:bfd3317d7773 | 226 | /** If collided |
877963042 | 0:bfd3317d7773 | 227 | * |
877963042 | 0:bfd3317d7773 | 228 | * return true if collided |
877963042 | 0:bfd3317d7773 | 229 | * return false if not |
877963042 | 0:bfd3317d7773 | 230 | */ |
877963042 | 0:bfd3317d7773 | 231 | bool coll(); |
877963042 | 0:bfd3317d7773 | 232 | |
877963042 | 0:bfd3317d7773 | 233 | |
877963042 | 0:bfd3317d7773 | 234 | private: |
877963042 | 0:bfd3317d7773 | 235 | |
877963042 | 0:bfd3317d7773 | 236 | int _vb; // mode, determine the barrier moving speed |
877963042 | 0:bfd3317d7773 | 237 | int _birdy; // parameter that determine y-axis of bird |
877963042 | 0:bfd3317d7773 | 238 | int _x1; |
877963042 | 0:bfd3317d7773 | 239 | int _x2; |
877963042 | 0:bfd3317d7773 | 240 | int _x3; |
877963042 | 0:bfd3317d7773 | 241 | int _x4; // x-axis coordinate of four barrier |
877963042 | 0:bfd3317d7773 | 242 | int _y1; |
877963042 | 0:bfd3317d7773 | 243 | int _y2; |
877963042 | 0:bfd3317d7773 | 244 | int _y3; |
877963042 | 0:bfd3317d7773 | 245 | int _y4; // y-axis coordinate of four barrie upper side |
877963042 | 0:bfd3317d7773 | 246 | int _birdh; // actual y coordinate of the bird |
877963042 | 0:bfd3317d7773 | 247 | int _score; // score display at the top-left corner while playing |
877963042 | 0:bfd3317d7773 | 248 | |
877963042 | 0:bfd3317d7773 | 249 | |
877963042 | 0:bfd3317d7773 | 250 | }; |