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.
main.cpp@29:42651f87522b, 2019-04-27 (annotated)
- Committer:
- JamesCummins
- Date:
- Sat Apr 27 12:50:30 2019 +0000
- Revision:
- 29:42651f87522b
- Parent:
- 26:0dc10374546f
- Child:
- 31:c95f1b1d6423
Classic wall collision coded but always returning true (preventing classic from running) - needs debugging
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JamesCummins | 0:7c96d84b673e | 1 | /* |
JamesCummins | 0:7c96d84b673e | 2 | ELEC2645 Embedded Systems Project |
JamesCummins | 0:7c96d84b673e | 3 | School of Electronic & Electrical Engineering |
JamesCummins | 0:7c96d84b673e | 4 | University of Leeds |
JamesCummins | 0:7c96d84b673e | 5 | Name: James Nathan Cummins |
JamesCummins | 0:7c96d84b673e | 6 | Username: el17jnc |
JamesCummins | 0:7c96d84b673e | 7 | Student ID Number: 201096364 |
JamesCummins | 0:7c96d84b673e | 8 | Date: 22/03/19 |
JamesCummins | 0:7c96d84b673e | 9 | */ |
JamesCummins | 0:7c96d84b673e | 10 | |
JamesCummins | 0:7c96d84b673e | 11 | #include "Gamepad.h" |
JamesCummins | 0:7c96d84b673e | 12 | #include "mbed.h" |
JamesCummins | 1:99d524f81566 | 13 | #include "N5110.h" |
JamesCummins | 20:4a39a1a2be51 | 14 | #include "BrickBreakerEngine.h" |
JamesCummins | 29:42651f87522b | 15 | #include "ClassicEngine.h" |
JamesCummins | 22:4e305ff8a050 | 16 | #include "OptionsEngine.h" |
JamesCummins | 24:c6415cc74b17 | 17 | #include "SDFileSystem.h" |
JamesCummins | 13:e5a36fbd48ae | 18 | #define RADIUS 3 |
JamesCummins | 0:7c96d84b673e | 19 | |
JamesCummins | 11:2cf0d4ce8677 | 20 | |
JamesCummins | 11:2cf0d4ce8677 | 21 | //Objects |
JamesCummins | 6:952ac12c7f00 | 22 | Gamepad gamepad; |
JamesCummins | 0:7c96d84b673e | 23 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
JamesCummins | 29:42651f87522b | 24 | ClassicEngine classic; |
JamesCummins | 20:4a39a1a2be51 | 25 | BrickBreakerEngine brick; |
JamesCummins | 22:4e305ff8a050 | 26 | OptionsEngine opt; |
JamesCummins | 29:42651f87522b | 27 | Map map; |
JamesCummins | 11:2cf0d4ce8677 | 28 | AnalogIn randnoise(PTB0); |
JamesCummins | 12:9982239b7906 | 29 | FXOS8700CQ accelerometer(I2C_SDA,I2C_SCL); |
JamesCummins | 24:c6415cc74b17 | 30 | Ball ball; |
JamesCummins | 25:b52aa23df120 | 31 | Pause pause; |
JamesCummins | 24:c6415cc74b17 | 32 | SDFileSystem sd(PTE3,PTE1,PTE2,PTE4,"sd"); |
JamesCummins | 11:2cf0d4ce8677 | 33 | |
JamesCummins | 20:4a39a1a2be51 | 34 | //Enumeric class and struct for game mode selection menu |
JamesCummins | 20:4a39a1a2be51 | 35 | enum StartOption{ |
JamesCummins | 20:4a39a1a2be51 | 36 | CLASSIC, |
JamesCummins | 20:4a39a1a2be51 | 37 | BRICKBREAKER, |
JamesCummins | 20:4a39a1a2be51 | 38 | OPTIONS |
JamesCummins | 20:4a39a1a2be51 | 39 | }; |
JamesCummins | 20:4a39a1a2be51 | 40 | |
JamesCummins | 20:4a39a1a2be51 | 41 | struct StartSelection{ |
JamesCummins | 20:4a39a1a2be51 | 42 | int output; |
JamesCummins | 20:4a39a1a2be51 | 43 | StartOption next_state[3]; |
JamesCummins | 20:4a39a1a2be51 | 44 | }; |
JamesCummins | 20:4a39a1a2be51 | 45 | |
JamesCummins | 11:2cf0d4ce8677 | 46 | //Methods |
JamesCummins | 7:6eb9cade57ab | 47 | void startscreen(); |
JamesCummins | 17:5104ecef5bd0 | 48 | StartOption menu(); |
JamesCummins | 12:9982239b7906 | 49 | void init(); |
JamesCummins | 15:1564bd6b713d | 50 | void print_start_menu(int output); |
JamesCummins | 29:42651f87522b | 51 | void classic_mode(); |
JamesCummins | 25:b52aa23df120 | 52 | void brickbreaker_mode(); |
JamesCummins | 25:b52aa23df120 | 53 | void options_menu(); |
JamesCummins | 8:4e306b16a941 | 54 | |
JamesCummins | 11:2cf0d4ce8677 | 55 | |
JamesCummins | 17:5104ecef5bd0 | 56 | ////////////////Main Function///////////////// |
JamesCummins | 8:4e306b16a941 | 57 | |
JamesCummins | 29:42651f87522b | 58 | int fps = 16; //declared globally so it doesn't have to be passed to |
JamesCummins | 29:42651f87522b | 59 | //the different game mode functions |
JamesCummins | 0:7c96d84b673e | 60 | int main(){ |
JamesCummins | 10:40c77d69e83c | 61 | init(); |
JamesCummins | 7:6eb9cade57ab | 62 | startscreen(); |
JamesCummins | 16:e846864778c4 | 63 | while(1){ |
JamesCummins | 17:5104ecef5bd0 | 64 | StartOption choice_selected = menu(); |
JamesCummins | 29:42651f87522b | 65 | if(choice_selected == CLASSIC){ classic_mode();} |
JamesCummins | 25:b52aa23df120 | 66 | if(choice_selected == BRICKBREAKER){ brickbreaker_mode();} |
JamesCummins | 25:b52aa23df120 | 67 | if(choice_selected == OPTIONS){ options_menu();} |
JamesCummins | 16:e846864778c4 | 68 | } |
JamesCummins | 0:7c96d84b673e | 69 | } |
JamesCummins | 8:4e306b16a941 | 70 | |
JamesCummins | 8:4e306b16a941 | 71 | |
JamesCummins | 11:2cf0d4ce8677 | 72 | |
JamesCummins | 8:4e306b16a941 | 73 | //////////////Start up functions/////////////////// |
JamesCummins | 11:2cf0d4ce8677 | 74 | |
JamesCummins | 10:40c77d69e83c | 75 | void init(){ |
JamesCummins | 10:40c77d69e83c | 76 | gamepad.init(); |
JamesCummins | 10:40c77d69e83c | 77 | lcd.init(); |
JamesCummins | 10:40c77d69e83c | 78 | lcd.setContrast(0.55); |
JamesCummins | 23:61fa82f76808 | 79 | brick.init(RADIUS, ball); |
JamesCummins | 22:4e305ff8a050 | 80 | opt.init(); |
JamesCummins | 29:42651f87522b | 81 | map.init(); |
JamesCummins | 25:b52aa23df120 | 82 | pause.init(); |
JamesCummins | 18:08046153e6ad | 83 | accelerometer.init(); |
JamesCummins | 24:c6415cc74b17 | 84 | sd.disk_initialize(); |
JamesCummins | 10:40c77d69e83c | 85 | wait(1); |
JamesCummins | 10:40c77d69e83c | 86 | } |
JamesCummins | 0:7c96d84b673e | 87 | |
JamesCummins | 7:6eb9cade57ab | 88 | void startscreen() { |
JamesCummins | 1:99d524f81566 | 89 | lcd.clear(); |
JamesCummins | 5:7e8f5fad7b6b | 90 | char gamename[] = {'L', 'A', 'B', 'Y', 'R', 'I', 'N', 'T', 'H', ' ', ' ', '\0'}; |
JamesCummins | 3:f2e5ffd2b94c | 91 | int i = 0; |
JamesCummins | 5:7e8f5fad7b6b | 92 | for(int a = 0; a < 35; a++){ |
JamesCummins | 4:2f01b85e57f9 | 93 | lcd.clear(); |
JamesCummins | 4:2f01b85e57f9 | 94 | lcd.drawCircle(24+2*a, 21, 3, FILL_BLACK); |
JamesCummins | 4:2f01b85e57f9 | 95 | for (i = 0; i < a/3; i++) { |
JamesCummins | 4:2f01b85e57f9 | 96 | lcd.printChar(gamename[i], 15+i*6, 2); |
JamesCummins | 4:2f01b85e57f9 | 97 | lcd.refresh(); |
JamesCummins | 4:2f01b85e57f9 | 98 | } |
JamesCummins | 15:1564bd6b713d | 99 | wait_ms(50); |
JamesCummins | 4:2f01b85e57f9 | 100 | } |
JamesCummins | 5:7e8f5fad7b6b | 101 | lcd.printString("Press start", 9, 4); |
JamesCummins | 5:7e8f5fad7b6b | 102 | lcd.printString("to play >", 15, 5); |
JamesCummins | 6:952ac12c7f00 | 103 | lcd.refresh(); |
JamesCummins | 6:952ac12c7f00 | 104 | bool advance = false; |
JamesCummins | 6:952ac12c7f00 | 105 | while (!advance){ |
JamesCummins | 6:952ac12c7f00 | 106 | if (gamepad.check_event(gamepad.START_PRESSED)){ |
JamesCummins | 6:952ac12c7f00 | 107 | lcd.clear(); |
JamesCummins | 6:952ac12c7f00 | 108 | lcd.refresh(); |
JamesCummins | 6:952ac12c7f00 | 109 | advance = true; |
JamesCummins | 6:952ac12c7f00 | 110 | } |
JamesCummins | 6:952ac12c7f00 | 111 | else { advance = false; } |
JamesCummins | 6:952ac12c7f00 | 112 | } |
JamesCummins | 0:7c96d84b673e | 113 | } |
JamesCummins | 7:6eb9cade57ab | 114 | |
JamesCummins | 17:5104ecef5bd0 | 115 | StartOption menu(){ |
JamesCummins | 15:1564bd6b713d | 116 | StartSelection fsm[3] = { |
JamesCummins | 14:108052b6222b | 117 | {0,{OPTIONS,BRICKBREAKER,CLASSIC}}, |
JamesCummins | 14:108052b6222b | 118 | {2,{CLASSIC,OPTIONS,BRICKBREAKER}}, |
JamesCummins | 14:108052b6222b | 119 | {4,{BRICKBREAKER,CLASSIC,OPTIONS}} |
JamesCummins | 7:6eb9cade57ab | 120 | }; |
JamesCummins | 14:108052b6222b | 121 | StartOption state = CLASSIC; //start with the arrow on the top option |
JamesCummins | 7:6eb9cade57ab | 122 | int next = 2; //next_state = 2 so that by default it doesn't change arrow position |
JamesCummins | 22:4e305ff8a050 | 123 | while(!(gamepad.check_event(gamepad.A_PRESSED))){ //select choice by pushing joystick to the right |
JamesCummins | 7:6eb9cade57ab | 124 | state = fsm[state].next_state[next]; |
JamesCummins | 7:6eb9cade57ab | 125 | lcd.clear(); |
JamesCummins | 7:6eb9cade57ab | 126 | if(gamepad.get_direction() == N){ next = 0;} |
JamesCummins | 7:6eb9cade57ab | 127 | else if(gamepad.get_direction() == S){ next = 1;} |
JamesCummins | 7:6eb9cade57ab | 128 | else {next = 2;} |
JamesCummins | 15:1564bd6b713d | 129 | print_start_menu(fsm[state].output); |
JamesCummins | 7:6eb9cade57ab | 130 | lcd.refresh(); |
JamesCummins | 7:6eb9cade57ab | 131 | wait(0.25); |
JamesCummins | 7:6eb9cade57ab | 132 | } |
JamesCummins | 7:6eb9cade57ab | 133 | lcd.clear(); |
JamesCummins | 7:6eb9cade57ab | 134 | lcd.refresh(); |
JamesCummins | 7:6eb9cade57ab | 135 | return state; |
JamesCummins | 15:1564bd6b713d | 136 | } |
JamesCummins | 15:1564bd6b713d | 137 | |
JamesCummins | 15:1564bd6b713d | 138 | void print_start_menu(int output){ |
JamesCummins | 15:1564bd6b713d | 139 | lcd.printString(">", 0, output); |
JamesCummins | 15:1564bd6b713d | 140 | lcd.printString("Classic", 36, 0); |
JamesCummins | 15:1564bd6b713d | 141 | lcd.printString("BrickBreak", 18, 2); |
JamesCummins | 15:1564bd6b713d | 142 | lcd.printString("Options", 36, 4); |
JamesCummins | 22:4e305ff8a050 | 143 | lcd.printString("(A = Select)", 12, 5); |
JamesCummins | 25:b52aa23df120 | 144 | } |
JamesCummins | 25:b52aa23df120 | 145 | |
JamesCummins | 29:42651f87522b | 146 | //////////////////Game Mode Functions//////////////////////////////// |
JamesCummins | 29:42651f87522b | 147 | |
JamesCummins | 29:42651f87522b | 148 | void classic_mode(){ |
JamesCummins | 29:42651f87522b | 149 | classic.init(ball, map); |
JamesCummins | 29:42651f87522b | 150 | while(!(map.check_wall_collision(gamepad, ball))){ |
JamesCummins | 29:42651f87522b | 151 | classic.classic_update(ball, accelerometer, map); |
JamesCummins | 29:42651f87522b | 152 | lcd.clear(); |
JamesCummins | 29:42651f87522b | 153 | classic.classic_draw(lcd, map, ball); |
JamesCummins | 29:42651f87522b | 154 | lcd.refresh(); |
JamesCummins | 29:42651f87522b | 155 | wait(1/fps); |
JamesCummins | 29:42651f87522b | 156 | if(gamepad.check_event(gamepad.BACK_PRESSED)){ |
JamesCummins | 29:42651f87522b | 157 | PauseOption choice = pause.pause_menu(gamepad, lcd, fps); |
JamesCummins | 29:42651f87522b | 158 | if(choice == RESUME){} |
JamesCummins | 29:42651f87522b | 159 | if(choice == RESTART){ classic.init(ball, map); } |
JamesCummins | 29:42651f87522b | 160 | if(choice == QUIT){ break; } |
JamesCummins | 29:42651f87522b | 161 | if(choice == HELP){ pause.classic_help(gamepad, lcd); } |
JamesCummins | 29:42651f87522b | 162 | } |
JamesCummins | 29:42651f87522b | 163 | } |
JamesCummins | 29:42651f87522b | 164 | } |
JamesCummins | 29:42651f87522b | 165 | |
JamesCummins | 25:b52aa23df120 | 166 | void brickbreaker_mode(){ |
JamesCummins | 25:b52aa23df120 | 167 | for(int i = 0; i < 45*fps; i++){ |
JamesCummins | 25:b52aa23df120 | 168 | if(i == 1){ brick.set_score(0); } //reset score when game restarts |
JamesCummins | 25:b52aa23df120 | 169 | ball.read_input(accelerometer); |
JamesCummins | 25:b52aa23df120 | 170 | ball.update(); |
JamesCummins | 25:b52aa23df120 | 171 | /*Vector2D position = _ball.get_position(); |
JamesCummins | 25:b52aa23df120 | 172 | printf("ball_x = %f | ball_y = %f\n", position.x, position.y); //note: running with tests causes the game to run slow and take ~2min30s*/ |
JamesCummins | 25:b52aa23df120 | 173 | brick.check_square_collision(randnoise, ball); |
JamesCummins | 25:b52aa23df120 | 174 | lcd.clear(); |
JamesCummins | 25:b52aa23df120 | 175 | brick.brickbreaker_draw(lcd, ball); |
JamesCummins | 25:b52aa23df120 | 176 | lcd.refresh(); |
JamesCummins | 25:b52aa23df120 | 177 | wait_ms(1000/fps); |
JamesCummins | 25:b52aa23df120 | 178 | if(gamepad.check_event(gamepad.BACK_PRESSED)){ |
JamesCummins | 29:42651f87522b | 179 | PauseOption choice = pause.pause_menu(gamepad, lcd, fps); |
JamesCummins | 29:42651f87522b | 180 | i = pause.brickbreaker_action(choice, gamepad, lcd, i, fps); //returns which frame to jump to |
JamesCummins | 25:b52aa23df120 | 181 | } |
JamesCummins | 25:b52aa23df120 | 182 | } |
JamesCummins | 26:0dc10374546f | 183 | brick.write_high_scores(); |
JamesCummins | 25:b52aa23df120 | 184 | } |
JamesCummins | 25:b52aa23df120 | 185 | |
JamesCummins | 25:b52aa23df120 | 186 | void options_menu(){ |
JamesCummins | 25:b52aa23df120 | 187 | Option choice = BRIGHTNESS; |
JamesCummins | 25:b52aa23df120 | 188 | while(!(gamepad.check_event(gamepad.A_PRESSED))){ |
JamesCummins | 25:b52aa23df120 | 189 | lcd.clear(); |
JamesCummins | 25:b52aa23df120 | 190 | opt.display_options(lcd); |
JamesCummins | 25:b52aa23df120 | 191 | choice = opt.option_selection(gamepad, lcd); |
JamesCummins | 25:b52aa23df120 | 192 | lcd.refresh(); |
JamesCummins | 25:b52aa23df120 | 193 | wait(0.2); |
JamesCummins | 25:b52aa23df120 | 194 | } |
JamesCummins | 25:b52aa23df120 | 195 | if(choice == BRIGHTNESS){ opt.change_brightness(gamepad, lcd); } |
JamesCummins | 25:b52aa23df120 | 196 | if(choice == BALL_SPEED){ opt.change_ball_speed(gamepad, lcd, ball); } |
JamesCummins | 26:0dc10374546f | 197 | if(choice == HIGH_SCORES){ opt.view_high_scores(gamepad, lcd); } |
JamesCummins | 25:b52aa23df120 | 198 | } |
JamesCummins | 29:42651f87522b | 199 | |
JamesCummins | 29:42651f87522b | 200 |