not final
Dependencies: mbed
main.cpp@14:3b4370d5b2c0, 2020-05-29 (annotated)
- Committer:
- ChenZirui
- Date:
- Fri May 29 04:25:22 2020 +0000
- Revision:
- 14:3b4370d5b2c0
- Parent:
- 8:5f0190b282f7
not final
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 |
eencae | 0:b7f1f47bb26a | 15 | |
ChenZirui | 5:7207c9b70108 | 16 | /////////////// structs ///////////////// |
ChenZirui | 5:7207c9b70108 | 17 | struct UserInput { |
ChenZirui | 5:7207c9b70108 | 18 | Direction d; |
ChenZirui | 5:7207c9b70108 | 19 | float mag; |
ChenZirui | 5:7207c9b70108 | 20 | }; |
ChenZirui | 5:7207c9b70108 | 21 | /////////////// objects /////////////// |
ChenZirui | 5:7207c9b70108 | 22 | N5110 lcd; |
eencae | 0:b7f1f47bb26a | 23 | Gamepad pad; |
ChenZirui | 5:7207c9b70108 | 24 | Touch touch; |
ChenZirui | 5:7207c9b70108 | 25 | ///////////// prototypes /////////////// |
ChenZirui | 5:7207c9b70108 | 26 | void init(); |
ChenZirui | 5:7207c9b70108 | 27 | void update_game(UserInput input); |
ChenZirui | 5:7207c9b70108 | 28 | void render(); |
ChenZirui | 5:7207c9b70108 | 29 | void welcome(); |
ChenZirui | 5:7207c9b70108 | 30 | |
ChenZirui | 5:7207c9b70108 | 31 | ///////////// functions //////////////// |
eencae | 0:b7f1f47bb26a | 32 | int main() |
eencae | 0:b7f1f47bb26a | 33 | { |
ChenZirui | 5:7207c9b70108 | 34 | #ifdef WITH_TESTING |
ChenZirui | 5:7207c9b70108 | 35 | int number_of_failures = run_all_tests(); |
ChenZirui | 7:f61ac963eb07 | 36 | //test part |
ChenZirui | 5:7207c9b70108 | 37 | if(number_of_failures > 0) return number_of_failures; |
ChenZirui | 5:7207c9b70108 | 38 | #endif |
ChenZirui | 7:f61ac963eb07 | 39 | int fps = 6; // frames per second |
ChenZirui | 8:5f0190b282f7 | 40 | init(); // initialise |
ChenZirui | 7:f61ac963eb07 | 41 | welcome(); // welcome screen for user to start |
ChenZirui | 7:f61ac963eb07 | 42 | render(); // first draw the initial frame |
ChenZirui | 7:f61ac963eb07 | 43 | wait(1.0f/fps); // and wait for one frame period |
ChenZirui | 7:f61ac963eb07 | 44 | pad.leds_on(); //turn on all leds as 'lives' showing |
ChenZirui | 7:f61ac963eb07 | 45 | |
ChenZirui | 7:f61ac963eb07 | 46 | |
ChenZirui | 7:f61ac963eb07 | 47 | while (1) //while loop, execute reading, updating, drawing,juding part frequentrly |
ChenZirui | 7:f61ac963eb07 | 48 | { |
ChenZirui | 7:f61ac963eb07 | 49 | touch.reading(pad); |
ChenZirui | 7:f61ac963eb07 | 50 | touch.update(pad,lcd); |
ChenZirui | 5:7207c9b70108 | 51 | render(); |
ChenZirui | 7:f61ac963eb07 | 52 | if(touch._leds>=6) |
ChenZirui | 7:f61ac963eb07 | 53 | { |
ChenZirui | 7:f61ac963eb07 | 54 | break; // if all lives finish , you will lsot game |
ChenZirui | 7:f61ac963eb07 | 55 | } |
ChenZirui | 5:7207c9b70108 | 56 | wait(1.0f/fps); |
ChenZirui | 5:7207c9b70108 | 57 | } |
ChenZirui | 7:f61ac963eb07 | 58 | lcd.init(); |
ChenZirui | 7:f61ac963eb07 | 59 | pad.init(); |
ChenZirui | 7:f61ac963eb07 | 60 | lcd.printString(" You fail ",0,1); |
ChenZirui | 7:f61ac963eb07 | 61 | lcd.printString(" Press reset ",0,4); // the reminder of game lost |
ChenZirui | 7:f61ac963eb07 | 62 | lcd.refresh(); //function to refresh the display |
eencae | 0:b7f1f47bb26a | 63 | } |
eencae | 0:b7f1f47bb26a | 64 | |
ChenZirui | 5:7207c9b70108 | 65 | // initialies all classes and libraries |
ChenZirui | 5:7207c9b70108 | 66 | void init() |
ChenZirui | 5:7207c9b70108 | 67 | { |
ChenZirui | 5:7207c9b70108 | 68 | lcd.init(); |
ChenZirui | 5:7207c9b70108 | 69 | pad.init(); |
ChenZirui | 7:f61ac963eb07 | 70 | touch.init(BOARD_WIDTH,BOARD_LENGTH,BULLET_SIZE,BULLET_SPEED,lcd); |
ChenZirui | 5:7207c9b70108 | 71 | } |
ChenZirui | 7:f61ac963eb07 | 72 | //clearing record before and drawing frames |
ChenZirui | 5:7207c9b70108 | 73 | void render() |
ChenZirui | 5:7207c9b70108 | 74 | { |
ChenZirui | 7:f61ac963eb07 | 75 | |
ChenZirui | 5:7207c9b70108 | 76 | lcd.clear(); |
ChenZirui | 5:7207c9b70108 | 77 | touch.draw(lcd); |
ChenZirui | 5:7207c9b70108 | 78 | lcd.refresh(); |
ChenZirui | 5:7207c9b70108 | 79 | } |
ChenZirui | 7:f61ac963eb07 | 80 | //welcome screen |
ChenZirui | 5:7207c9b70108 | 81 | void welcome() { |
ChenZirui | 5:7207c9b70108 | 82 | |
ChenZirui | 5:7207c9b70108 | 83 | lcd.printString(" touch! ",0,1); |
ChenZirui | 5:7207c9b70108 | 84 | lcd.printString(" Press Start ",0,4); |
ChenZirui | 5:7207c9b70108 | 85 | lcd.refresh(); |
ChenZirui | 5:7207c9b70108 | 86 | |
ChenZirui | 5:7207c9b70108 | 87 | // wait flashing LEDs until start button is pressed |
ChenZirui | 5:7207c9b70108 | 88 | while ( pad.start_pressed() == false) { |
ChenZirui | 5:7207c9b70108 | 89 | lcd.setContrast( pad.read_pot1()); |
ChenZirui | 5:7207c9b70108 | 90 | pad.leds_on(); |
ChenZirui | 5:7207c9b70108 | 91 | wait(0.1); |
ChenZirui | 5:7207c9b70108 | 92 | pad.leds_off(); |
ChenZirui | 5:7207c9b70108 | 93 | wait(0.1); |
ChenZirui | 5:7207c9b70108 | 94 | } |
ChenZirui | 5:7207c9b70108 | 95 | |
ChenZirui | 5:7207c9b70108 | 96 | } |