James Cummins / Mbed 2 deprecated el17jnc

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BrickBreakerEngine.h Source File

BrickBreakerEngine.h

00001 #ifndef BRICKBREAKERENGINE_H
00002 #define BRICKBREAKERENGINE_H
00003 
00004 #include "mbed.h"
00005 #include "N5110.h"
00006 #include "Gamepad.h"
00007 #include "FXOS8700CQ.h"
00008 #include "Ball.h"
00009 #include "Pause.h"
00010 #include "SDFileSystem.h"
00011 
00012 #include <cmath>
00013 
00014 /** BrickBreakerEngine Class
00015 @brief Library to power the BrickBreaker game mode
00016 @brief Includes feature to check for and write high scores
00017 
00018 @author James Cummins
00019 
00020 @code
00021 
00022 #include "mbed.h"
00023 #include "BrickBreakerEngine.h"
00024 #define RADIUS 3
00025 
00026 BrickBreakerEngine engine;   //Constructor to create engine object
00027 AnalogIn randnoise(PTB0);    //Get a random noise signal to seed the random square generator
00028 Gamepad gamepad;
00029 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
00030 Ball ball;
00031 FXOS8700CQ accelerometer(I2C_SDA,I2C_SCL);
00032 
00033 int main(){
00034     //Initialise the engine object with the ball's radius and the ball object
00035     engine.init(RADIUS, ball)
00036     int total_frames = 30000;
00037     for (int i = 0; i < total_frames; i++){
00038         //Not strictly needed as included in the initialiser
00039         engine.set_score(0);
00040         lcd.clear();
00041         ball.read_input(accelerometer);
00042         ball.update()
00043         
00044         //Check for a collision between a square and the ball object
00045         engine.check_square_collision(randnoise, ball);     //increments score if there's a collision
00046         
00047         engine.draw(lcd, ball);
00048         lcd.refresh();
00049         
00050         time_warning();
00051     }
00052     //End screen concludes game mode
00053     engine.end(gamepad, lcd);
00054     engine.write_high_scores;   //only needs computing once so perform after while loop
00055 }
00056 @endcode
00057 */
00058 
00059 class BrickBreakerEngine {
00060     
00061 public:
00062     /** 
00063     * @brief Create an engine object for the BrickBreaker game mode
00064     */
00065     BrickBreakerEngine();
00066     /** 
00067     * @brief Delete a brickbreaker engine object to free up memory
00068     */
00069     ~BrickBreakerEngine();
00070     /** 
00071     * @brief Initialise the engine object
00072     * Initialises the ball object used and sets the position of the first square
00073     * to hit with a random number. Gets the ball radius from ball object
00074     * @param radius - integer for the ball's radius
00075     * @param ball - object of Ball.h class
00076     */
00077     void init(int radius, Ball &ball);
00078     /** 
00079     * @brief draw the features of the brickbreaker game mode on the LCD screen
00080     * @param lcd - N5110 object to interact with the LCD screen
00081     * @param ball - Ball object to access ball drawing function
00082     */
00083     void brickbreaker_draw(N5110 &lcd, Ball &ball);
00084     /** 
00085     * @brief set the stored score for the brickbreaker mode to a specific value
00086     * @param score - integer with desired score value
00087     */
00088     void set_score(int score);
00089     /** 
00090     * @brief check whether the ball has collided with the square and, if so,
00091     * randomly generate a new square based on random noise on a disconnected mbed port
00092     * @param randnoise - AnalogIn reading from a disconnected port
00093     * @param ball - Ball object to get the ball's position 
00094     */
00095     void check_square_collision(AnalogIn &randnoise, Ball &ball);
00096     /** 
00097     * @brief Check how long left in the game mode, and display LED sequence to
00098     * show how long's left
00099     * @param gamepad - Gamepad object to turn on and off LEDs
00100     * @param frame - integer number of frames that have passed
00101     * @param fps - integer number of frames per second the game runs at
00102     */
00103     void time_warning(Gamepad &gamepad, int frame, int fps);
00104     /** 
00105     * @brief read the current high scores, check if the current score is higher 
00106     * than any and update the list accordingly
00107     */
00108     void write_high_scores();
00109     /** 
00110     * @brief display the end of game screen with the player's final score
00111     * @param gamepad - Gamepad object for use of buttons
00112     * @param lcd - N5110 object to display end message on screen
00113     */
00114     void end(Gamepad &gamepad, N5110 &lcd);
00115 
00116 private:
00117 //private functions
00118     void generate_rand_square(AnalogIn &randnoise); 
00119     void print_score(N5110 &lcd);
00120     void read_high_scores();
00121     void check_high_score();
00122     
00123 //private variables
00124     int _index_array[6];
00125     float _array_of_values[6];  //holds the 5 previous high scores and the score from previous play
00126     int _ball_radius;
00127     Vector2D _square_coord;
00128     int _score;
00129 };
00130 #endif