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@26:0dc10374546f, 2019-04-23 (annotated)
- Committer:
- JamesCummins
- Date:
- Tue Apr 23 23:55:05 2019 +0000
- Revision:
- 26:0dc10374546f
- Parent:
- 25:b52aa23df120
- Child:
- 29:42651f87522b
Big array for classic mode's map [200x500] approx 55% complete
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 | 22:4e305ff8a050 | 15 | #include "OptionsEngine.h" |
JamesCummins | 24:c6415cc74b17 | 16 | #include "SDFileSystem.h" |
JamesCummins | 13:e5a36fbd48ae | 17 | #define RADIUS 3 |
JamesCummins | 0:7c96d84b673e | 18 | |
JamesCummins | 11:2cf0d4ce8677 | 19 | |
JamesCummins | 11:2cf0d4ce8677 | 20 | //Objects |
JamesCummins | 6:952ac12c7f00 | 21 | Gamepad gamepad; |
JamesCummins | 0:7c96d84b673e | 22 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
JamesCummins | 20:4a39a1a2be51 | 23 | BrickBreakerEngine brick; |
JamesCummins | 22:4e305ff8a050 | 24 | OptionsEngine opt; |
JamesCummins | 11:2cf0d4ce8677 | 25 | AnalogIn randnoise(PTB0); |
JamesCummins | 12:9982239b7906 | 26 | FXOS8700CQ accelerometer(I2C_SDA,I2C_SCL); |
JamesCummins | 24:c6415cc74b17 | 27 | Ball ball; |
JamesCummins | 25:b52aa23df120 | 28 | Pause pause; |
JamesCummins | 24:c6415cc74b17 | 29 | SDFileSystem sd(PTE3,PTE1,PTE2,PTE4,"sd"); |
JamesCummins | 11:2cf0d4ce8677 | 30 | |
JamesCummins | 20:4a39a1a2be51 | 31 | //Enumeric class and struct for game mode selection menu |
JamesCummins | 20:4a39a1a2be51 | 32 | enum StartOption{ |
JamesCummins | 20:4a39a1a2be51 | 33 | CLASSIC, |
JamesCummins | 20:4a39a1a2be51 | 34 | BRICKBREAKER, |
JamesCummins | 20:4a39a1a2be51 | 35 | OPTIONS |
JamesCummins | 20:4a39a1a2be51 | 36 | }; |
JamesCummins | 20:4a39a1a2be51 | 37 | |
JamesCummins | 20:4a39a1a2be51 | 38 | struct StartSelection{ |
JamesCummins | 20:4a39a1a2be51 | 39 | int output; |
JamesCummins | 20:4a39a1a2be51 | 40 | StartOption next_state[3]; |
JamesCummins | 20:4a39a1a2be51 | 41 | }; |
JamesCummins | 20:4a39a1a2be51 | 42 | |
JamesCummins | 11:2cf0d4ce8677 | 43 | //Methods |
JamesCummins | 7:6eb9cade57ab | 44 | void startscreen(); |
JamesCummins | 17:5104ecef5bd0 | 45 | StartOption menu(); |
JamesCummins | 12:9982239b7906 | 46 | void init(); |
JamesCummins | 15:1564bd6b713d | 47 | void print_start_menu(int output); |
JamesCummins | 25:b52aa23df120 | 48 | void brickbreaker_mode(); |
JamesCummins | 25:b52aa23df120 | 49 | void options_menu(); |
JamesCummins | 8:4e306b16a941 | 50 | |
JamesCummins | 11:2cf0d4ce8677 | 51 | |
JamesCummins | 17:5104ecef5bd0 | 52 | ////////////////Main Function///////////////// |
JamesCummins | 8:4e306b16a941 | 53 | |
JamesCummins | 25:b52aa23df120 | 54 | int fps = 30; |
JamesCummins | 8:4e306b16a941 | 55 | |
JamesCummins | 0:7c96d84b673e | 56 | int main(){ |
JamesCummins | 10:40c77d69e83c | 57 | init(); |
JamesCummins | 7:6eb9cade57ab | 58 | startscreen(); |
JamesCummins | 16:e846864778c4 | 59 | while(1){ |
JamesCummins | 17:5104ecef5bd0 | 60 | StartOption choice_selected = menu(); |
JamesCummins | 20:4a39a1a2be51 | 61 | if(choice_selected == CLASSIC){ /*engine.classic_mode(accelerometer, gamepad, lcd, fps);*/} |
JamesCummins | 25:b52aa23df120 | 62 | if(choice_selected == BRICKBREAKER){ brickbreaker_mode();} |
JamesCummins | 25:b52aa23df120 | 63 | if(choice_selected == OPTIONS){ options_menu();} |
JamesCummins | 16:e846864778c4 | 64 | } |
JamesCummins | 0:7c96d84b673e | 65 | } |
JamesCummins | 8:4e306b16a941 | 66 | |
JamesCummins | 8:4e306b16a941 | 67 | |
JamesCummins | 11:2cf0d4ce8677 | 68 | |
JamesCummins | 8:4e306b16a941 | 69 | //////////////Start up functions/////////////////// |
JamesCummins | 11:2cf0d4ce8677 | 70 | |
JamesCummins | 10:40c77d69e83c | 71 | void init(){ |
JamesCummins | 10:40c77d69e83c | 72 | gamepad.init(); |
JamesCummins | 10:40c77d69e83c | 73 | lcd.init(); |
JamesCummins | 10:40c77d69e83c | 74 | lcd.setContrast(0.55); |
JamesCummins | 23:61fa82f76808 | 75 | brick.init(RADIUS, ball); |
JamesCummins | 22:4e305ff8a050 | 76 | opt.init(); |
JamesCummins | 25:b52aa23df120 | 77 | pause.init(); |
JamesCummins | 18:08046153e6ad | 78 | accelerometer.init(); |
JamesCummins | 24:c6415cc74b17 | 79 | sd.disk_initialize(); |
JamesCummins | 10:40c77d69e83c | 80 | wait(1); |
JamesCummins | 10:40c77d69e83c | 81 | } |
JamesCummins | 0:7c96d84b673e | 82 | |
JamesCummins | 7:6eb9cade57ab | 83 | void startscreen() { |
JamesCummins | 1:99d524f81566 | 84 | lcd.clear(); |
JamesCummins | 5:7e8f5fad7b6b | 85 | char gamename[] = {'L', 'A', 'B', 'Y', 'R', 'I', 'N', 'T', 'H', ' ', ' ', '\0'}; |
JamesCummins | 3:f2e5ffd2b94c | 86 | int i = 0; |
JamesCummins | 5:7e8f5fad7b6b | 87 | for(int a = 0; a < 35; a++){ |
JamesCummins | 4:2f01b85e57f9 | 88 | lcd.clear(); |
JamesCummins | 4:2f01b85e57f9 | 89 | lcd.drawCircle(24+2*a, 21, 3, FILL_BLACK); |
JamesCummins | 4:2f01b85e57f9 | 90 | for (i = 0; i < a/3; i++) { |
JamesCummins | 4:2f01b85e57f9 | 91 | lcd.printChar(gamename[i], 15+i*6, 2); |
JamesCummins | 4:2f01b85e57f9 | 92 | lcd.refresh(); |
JamesCummins | 4:2f01b85e57f9 | 93 | } |
JamesCummins | 15:1564bd6b713d | 94 | wait_ms(50); |
JamesCummins | 4:2f01b85e57f9 | 95 | } |
JamesCummins | 5:7e8f5fad7b6b | 96 | lcd.printString("Press start", 9, 4); |
JamesCummins | 5:7e8f5fad7b6b | 97 | lcd.printString("to play >", 15, 5); |
JamesCummins | 6:952ac12c7f00 | 98 | lcd.refresh(); |
JamesCummins | 6:952ac12c7f00 | 99 | bool advance = false; |
JamesCummins | 6:952ac12c7f00 | 100 | while (!advance){ |
JamesCummins | 6:952ac12c7f00 | 101 | if (gamepad.check_event(gamepad.START_PRESSED)){ |
JamesCummins | 6:952ac12c7f00 | 102 | lcd.clear(); |
JamesCummins | 6:952ac12c7f00 | 103 | lcd.refresh(); |
JamesCummins | 6:952ac12c7f00 | 104 | advance = true; |
JamesCummins | 6:952ac12c7f00 | 105 | } |
JamesCummins | 6:952ac12c7f00 | 106 | else { advance = false; } |
JamesCummins | 6:952ac12c7f00 | 107 | } |
JamesCummins | 0:7c96d84b673e | 108 | } |
JamesCummins | 7:6eb9cade57ab | 109 | |
JamesCummins | 17:5104ecef5bd0 | 110 | StartOption menu(){ |
JamesCummins | 15:1564bd6b713d | 111 | StartSelection fsm[3] = { |
JamesCummins | 14:108052b6222b | 112 | {0,{OPTIONS,BRICKBREAKER,CLASSIC}}, |
JamesCummins | 14:108052b6222b | 113 | {2,{CLASSIC,OPTIONS,BRICKBREAKER}}, |
JamesCummins | 14:108052b6222b | 114 | {4,{BRICKBREAKER,CLASSIC,OPTIONS}} |
JamesCummins | 7:6eb9cade57ab | 115 | }; |
JamesCummins | 14:108052b6222b | 116 | StartOption state = CLASSIC; //start with the arrow on the top option |
JamesCummins | 7:6eb9cade57ab | 117 | int next = 2; //next_state = 2 so that by default it doesn't change arrow position |
JamesCummins | 22:4e305ff8a050 | 118 | while(!(gamepad.check_event(gamepad.A_PRESSED))){ //select choice by pushing joystick to the right |
JamesCummins | 7:6eb9cade57ab | 119 | state = fsm[state].next_state[next]; |
JamesCummins | 7:6eb9cade57ab | 120 | lcd.clear(); |
JamesCummins | 7:6eb9cade57ab | 121 | if(gamepad.get_direction() == N){ next = 0;} |
JamesCummins | 7:6eb9cade57ab | 122 | else if(gamepad.get_direction() == S){ next = 1;} |
JamesCummins | 7:6eb9cade57ab | 123 | else {next = 2;} |
JamesCummins | 15:1564bd6b713d | 124 | print_start_menu(fsm[state].output); |
JamesCummins | 7:6eb9cade57ab | 125 | lcd.refresh(); |
JamesCummins | 7:6eb9cade57ab | 126 | wait(0.25); |
JamesCummins | 7:6eb9cade57ab | 127 | } |
JamesCummins | 7:6eb9cade57ab | 128 | lcd.clear(); |
JamesCummins | 7:6eb9cade57ab | 129 | lcd.refresh(); |
JamesCummins | 7:6eb9cade57ab | 130 | return state; |
JamesCummins | 15:1564bd6b713d | 131 | } |
JamesCummins | 15:1564bd6b713d | 132 | |
JamesCummins | 15:1564bd6b713d | 133 | void print_start_menu(int output){ |
JamesCummins | 15:1564bd6b713d | 134 | lcd.printString(">", 0, output); |
JamesCummins | 15:1564bd6b713d | 135 | lcd.printString("Classic", 36, 0); |
JamesCummins | 15:1564bd6b713d | 136 | lcd.printString("BrickBreak", 18, 2); |
JamesCummins | 15:1564bd6b713d | 137 | lcd.printString("Options", 36, 4); |
JamesCummins | 22:4e305ff8a050 | 138 | lcd.printString("(A = Select)", 12, 5); |
JamesCummins | 25:b52aa23df120 | 139 | } |
JamesCummins | 25:b52aa23df120 | 140 | |
JamesCummins | 25:b52aa23df120 | 141 | void brickbreaker_mode(){ |
JamesCummins | 25:b52aa23df120 | 142 | for(int i = 0; i < 45*fps; i++){ |
JamesCummins | 25:b52aa23df120 | 143 | if(i == 1){ brick.set_score(0); } //reset score when game restarts |
JamesCummins | 25:b52aa23df120 | 144 | ball.read_input(accelerometer); |
JamesCummins | 25:b52aa23df120 | 145 | ball.update(); |
JamesCummins | 25:b52aa23df120 | 146 | /*Vector2D position = _ball.get_position(); |
JamesCummins | 25:b52aa23df120 | 147 | 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 | 148 | brick.check_square_collision(randnoise, ball); |
JamesCummins | 25:b52aa23df120 | 149 | lcd.clear(); |
JamesCummins | 25:b52aa23df120 | 150 | brick.brickbreaker_draw(lcd, ball); |
JamesCummins | 25:b52aa23df120 | 151 | lcd.refresh(); |
JamesCummins | 25:b52aa23df120 | 152 | wait_ms(1000/fps); |
JamesCummins | 25:b52aa23df120 | 153 | if(gamepad.check_event(gamepad.BACK_PRESSED)){ |
JamesCummins | 25:b52aa23df120 | 154 | int choice = pause.pause_menu(gamepad, lcd, fps, i, BRICKBREAKER_MODE); |
JamesCummins | 25:b52aa23df120 | 155 | i = choice; |
JamesCummins | 25:b52aa23df120 | 156 | } |
JamesCummins | 25:b52aa23df120 | 157 | } |
JamesCummins | 26:0dc10374546f | 158 | brick.write_high_scores(); |
JamesCummins | 25:b52aa23df120 | 159 | } |
JamesCummins | 25:b52aa23df120 | 160 | |
JamesCummins | 25:b52aa23df120 | 161 | void options_menu(){ |
JamesCummins | 25:b52aa23df120 | 162 | Option choice = BRIGHTNESS; |
JamesCummins | 25:b52aa23df120 | 163 | while(!(gamepad.check_event(gamepad.A_PRESSED))){ |
JamesCummins | 25:b52aa23df120 | 164 | lcd.clear(); |
JamesCummins | 25:b52aa23df120 | 165 | opt.display_options(lcd); |
JamesCummins | 25:b52aa23df120 | 166 | choice = opt.option_selection(gamepad, lcd); |
JamesCummins | 25:b52aa23df120 | 167 | lcd.refresh(); |
JamesCummins | 25:b52aa23df120 | 168 | wait(0.2); |
JamesCummins | 25:b52aa23df120 | 169 | } |
JamesCummins | 25:b52aa23df120 | 170 | if(choice == BRIGHTNESS){ opt.change_brightness(gamepad, lcd); } |
JamesCummins | 25:b52aa23df120 | 171 | if(choice == BALL_SPEED){ opt.change_ball_speed(gamepad, lcd, ball); } |
JamesCummins | 26:0dc10374546f | 172 | if(choice == HIGH_SCORES){ opt.view_high_scores(gamepad, lcd); } |
JamesCummins | 25:b52aa23df120 | 173 | } |