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.
BrickBreaker_Engine/BrickBreakerEngine.cpp@38:a85bc227b907, 2019-05-09 (annotated)
- Committer:
- JamesCummins
- Date:
- Thu May 09 01:09:18 2019 +0000
- Revision:
- 38:a85bc227b907
- Parent:
- 35:138ad0faa42b
Doxygen documentation written out and in line commenting completed. Game working and finished. Tests.h still to write out
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JamesCummins | 20:4a39a1a2be51 | 1 | #include "BrickBreakerEngine.h" |
JamesCummins | 20:4a39a1a2be51 | 2 | |
JamesCummins | 20:4a39a1a2be51 | 3 | //Constructor |
JamesCummins | 20:4a39a1a2be51 | 4 | BrickBreakerEngine::BrickBreakerEngine(){ |
JamesCummins | 20:4a39a1a2be51 | 5 | } |
JamesCummins | 20:4a39a1a2be51 | 6 | |
JamesCummins | 20:4a39a1a2be51 | 7 | //Destructor |
JamesCummins | 20:4a39a1a2be51 | 8 | BrickBreakerEngine::~BrickBreakerEngine(){ |
JamesCummins | 20:4a39a1a2be51 | 9 | } |
JamesCummins | 20:4a39a1a2be51 | 10 | |
JamesCummins | 20:4a39a1a2be51 | 11 | //Initialiser |
JamesCummins | 23:61fa82f76808 | 12 | void BrickBreakerEngine::init(int radius, Ball &ball){ |
JamesCummins | 20:4a39a1a2be51 | 13 | _ball_radius = radius; |
JamesCummins | 23:61fa82f76808 | 14 | ball.init(_ball_radius); |
JamesCummins | 38:a85bc227b907 | 15 | srand(time(NULL)); //first square will be the same each time |
JamesCummins | 38:a85bc227b907 | 16 | _square_coord.x = 2 + rand()%80; //but is randomly generated by random noise thereafter |
JamesCummins | 20:4a39a1a2be51 | 17 | _square_coord.y = 8 + rand()%36; |
JamesCummins | 20:4a39a1a2be51 | 18 | } |
JamesCummins | 20:4a39a1a2be51 | 19 | |
JamesCummins | 20:4a39a1a2be51 | 20 | /////////////Brickbreaker functionality///////////////////// |
JamesCummins | 20:4a39a1a2be51 | 21 | |
JamesCummins | 38:a85bc227b907 | 22 | void BrickBreakerEngine::brickbreaker_draw(N5110 &lcd, Ball &ball){ |
JamesCummins | 38:a85bc227b907 | 23 | ball.draw(lcd); //draw ball |
JamesCummins | 38:a85bc227b907 | 24 | lcd.drawRect(_square_coord.x, _square_coord.y, 5, 5, FILL_BLACK); //draw randomly generated square |
JamesCummins | 38:a85bc227b907 | 25 | print_score(lcd); //draw score in corner |
JamesCummins | 38:a85bc227b907 | 26 | } |
JamesCummins | 38:a85bc227b907 | 27 | |
JamesCummins | 25:b52aa23df120 | 28 | void BrickBreakerEngine::set_score(int score){ |
JamesCummins | 38:a85bc227b907 | 29 | _score = score; //used to initialise score upon restart |
JamesCummins | 25:b52aa23df120 | 30 | } |
JamesCummins | 25:b52aa23df120 | 31 | |
JamesCummins | 20:4a39a1a2be51 | 32 | void BrickBreakerEngine::generate_rand_square(AnalogIn &randnoise){ |
JamesCummins | 38:a85bc227b907 | 33 | int rand = randnoise.read_u16(); //noise on unconnected ADC channel expanded to 16 bit int |
JamesCummins | 38:a85bc227b907 | 34 | Vector2D square_coords = {rand % 80, 8 + rand % 36}; //offset by 8 so it doesn't overlap the score |
JamesCummins | 38:a85bc227b907 | 35 | _square_coord = square_coords; //update the square coordinates for rendering |
JamesCummins | 20:4a39a1a2be51 | 36 | } |
JamesCummins | 20:4a39a1a2be51 | 37 | |
JamesCummins | 23:61fa82f76808 | 38 | void BrickBreakerEngine::check_square_collision(AnalogIn &randnoise, Ball &ball){ |
JamesCummins | 23:61fa82f76808 | 39 | int radius = ball.get_radius(); |
JamesCummins | 23:61fa82f76808 | 40 | Vector2D position = ball.get_position(); |
JamesCummins | 38:a85bc227b907 | 41 | if (abs(position.x - (_square_coord.x + 2)) <= (radius + 2) and //adding 2 to square coords gives the centre of the square |
JamesCummins | 38:a85bc227b907 | 42 | abs(position.y - (_square_coord.y + 2)) <= (radius + 2)) { // <= radius+2 checks that edges aren't touching rather than centres of each |
JamesCummins | 38:a85bc227b907 | 43 | _score++; //increment score if collision |
JamesCummins | 38:a85bc227b907 | 44 | generate_rand_square(randnoise); //generate new square |
JamesCummins | 20:4a39a1a2be51 | 45 | } |
JamesCummins | 20:4a39a1a2be51 | 46 | } |
JamesCummins | 20:4a39a1a2be51 | 47 | |
JamesCummins | 20:4a39a1a2be51 | 48 | void BrickBreakerEngine::print_score(N5110 &lcd){ |
JamesCummins | 20:4a39a1a2be51 | 49 | char buffer[2]; |
JamesCummins | 20:4a39a1a2be51 | 50 | int score = _score; |
JamesCummins | 38:a85bc227b907 | 51 | sprintf(buffer, "%d", score); //use sprintf so that a variable (rather than a constant) can be use in printString. |
JamesCummins | 38:a85bc227b907 | 52 | lcd.printString(buffer, 72, 0); //print in top right corner of the screen |
JamesCummins | 20:4a39a1a2be51 | 53 | } |
JamesCummins | 25:b52aa23df120 | 54 | |
JamesCummins | 26:0dc10374546f | 55 | void BrickBreakerEngine::read_high_scores(){ |
JamesCummins | 38:a85bc227b907 | 56 | FILE *fp; //open file stream |
JamesCummins | 38:a85bc227b907 | 57 | fp = fopen("/sd/bbhighscores.txt", "r"); //open file for brickbreak high scores |
JamesCummins | 34:7e03391cb8a6 | 58 | |
JamesCummins | 34:7e03391cb8a6 | 59 | if(fp == NULL){ |
JamesCummins | 38:a85bc227b907 | 60 | printf("Error: Could not open file"); //check successfully opened |
JamesCummins | 34:7e03391cb8a6 | 61 | } else { |
JamesCummins | 34:7e03391cb8a6 | 62 | int i = 0; |
JamesCummins | 38:a85bc227b907 | 63 | rewind(fp); //return to start of file |
JamesCummins | 34:7e03391cb8a6 | 64 | |
JamesCummins | 38:a85bc227b907 | 65 | while(fscanf(fp, "%d,%f", &_index_array[i], &_array_of_values[i]) != EOF){ //read into respective arrays while iterating through line by line |
JamesCummins | 38:a85bc227b907 | 66 | i++; //increment which line of array to write into |
JamesCummins | 34:7e03391cb8a6 | 67 | } |
JamesCummins | 38:a85bc227b907 | 68 | fclose(fp); //close file stream |
JamesCummins | 26:0dc10374546f | 69 | } |
JamesCummins | 26:0dc10374546f | 70 | } |
JamesCummins | 26:0dc10374546f | 71 | |
JamesCummins | 26:0dc10374546f | 72 | void BrickBreakerEngine::check_high_score(){ |
JamesCummins | 26:0dc10374546f | 73 | read_high_scores(); |
JamesCummins | 38:a85bc227b907 | 74 | for(int i = 4; i >= 0; i--){ //quick algorithm to keep moving the current score |
JamesCummins | 38:a85bc227b907 | 75 | if(_array_of_values[i] < _score){ //up the leaderboard until it reaches a score greater than itself |
JamesCummins | 35:138ad0faa42b | 76 | _array_of_values[i+1] = _array_of_values[i]; |
JamesCummins | 38:a85bc227b907 | 77 | _array_of_values[i] = _score; //note: requires the file to be initialised with dummy scores in descending order |
JamesCummins | 34:7e03391cb8a6 | 78 | } |
JamesCummins | 38:a85bc227b907 | 79 | } //produces updated array with the current score integrated into high scores if its a top 5 score |
JamesCummins | 26:0dc10374546f | 80 | } |
JamesCummins | 26:0dc10374546f | 81 | |
JamesCummins | 26:0dc10374546f | 82 | void BrickBreakerEngine::write_high_scores(){ |
JamesCummins | 26:0dc10374546f | 83 | check_high_score(); |
JamesCummins | 38:a85bc227b907 | 84 | FILE *fp; //open file stream |
JamesCummins | 38:a85bc227b907 | 85 | fp = fopen("/sd/bbhighscores.txt", "w"); //open brickbreaker high score file ready for writing |
JamesCummins | 34:7e03391cb8a6 | 86 | if(fp == NULL){ |
JamesCummins | 38:a85bc227b907 | 87 | printf("Error: Could not open file"); //check open |
JamesCummins | 34:7e03391cb8a6 | 88 | } else { |
JamesCummins | 38:a85bc227b907 | 89 | for(int i = 0; i < 5; i++){ |
JamesCummins | 38:a85bc227b907 | 90 | fprintf(fp, "%d, %f\n", _index_array[i], _array_of_values[i]); //send first 5 values of index and value array to SD card |
JamesCummins | 34:7e03391cb8a6 | 91 | } |
JamesCummins | 38:a85bc227b907 | 92 | fclose(fp); //close file stream |
JamesCummins | 26:0dc10374546f | 93 | } |
JamesCummins | 32:eff573ad8e42 | 94 | } |
JamesCummins | 32:eff573ad8e42 | 95 | |
JamesCummins | 32:eff573ad8e42 | 96 | void BrickBreakerEngine::time_warning(Gamepad &gamepad, int frame, int fps){ |
JamesCummins | 38:a85bc227b907 | 97 | gamepad.leds_on(); //initially set all LEDs on |
JamesCummins | 32:eff573ad8e42 | 98 | int game_length = 45 * fps; |
JamesCummins | 38:a85bc227b907 | 99 | if(frame > 0.17 * game_length){ //when 1/6th of game played, turn first LED off |
JamesCummins | 32:eff573ad8e42 | 100 | gamepad.led(6, 0); |
JamesCummins | 32:eff573ad8e42 | 101 | } |
JamesCummins | 38:a85bc227b907 | 102 | if(frame > 0.33 * game_length){ //when 1/3rd of game played, turn second LED off |
JamesCummins | 32:eff573ad8e42 | 103 | gamepad.led(5, 0); |
JamesCummins | 32:eff573ad8e42 | 104 | } |
JamesCummins | 38:a85bc227b907 | 105 | if(frame > 0.5 * game_length){ //1/2 played, turn 3rd LED off |
JamesCummins | 32:eff573ad8e42 | 106 | gamepad.led(4, 0); |
JamesCummins | 32:eff573ad8e42 | 107 | } |
JamesCummins | 38:a85bc227b907 | 108 | if(frame > 0.67 * game_length){ //2/3 played, turn 4th off |
JamesCummins | 32:eff573ad8e42 | 109 | gamepad.led(3, 0); |
JamesCummins | 32:eff573ad8e42 | 110 | } |
JamesCummins | 38:a85bc227b907 | 111 | if(frame > 0.83 * game_length){ //5/6th played, turn 5th off |
JamesCummins | 32:eff573ad8e42 | 112 | gamepad.led(2, 0); |
JamesCummins | 32:eff573ad8e42 | 113 | } |
JamesCummins | 38:a85bc227b907 | 114 | if(frame > 0.97 * game_length){ //turn final one off just before the game ends |
JamesCummins | 32:eff573ad8e42 | 115 | gamepad.led(1, 0); |
JamesCummins | 32:eff573ad8e42 | 116 | } |
JamesCummins | 32:eff573ad8e42 | 117 | } |
JamesCummins | 32:eff573ad8e42 | 118 | |
JamesCummins | 32:eff573ad8e42 | 119 | void BrickBreakerEngine::end(Gamepad &gamepad, N5110 &lcd){ |
JamesCummins | 38:a85bc227b907 | 120 | while(!(gamepad.check_event(gamepad.A_PRESSED))){ //check when user wants to advance |
JamesCummins | 32:eff573ad8e42 | 121 | lcd.clear(); |
JamesCummins | 32:eff573ad8e42 | 122 | char buffer[2]; |
JamesCummins | 38:a85bc227b907 | 123 | sprintf(buffer, "%d", _score); //read the final score into buffer |
JamesCummins | 32:eff573ad8e42 | 124 | lcd.printString("Time up!", 18, 1); |
JamesCummins | 38:a85bc227b907 | 125 | lcd.printString("You scored:", 9, 3); //display time up message |
JamesCummins | 38:a85bc227b907 | 126 | lcd.printString(buffer, 36, 4); //display final score |
JamesCummins | 38:a85bc227b907 | 127 | lcd.printString("(A = back)", 24, 5); //check for advance message |
JamesCummins | 32:eff573ad8e42 | 128 | lcd.refresh(); |
JamesCummins | 38:a85bc227b907 | 129 | wait(0.2); //longer duration between frames to reduce button bounce impact |
JamesCummins | 32:eff573ad8e42 | 130 | } |
JamesCummins | 35:138ad0faa42b | 131 | } |