Dependencies: mbed
main.cpp@13:76bac89c447b, 2020-05-27 (annotated)
- Committer:
- Mousky
- Date:
- Wed May 27 07:33:59 2020 +0000
- Revision:
- 13:76bac89c447b
- Parent:
- 12:2af7b4868033
Final version;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Mousky | 12:2af7b4868033 | 1 | /* |
Mousky | 12:2af7b4868033 | 2 | ELEC2645 Embedded Systems Project |
Mousky | 12:2af7b4868033 | 3 | School of Electronic & Electrical Engineering |
Mousky | 12:2af7b4868033 | 4 | University of Leeds |
Mousky | 12:2af7b4868033 | 5 | 2019/20 |
Mousky | 12:2af7b4868033 | 6 | |
Mousky | 13:76bac89c447b | 7 | Name:Wu Chenhao |
Mousky | 12:2af7b4868033 | 8 | Username: el17c2w |
Mousky | 12:2af7b4868033 | 9 | Student ID Number: 201199595 |
Mousky | 12:2af7b4868033 | 10 | Date: 2020/5 |
Mousky | 12:2af7b4868033 | 11 | */ |
Mousky | 12:2af7b4868033 | 12 | |
eencae | 0:e1442f3aa3c7 | 13 | ///////// pre-processor directives //////// |
eencae | 0:e1442f3aa3c7 | 14 | #include "mbed.h" |
eencae | 0:e1442f3aa3c7 | 15 | #include "Gamepad.h" |
eencae | 0:e1442f3aa3c7 | 16 | #include "N5110.h" |
Mousky | 12:2af7b4868033 | 17 | #include "Engine.h" |
Mousky | 12:2af7b4868033 | 18 | #include "dust.h" |
eencae | 0:e1442f3aa3c7 | 19 | |
valavanisalex | 10:167d0aa1c9b3 | 20 | #ifdef WITH_TESTING |
valavanisalex | 10:167d0aa1c9b3 | 21 | # include "tests.h" |
valavanisalex | 10:167d0aa1c9b3 | 22 | #endif |
valavanisalex | 10:167d0aa1c9b3 | 23 | |
Mousky | 12:2af7b4868033 | 24 | #define PADDLE_WIDTH 10 |
Mousky | 12:2af7b4868033 | 25 | #define PADDLE_HEIGHT 2 |
eencae | 4:d349e5d847cf | 26 | #define BALL_SIZE 2 |
Mousky | 12:2af7b4868033 | 27 | #define BALL2_SIZE 2 |
Mousky | 12:2af7b4868033 | 28 | #define BALL_SPEED 2 |
Mousky | 12:2af7b4868033 | 29 | #define THING_SIZE 10 |
Mousky | 12:2af7b4868033 | 30 | #define REPADDLE_SIZE 40 |
Mousky | 12:2af7b4868033 | 31 | |
eencae | 1:25a839625a1e | 32 | |
eencae | 0:e1442f3aa3c7 | 33 | /////////////// structs ///////////////// |
eencae | 0:e1442f3aa3c7 | 34 | struct UserInput { |
eencae | 0:e1442f3aa3c7 | 35 | Direction d; |
eencae | 0:e1442f3aa3c7 | 36 | float mag; |
eencae | 0:e1442f3aa3c7 | 37 | }; |
eencae | 0:e1442f3aa3c7 | 38 | /////////////// objects /////////////// |
eencae | 0:e1442f3aa3c7 | 39 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
eencae | 0:e1442f3aa3c7 | 40 | Gamepad pad; |
Mousky | 12:2af7b4868033 | 41 | Engine pong; |
eencae | 0:e1442f3aa3c7 | 42 | ///////////// prototypes /////////////// |
eencae | 0:e1442f3aa3c7 | 43 | void init(); |
eencae | 0:e1442f3aa3c7 | 44 | void update_game(UserInput input); |
eencae | 0:e1442f3aa3c7 | 45 | void render(); |
eencae | 3:910d7e87f367 | 46 | void welcome(); |
Mousky | 12:2af7b4868033 | 47 | void menu(); |
Mousky | 12:2af7b4868033 | 48 | void loading(); |
Mousky | 12:2af7b4868033 | 49 | void producer(); |
eencae | 6:d9d05b321b4d | 50 | |
eencae | 0:e1442f3aa3c7 | 51 | ///////////// functions //////////////// |
eencae | 0:e1442f3aa3c7 | 52 | int main() |
eencae | 0:e1442f3aa3c7 | 53 | { |
valavanisalex | 10:167d0aa1c9b3 | 54 | #ifdef WITH_TESTING |
valavanisalex | 10:167d0aa1c9b3 | 55 | int number_of_failures = run_all_tests(); |
valavanisalex | 10:167d0aa1c9b3 | 56 | if(number_of_failures > 0) return number_of_failures; |
valavanisalex | 10:167d0aa1c9b3 | 57 | #endif |
eencae | 3:910d7e87f367 | 58 | int fps = 8; // frames per second |
Mousky | 12:2af7b4868033 | 59 | while(1){ |
Mousky | 12:2af7b4868033 | 60 | init(); |
Mousky | 12:2af7b4868033 | 61 | menu(); |
Mousky | 12:2af7b4868033 | 62 | if (pad.check_event(Gamepad::Y_PRESSED) == true){ |
Mousky | 12:2af7b4868033 | 63 | while(pad.check_event(Gamepad::BACK_PRESSED) == false){ |
Mousky | 12:2af7b4868033 | 64 | init(); |
Mousky | 12:2af7b4868033 | 65 | welcome(); |
Mousky | 12:2af7b4868033 | 66 | if(pad.check_event(Gamepad::START_PRESSED) == false) { |
Mousky | 12:2af7b4868033 | 67 | loading(); |
Mousky | 12:2af7b4868033 | 68 | while (1) { |
Mousky | 12:2af7b4868033 | 69 | pong.read_input(pad); |
Mousky | 12:2af7b4868033 | 70 | pong.update(lcd,pad); |
Mousky | 12:2af7b4868033 | 71 | // The world |
Mousky | 12:2af7b4868033 | 72 | pong.The_world(lcd, pad); |
Mousky | 12:2af7b4868033 | 73 | // Bite the dust |
Mousky | 12:2af7b4868033 | 74 | pong.bite_dust(lcd, pad); |
Mousky | 12:2af7b4868033 | 75 | render(); |
Mousky | 12:2af7b4868033 | 76 | wait(1.0f/fps); |
Mousky | 12:2af7b4868033 | 77 | } |
Mousky | 12:2af7b4868033 | 78 | } |
Mousky | 12:2af7b4868033 | 79 | wait(0.1); |
Mousky | 12:2af7b4868033 | 80 | } |
Mousky | 12:2af7b4868033 | 81 | } |
Mousky | 12:2af7b4868033 | 82 | if(pad.check_event(Gamepad::A_PRESSED) == true){ |
Mousky | 12:2af7b4868033 | 83 | while(pad.check_event(Gamepad::BACK_PRESSED) == false){ |
Mousky | 12:2af7b4868033 | 84 | init(); |
Mousky | 12:2af7b4868033 | 85 | producer(); |
Mousky | 12:2af7b4868033 | 86 | } |
Mousky | 12:2af7b4868033 | 87 | } |
Mousky | 12:2af7b4868033 | 88 | wait(0.1); |
eencae | 0:e1442f3aa3c7 | 89 | } |
eencae | 0:e1442f3aa3c7 | 90 | } |
eencae | 0:e1442f3aa3c7 | 91 | |
Mousky | 12:2af7b4868033 | 92 | // refresh the game and redraw the graph |
eencae | 0:e1442f3aa3c7 | 93 | void render() |
eencae | 0:e1442f3aa3c7 | 94 | { |
eencae | 3:910d7e87f367 | 95 | // clear screen, re-draw and refresh |
eencae | 3:910d7e87f367 | 96 | lcd.clear(); |
eencae | 1:25a839625a1e | 97 | pong.draw(lcd); |
eencae | 0:e1442f3aa3c7 | 98 | lcd.refresh(); |
eencae | 3:910d7e87f367 | 99 | } |
eencae | 3:910d7e87f367 | 100 | |
Mousky | 12:2af7b4868033 | 101 | void init() // initialies all classes and libraries |
Mousky | 12:2af7b4868033 | 102 | { |
Mousky | 12:2af7b4868033 | 103 | // need to initialise LCD and Gamepad |
Mousky | 12:2af7b4868033 | 104 | lcd.init(); |
Mousky | 12:2af7b4868033 | 105 | pad.init(); |
Mousky | 12:2af7b4868033 | 106 | // initialise the game with correct ball and paddle sizes |
Mousky | 12:2af7b4868033 | 107 | pong.init(PADDLE_WIDTH,PADDLE_HEIGHT,BALL_SIZE,BALL2_SIZE,BALL_SPEED,THING_SIZE,REPADDLE_SIZE); |
Mousky | 12:2af7b4868033 | 108 | } |
Mousky | 12:2af7b4868033 | 109 | |
Mousky | 12:2af7b4868033 | 110 | |
Mousky | 12:2af7b4868033 | 111 | // set the menu of the game |
Mousky | 12:2af7b4868033 | 112 | void menu() // this function draws each frame on the LCD |
Mousky | 12:2af7b4868033 | 113 | { |
Mousky | 12:2af7b4868033 | 114 | lcd.clear(); |
Mousky | 12:2af7b4868033 | 115 | lcd.printString("Press button",0,0); |
Mousky | 12:2af7b4868033 | 116 | lcd.printString("to select",0,1); |
Mousky | 12:2af7b4868033 | 117 | lcd.printString("Y: Breakout ",0,2); |
Mousky | 12:2af7b4868033 | 118 | lcd.printString(" clone",0,3); |
Mousky | 12:2af7b4868033 | 119 | lcd.printString("A: Made by",0,4); |
Mousky | 12:2af7b4868033 | 120 | lcd.refresh(); |
Mousky | 12:2af7b4868033 | 121 | wait(0.4); |
Mousky | 12:2af7b4868033 | 122 | } |
Mousky | 12:2af7b4868033 | 123 | |
Mousky | 12:2af7b4868033 | 124 | |
Mousky | 12:2af7b4868033 | 125 | void producer() |
Mousky | 12:2af7b4868033 | 126 | { |
Mousky | 12:2af7b4868033 | 127 | lcd.clear(); |
Mousky | 12:2af7b4868033 | 128 | lcd.printString(" Produced by:",0,1); |
Mousky | 12:2af7b4868033 | 129 | lcd.printString(" Chenhao Wu ",0,3); |
Mousky | 12:2af7b4868033 | 130 | lcd.printString(" 201199595 ",0,4); |
Mousky | 12:2af7b4868033 | 131 | lcd.refresh(); |
Mousky | 12:2af7b4868033 | 132 | wait(0.4); |
Mousky | 12:2af7b4868033 | 133 | } |
Mousky | 12:2af7b4868033 | 134 | |
Mousky | 12:2af7b4868033 | 135 | void welcome() { // simple splash screen displayed on start-up |
eencae | 3:910d7e87f367 | 136 | |
Mousky | 12:2af7b4868033 | 137 | lcd.printString("Breakout clone ",0,1); |
Mousky | 12:2af7b4868033 | 138 | lcd.printString(" !!!",0,2); |
eencae | 4:d349e5d847cf | 139 | lcd.printString(" Press Start ",0,4); |
eencae | 3:910d7e87f367 | 140 | lcd.refresh(); |
eencae | 3:910d7e87f367 | 141 | // wait flashing LEDs until start button is pressed |
eencae | 3:910d7e87f367 | 142 | while ( pad.check_event(Gamepad::START_PRESSED) == false) { |
eencae | 3:910d7e87f367 | 143 | pad.leds_on(); |
eencae | 3:910d7e87f367 | 144 | wait(0.1); |
eencae | 3:910d7e87f367 | 145 | pad.leds_off(); |
eencae | 3:910d7e87f367 | 146 | wait(0.1); |
eencae | 3:910d7e87f367 | 147 | } |
Mousky | 12:2af7b4868033 | 148 | } |
Mousky | 12:2af7b4868033 | 149 | |
Mousky | 12:2af7b4868033 | 150 | // set the loading state |
Mousky | 12:2af7b4868033 | 151 | void loading(){ |
Mousky | 12:2af7b4868033 | 152 | lcd.clear(); |
Mousky | 12:2af7b4868033 | 153 | lcd.printString(" JOJO's advanture!!! ",0,3); |
Mousky | 12:2af7b4868033 | 154 | lcd.refresh(); |
Mousky | 12:2af7b4868033 | 155 | wait_ms(500); |
Mousky | 12:2af7b4868033 | 156 | lcd.clear(); |
Mousky | 12:2af7b4868033 | 157 | lcd.printString(" READY!!! ",0,3); |
Mousky | 12:2af7b4868033 | 158 | lcd.refresh(); |
Mousky | 12:2af7b4868033 | 159 | wait_ms(500); |
Mousky | 12:2af7b4868033 | 160 | lcd.clear(); |
Mousky | 12:2af7b4868033 | 161 | lcd.printString(" 3 ",0,3); |
Mousky | 12:2af7b4868033 | 162 | lcd.refresh(); |
Mousky | 12:2af7b4868033 | 163 | wait_ms(500); |
Mousky | 12:2af7b4868033 | 164 | lcd.clear(); |
Mousky | 12:2af7b4868033 | 165 | lcd.printString(" 2 ",0,3); |
Mousky | 12:2af7b4868033 | 166 | lcd.refresh(); |
Mousky | 12:2af7b4868033 | 167 | wait_ms(500); |
Mousky | 12:2af7b4868033 | 168 | lcd.clear(); |
Mousky | 12:2af7b4868033 | 169 | lcd.printString(" 1 ",0,3); |
Mousky | 12:2af7b4868033 | 170 | lcd.refresh(); |
Mousky | 12:2af7b4868033 | 171 | wait_ms(500); |
Mousky | 12:2af7b4868033 | 172 | lcd.clear(); |
Mousky | 12:2af7b4868033 | 173 | lcd.printString(" Fight!!! ",0,3); |
Mousky | 12:2af7b4868033 | 174 | lcd.refresh(); |
Mousky | 12:2af7b4868033 | 175 | wait_ms(500); |
eencae | 0:e1442f3aa3c7 | 176 | } |