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.h@38:a85bc227b907, 2019-05-09 (annotated)
- Committer:
- JamesCummins
- Date:
- Thu May 09 01:09:18 2019 +0000
- Revision:
- 38:a85bc227b907
- Parent:
- 37:de1f584bce71
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 | #ifndef BRICKBREAKERENGINE_H |
| JamesCummins | 20:4a39a1a2be51 | 2 | #define BRICKBREAKERENGINE_H |
| JamesCummins | 20:4a39a1a2be51 | 3 | |
| JamesCummins | 20:4a39a1a2be51 | 4 | #include "mbed.h" |
| JamesCummins | 20:4a39a1a2be51 | 5 | #include "N5110.h" |
| JamesCummins | 20:4a39a1a2be51 | 6 | #include "Gamepad.h" |
| JamesCummins | 20:4a39a1a2be51 | 7 | #include "FXOS8700CQ.h" |
| JamesCummins | 20:4a39a1a2be51 | 8 | #include "Ball.h" |
| JamesCummins | 20:4a39a1a2be51 | 9 | #include "Pause.h" |
| JamesCummins | 25:b52aa23df120 | 10 | #include "SDFileSystem.h" |
| JamesCummins | 20:4a39a1a2be51 | 11 | |
| JamesCummins | 34:7e03391cb8a6 | 12 | #include <cmath> |
| JamesCummins | 34:7e03391cb8a6 | 13 | |
| JamesCummins | 38:a85bc227b907 | 14 | /** BrickBreakerEngine Class |
| JamesCummins | 38:a85bc227b907 | 15 | @brief Library to power the BrickBreaker game mode |
| JamesCummins | 38:a85bc227b907 | 16 | @brief Includes feature to check for and write high scores |
| JamesCummins | 38:a85bc227b907 | 17 | |
| JamesCummins | 38:a85bc227b907 | 18 | @author James Cummins |
| JamesCummins | 38:a85bc227b907 | 19 | |
| JamesCummins | 38:a85bc227b907 | 20 | @code |
| JamesCummins | 38:a85bc227b907 | 21 | |
| JamesCummins | 38:a85bc227b907 | 22 | #include "mbed.h" |
| JamesCummins | 38:a85bc227b907 | 23 | #include "BrickBreakerEngine.h" |
| JamesCummins | 38:a85bc227b907 | 24 | #define RADIUS 3 |
| JamesCummins | 38:a85bc227b907 | 25 | |
| JamesCummins | 38:a85bc227b907 | 26 | BrickBreakerEngine engine; //Constructor to create engine object |
| JamesCummins | 38:a85bc227b907 | 27 | AnalogIn randnoise(PTB0); //Get a random noise signal to seed the random square generator |
| JamesCummins | 38:a85bc227b907 | 28 | Gamepad gamepad; |
| JamesCummins | 38:a85bc227b907 | 29 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
| JamesCummins | 38:a85bc227b907 | 30 | Ball ball; |
| JamesCummins | 38:a85bc227b907 | 31 | FXOS8700CQ accelerometer(I2C_SDA,I2C_SCL); |
| JamesCummins | 38:a85bc227b907 | 32 | |
| JamesCummins | 38:a85bc227b907 | 33 | int main(){ |
| JamesCummins | 38:a85bc227b907 | 34 | //Initialise the engine object with the ball's radius and the ball object |
| JamesCummins | 38:a85bc227b907 | 35 | engine.init(RADIUS, ball) |
| JamesCummins | 38:a85bc227b907 | 36 | int total_frames = 30000; |
| JamesCummins | 38:a85bc227b907 | 37 | for (int i = 0; i < total_frames; i++){ |
| JamesCummins | 38:a85bc227b907 | 38 | //Not strictly needed as included in the initialiser |
| JamesCummins | 38:a85bc227b907 | 39 | engine.set_score(0); |
| JamesCummins | 38:a85bc227b907 | 40 | lcd.clear(); |
| JamesCummins | 38:a85bc227b907 | 41 | ball.read_input(accelerometer); |
| JamesCummins | 38:a85bc227b907 | 42 | ball.update() |
| JamesCummins | 38:a85bc227b907 | 43 | |
| JamesCummins | 38:a85bc227b907 | 44 | //Check for a collision between a square and the ball object |
| JamesCummins | 38:a85bc227b907 | 45 | engine.check_square_collision(randnoise, ball); //increments score if there's a collision |
| JamesCummins | 38:a85bc227b907 | 46 | |
| JamesCummins | 38:a85bc227b907 | 47 | engine.draw(lcd, ball); |
| JamesCummins | 38:a85bc227b907 | 48 | lcd.refresh(); |
| JamesCummins | 38:a85bc227b907 | 49 | |
| JamesCummins | 38:a85bc227b907 | 50 | time_warning(); |
| JamesCummins | 38:a85bc227b907 | 51 | } |
| JamesCummins | 38:a85bc227b907 | 52 | //End screen concludes game mode |
| JamesCummins | 38:a85bc227b907 | 53 | engine.end(gamepad, lcd); |
| JamesCummins | 38:a85bc227b907 | 54 | engine.write_high_scores; //only needs computing once so perform after while loop |
| JamesCummins | 38:a85bc227b907 | 55 | } |
| JamesCummins | 38:a85bc227b907 | 56 | @endcode |
| JamesCummins | 38:a85bc227b907 | 57 | */ |
| JamesCummins | 38:a85bc227b907 | 58 | |
| JamesCummins | 20:4a39a1a2be51 | 59 | class BrickBreakerEngine { |
| JamesCummins | 20:4a39a1a2be51 | 60 | |
| JamesCummins | 20:4a39a1a2be51 | 61 | public: |
| JamesCummins | 37:de1f584bce71 | 62 | /** |
| JamesCummins | 37:de1f584bce71 | 63 | * @brief Create an engine object for the BrickBreaker game mode |
| JamesCummins | 37:de1f584bce71 | 64 | */ |
| JamesCummins | 20:4a39a1a2be51 | 65 | BrickBreakerEngine(); |
| JamesCummins | 37:de1f584bce71 | 66 | /** |
| JamesCummins | 37:de1f584bce71 | 67 | * @brief Delete a brickbreaker engine object to free up memory |
| JamesCummins | 37:de1f584bce71 | 68 | */ |
| JamesCummins | 20:4a39a1a2be51 | 69 | ~BrickBreakerEngine(); |
| JamesCummins | 37:de1f584bce71 | 70 | /** |
| JamesCummins | 37:de1f584bce71 | 71 | * @brief Initialise the engine object |
| JamesCummins | 37:de1f584bce71 | 72 | * Initialises the ball object used and sets the position of the first square |
| JamesCummins | 37:de1f584bce71 | 73 | * to hit with a random number. Gets the ball radius from ball object |
| JamesCummins | 37:de1f584bce71 | 74 | * @param radius - integer for the ball's radius |
| JamesCummins | 37:de1f584bce71 | 75 | * @param ball - object of Ball.h class |
| JamesCummins | 37:de1f584bce71 | 76 | */ |
| JamesCummins | 37:de1f584bce71 | 77 | void init(int radius, Ball &ball); |
| JamesCummins | 37:de1f584bce71 | 78 | /** |
| JamesCummins | 37:de1f584bce71 | 79 | * @brief draw the features of the brickbreaker game mode on the LCD screen |
| JamesCummins | 37:de1f584bce71 | 80 | * @param lcd - N5110 object to interact with the LCD screen |
| JamesCummins | 37:de1f584bce71 | 81 | * @param ball - Ball object to access ball drawing function |
| JamesCummins | 37:de1f584bce71 | 82 | */ |
| JamesCummins | 37:de1f584bce71 | 83 | void brickbreaker_draw(N5110 &lcd, Ball &ball); |
| JamesCummins | 37:de1f584bce71 | 84 | /** |
| JamesCummins | 37:de1f584bce71 | 85 | * @brief set the stored score for the brickbreaker mode to a specific value |
| JamesCummins | 37:de1f584bce71 | 86 | * @param score - integer with desired score value |
| JamesCummins | 37:de1f584bce71 | 87 | */ |
| JamesCummins | 25:b52aa23df120 | 88 | void set_score(int score); |
| JamesCummins | 37:de1f584bce71 | 89 | /** |
| JamesCummins | 37:de1f584bce71 | 90 | * @brief check whether the ball has collided with the square and, if so, |
| JamesCummins | 37:de1f584bce71 | 91 | * randomly generate a new square based on random noise on a disconnected mbed port |
| JamesCummins | 37:de1f584bce71 | 92 | * @param randnoise - AnalogIn reading from a disconnected port |
| JamesCummins | 37:de1f584bce71 | 93 | * @param ball - Ball object to get the ball's position |
| JamesCummins | 37:de1f584bce71 | 94 | */ |
| JamesCummins | 25:b52aa23df120 | 95 | void check_square_collision(AnalogIn &randnoise, Ball &ball); |
| JamesCummins | 37:de1f584bce71 | 96 | /** |
| JamesCummins | 37:de1f584bce71 | 97 | * @brief Check how long left in the game mode, and display LED sequence to |
| JamesCummins | 37:de1f584bce71 | 98 | * show how long's left |
| JamesCummins | 37:de1f584bce71 | 99 | * @param gamepad - Gamepad object to turn on and off LEDs |
| JamesCummins | 37:de1f584bce71 | 100 | * @param frame - integer number of frames that have passed |
| JamesCummins | 37:de1f584bce71 | 101 | * @param fps - integer number of frames per second the game runs at |
| JamesCummins | 37:de1f584bce71 | 102 | */ |
| JamesCummins | 32:eff573ad8e42 | 103 | void time_warning(Gamepad &gamepad, int frame, int fps); |
| JamesCummins | 37:de1f584bce71 | 104 | /** |
| JamesCummins | 37:de1f584bce71 | 105 | * @brief read the current high scores, check if the current score is higher |
| JamesCummins | 37:de1f584bce71 | 106 | * than any and update the list accordingly |
| JamesCummins | 37:de1f584bce71 | 107 | */ |
| JamesCummins | 26:0dc10374546f | 108 | void write_high_scores(); |
| JamesCummins | 37:de1f584bce71 | 109 | /** |
| JamesCummins | 37:de1f584bce71 | 110 | * @brief display the end of game screen with the player's final score |
| JamesCummins | 37:de1f584bce71 | 111 | * @param gamepad - Gamepad object for use of buttons |
| JamesCummins | 37:de1f584bce71 | 112 | * @param lcd - N5110 object to display end message on screen |
| JamesCummins | 37:de1f584bce71 | 113 | */ |
| JamesCummins | 32:eff573ad8e42 | 114 | void end(Gamepad &gamepad, N5110 &lcd); |
| JamesCummins | 20:4a39a1a2be51 | 115 | |
| JamesCummins | 20:4a39a1a2be51 | 116 | private: |
| JamesCummins | 20:4a39a1a2be51 | 117 | //private functions |
| JamesCummins | 20:4a39a1a2be51 | 118 | void generate_rand_square(AnalogIn &randnoise); |
| JamesCummins | 20:4a39a1a2be51 | 119 | void print_score(N5110 &lcd); |
| JamesCummins | 26:0dc10374546f | 120 | void read_high_scores(); |
| JamesCummins | 26:0dc10374546f | 121 | void check_high_score(); |
| JamesCummins | 20:4a39a1a2be51 | 122 | |
| JamesCummins | 20:4a39a1a2be51 | 123 | //private variables |
| JamesCummins | 35:138ad0faa42b | 124 | int _index_array[6]; |
| JamesCummins | 38:a85bc227b907 | 125 | float _array_of_values[6]; //holds the 5 previous high scores and the score from previous play |
| JamesCummins | 20:4a39a1a2be51 | 126 | int _ball_radius; |
| JamesCummins | 20:4a39a1a2be51 | 127 | Vector2D _square_coord; |
| JamesCummins | 20:4a39a1a2be51 | 128 | int _score; |
| JamesCummins | 20:4a39a1a2be51 | 129 | }; |
| JamesCummins | 20:4a39a1a2be51 | 130 | #endif |