Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
main.cpp@15:7ca2d1b2bd0e, 2020-05-29 (annotated)
- Committer:
- ChenZirui
- Date:
- Fri May 29 07:35:45 2020 +0000
- Revision:
- 15:7ca2d1b2bd0e
- Parent:
- 8:5f0190b282f7
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 | 
|---|---|---|---|
| ChenZirui | 5:7207c9b70108 | 1 | ///////// pre-processor directives //////// | 
| eencae | 0:b7f1f47bb26a | 2 | #include "mbed.h" | 
| eencae | 0:b7f1f47bb26a | 3 | #include "Gamepad.h" | 
| eencae | 0:b7f1f47bb26a | 4 | #include "N5110.h" | 
| ChenZirui | 5:7207c9b70108 | 5 | #include "Touch.h" | 
| ChenZirui | 6:b393cfe4e0a7 | 6 | #include "Bitmap.h" | 
| ChenZirui | 5:7207c9b70108 | 7 | #ifdef WITH_TESTING | 
| ChenZirui | 6:b393cfe4e0a7 | 8 | # include "tests.h" | 
| ChenZirui | 5:7207c9b70108 | 9 | #endif | 
| eencae | 0:b7f1f47bb26a | 10 | |
| ChenZirui | 7:f61ac963eb07 | 11 | #define BOARD_WIDTH 2 | 
| ChenZirui | 7:f61ac963eb07 | 12 | #define BOARD_LENGTH 8 | 
| ChenZirui | 7:f61ac963eb07 | 13 | #define BULLET_SIZE 2 | 
| ChenZirui | 7:f61ac963eb07 | 14 | #define BULLET_SPEED 3 | 
| ChenZirui | 15:7ca2d1b2bd0e | 15 | #define BOARD_X 42 | 
| ChenZirui | 15:7ca2d1b2bd0e | 16 | #define BOARD_Y 24 | 
| ChenZirui | 5:7207c9b70108 | 17 | /////////////// structs ///////////////// | 
| ChenZirui | 5:7207c9b70108 | 18 | struct UserInput { | 
| ChenZirui | 5:7207c9b70108 | 19 | Direction d; | 
| ChenZirui | 5:7207c9b70108 | 20 | float mag; | 
| ChenZirui | 5:7207c9b70108 | 21 | }; | 
| ChenZirui | 5:7207c9b70108 | 22 | /////////////// objects /////////////// | 
| ChenZirui | 5:7207c9b70108 | 23 | N5110 lcd; | 
| eencae | 0:b7f1f47bb26a | 24 | Gamepad pad; | 
| ChenZirui | 5:7207c9b70108 | 25 | Touch touch; | 
| ChenZirui | 5:7207c9b70108 | 26 | ///////////// prototypes /////////////// | 
| ChenZirui | 5:7207c9b70108 | 27 | void init(); | 
| ChenZirui | 5:7207c9b70108 | 28 | void update_game(UserInput input); | 
| ChenZirui | 5:7207c9b70108 | 29 | void render(); | 
| ChenZirui | 5:7207c9b70108 | 30 | void welcome(); | 
| ChenZirui | 5:7207c9b70108 | 31 | |
| ChenZirui | 5:7207c9b70108 | 32 | ///////////// functions //////////////// | 
| eencae | 0:b7f1f47bb26a | 33 | int main() | 
| eencae | 0:b7f1f47bb26a | 34 | { | 
| ChenZirui | 5:7207c9b70108 | 35 | #ifdef WITH_TESTING | 
| ChenZirui | 5:7207c9b70108 | 36 | int number_of_failures = run_all_tests(); | 
| ChenZirui | 7:f61ac963eb07 | 37 | //test part | 
| ChenZirui | 5:7207c9b70108 | 38 | if(number_of_failures > 0) return number_of_failures; | 
| ChenZirui | 5:7207c9b70108 | 39 | #endif | 
| ChenZirui | 7:f61ac963eb07 | 40 | int fps = 6; // frames per second | 
| ChenZirui | 8:5f0190b282f7 | 41 | init(); // initialise | 
| ChenZirui | 7:f61ac963eb07 | 42 | welcome(); // welcome screen for user to start | 
| ChenZirui | 7:f61ac963eb07 | 43 | render(); // first draw the initial frame | 
| ChenZirui | 7:f61ac963eb07 | 44 | wait(1.0f/fps); // and wait for one frame period | 
| ChenZirui | 7:f61ac963eb07 | 45 | pad.leds_on(); //turn on all leds as 'lives' showing | 
| ChenZirui | 7:f61ac963eb07 | 46 | |
| ChenZirui | 7:f61ac963eb07 | 47 | |
| ChenZirui | 7:f61ac963eb07 | 48 | while (1) //while loop, execute reading, updating, drawing,juding part frequentrly | 
| ChenZirui | 7:f61ac963eb07 | 49 | { | 
| ChenZirui | 7:f61ac963eb07 | 50 | touch.reading(pad); | 
| ChenZirui | 7:f61ac963eb07 | 51 | touch.update(pad,lcd); | 
| ChenZirui | 5:7207c9b70108 | 52 | render(); | 
| ChenZirui | 7:f61ac963eb07 | 53 | if(touch._leds>=6) | 
| ChenZirui | 7:f61ac963eb07 | 54 | { | 
| ChenZirui | 7:f61ac963eb07 | 55 | break; // if all lives finish , you will lsot game | 
| ChenZirui | 7:f61ac963eb07 | 56 | } | 
| ChenZirui | 5:7207c9b70108 | 57 | wait(1.0f/fps); | 
| ChenZirui | 5:7207c9b70108 | 58 | } | 
| ChenZirui | 7:f61ac963eb07 | 59 | lcd.init(); | 
| ChenZirui | 7:f61ac963eb07 | 60 | pad.init(); | 
| ChenZirui | 7:f61ac963eb07 | 61 | lcd.printString(" You fail ",0,1); | 
| ChenZirui | 7:f61ac963eb07 | 62 | lcd.printString(" Press reset ",0,4); // the reminder of game lost | 
| ChenZirui | 7:f61ac963eb07 | 63 | lcd.refresh(); //function to refresh the display | 
| eencae | 0:b7f1f47bb26a | 64 | } | 
| eencae | 0:b7f1f47bb26a | 65 | |
| ChenZirui | 5:7207c9b70108 | 66 | // initialies all classes and libraries | 
| ChenZirui | 5:7207c9b70108 | 67 | void init() | 
| ChenZirui | 5:7207c9b70108 | 68 | { | 
| ChenZirui | 5:7207c9b70108 | 69 | lcd.init(); | 
| ChenZirui | 5:7207c9b70108 | 70 | pad.init(); | 
| ChenZirui | 15:7ca2d1b2bd0e | 71 | touch.init(BOARD_WIDTH,BOARD_LENGTH,BULLET_SIZE,BULLET_SPEED,lcd,BOARD_X,BOARD_Y); | 
| ChenZirui | 5:7207c9b70108 | 72 | } | 
| ChenZirui | 7:f61ac963eb07 | 73 | //clearing record before and drawing frames | 
| ChenZirui | 5:7207c9b70108 | 74 | void render() | 
| ChenZirui | 5:7207c9b70108 | 75 | { | 
| ChenZirui | 7:f61ac963eb07 | 76 | |
| ChenZirui | 5:7207c9b70108 | 77 | lcd.clear(); | 
| ChenZirui | 5:7207c9b70108 | 78 | touch.draw(lcd); | 
| ChenZirui | 5:7207c9b70108 | 79 | lcd.refresh(); | 
| ChenZirui | 5:7207c9b70108 | 80 | } | 
| ChenZirui | 7:f61ac963eb07 | 81 | //welcome screen | 
| ChenZirui | 5:7207c9b70108 | 82 | void welcome() { | 
| ChenZirui | 5:7207c9b70108 | 83 | |
| ChenZirui | 15:7ca2d1b2bd0e | 84 | lcd.printString(" STOP BULLET ",0,1); | 
| ChenZirui | 5:7207c9b70108 | 85 | lcd.printString(" Press Start ",0,4); | 
| ChenZirui | 5:7207c9b70108 | 86 | lcd.refresh(); | 
| ChenZirui | 5:7207c9b70108 | 87 | |
| ChenZirui | 5:7207c9b70108 | 88 | // wait flashing LEDs until start button is pressed | 
| ChenZirui | 5:7207c9b70108 | 89 | while ( pad.start_pressed() == false) { | 
| ChenZirui | 5:7207c9b70108 | 90 | lcd.setContrast( pad.read_pot1()); | 
| ChenZirui | 5:7207c9b70108 | 91 | pad.leds_on(); | 
| ChenZirui | 5:7207c9b70108 | 92 | wait(0.1); | 
| ChenZirui | 5:7207c9b70108 | 93 | pad.leds_off(); | 
| ChenZirui | 5:7207c9b70108 | 94 | wait(0.1); | 
| ChenZirui | 5:7207c9b70108 | 95 | } | 
| ChenZirui | 5:7207c9b70108 | 96 | |
| ChenZirui | 5:7207c9b70108 | 97 | } |