James Cummins / Mbed 2 deprecated el17jnc

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BrickBreakerEngine.cpp Source File

BrickBreakerEngine.cpp

00001 #include "BrickBreakerEngine.h"
00002 
00003 //Constructor
00004 BrickBreakerEngine::BrickBreakerEngine(){
00005 }
00006 
00007 //Destructor
00008 BrickBreakerEngine::~BrickBreakerEngine(){
00009 }
00010 
00011 //Initialiser
00012 void BrickBreakerEngine::init(int radius, Ball &ball){
00013     _ball_radius = radius;
00014     ball.init(_ball_radius);
00015     srand(time(NULL));                  //first square will be the same each time
00016     _square_coord.x = 2 + rand()%80;    //but is randomly generated by random noise thereafter
00017     _square_coord.y = 8 + rand()%36;
00018 }
00019 
00020 /////////////Brickbreaker functionality/////////////////////
00021 
00022 void BrickBreakerEngine::brickbreaker_draw(N5110 &lcd, Ball &ball){
00023     ball.draw(lcd);     //draw ball
00024     lcd.drawRect(_square_coord.x, _square_coord.y, 5, 5, FILL_BLACK);   //draw randomly generated square
00025     print_score(lcd);       //draw score in corner
00026 }
00027 
00028 void BrickBreakerEngine::set_score(int score){
00029     _score = score;         //used to initialise score upon restart
00030 }
00031 
00032 void BrickBreakerEngine::generate_rand_square(AnalogIn &randnoise){
00033     int rand = randnoise.read_u16();            //noise on unconnected ADC channel expanded to 16 bit int
00034     Vector2D square_coords = {rand % 80, 8 + rand % 36};    //offset by 8 so it doesn't overlap the score
00035     _square_coord = square_coords;      //update the square coordinates for rendering
00036 }
00037 
00038 void BrickBreakerEngine::check_square_collision(AnalogIn &randnoise, Ball &ball){
00039     int radius = ball.get_radius();
00040     Vector2D position = ball.get_position();
00041     if (abs(position.x - (_square_coord.x + 2)) <= (radius + 2) and         //adding 2 to square coords gives the centre of the square
00042         abs(position.y - (_square_coord.y + 2)) <= (radius + 2)) {          // <= radius+2 checks that edges aren't touching rather than centres of each
00043             _score++;           //increment score if collision
00044             generate_rand_square(randnoise);        //generate new square
00045     }
00046 }
00047 
00048 void BrickBreakerEngine::print_score(N5110 &lcd){
00049     char buffer[2];
00050     int score = _score;
00051     sprintf(buffer, "%d", score);       //use sprintf so that a variable (rather than a constant) can be use in printString.
00052     lcd.printString(buffer, 72, 0);     //print in top right corner of the screen
00053 }
00054 
00055 void BrickBreakerEngine::read_high_scores(){
00056     FILE *fp;                                   //open file stream
00057     fp = fopen("/sd/bbhighscores.txt", "r");    //open file for brickbreak high scores
00058     
00059     if(fp == NULL){
00060         printf("Error: Could not open file");   //check successfully opened
00061     } else {
00062         int i = 0;
00063         rewind(fp);         //return to start of file
00064         
00065         while(fscanf(fp, "%d,%f", &_index_array[i], &_array_of_values[i]) != EOF){  //read into respective arrays while iterating through line by line
00066             i++;        //increment which line of array to write into
00067         }
00068         fclose(fp);     //close file stream
00069     }
00070 }
00071 
00072 void BrickBreakerEngine::check_high_score(){
00073     read_high_scores();
00074     for(int i = 4; i >= 0; i--){                            //quick algorithm to keep moving the current score
00075         if(_array_of_values[i] < _score){                   //up the leaderboard until it reaches a score greater than itself
00076             _array_of_values[i+1] = _array_of_values[i];
00077             _array_of_values[i] = _score;                   //note: requires the file to be initialised with dummy scores in descending order
00078         }
00079     }               //produces updated array with the current score integrated into high scores if its a top 5 score
00080 }
00081 
00082 void BrickBreakerEngine::write_high_scores(){
00083     check_high_score();
00084     FILE *fp;           //open file stream
00085     fp = fopen("/sd/bbhighscores.txt", "w");        //open brickbreaker high score file ready for writing
00086     if(fp == NULL){
00087         printf("Error: Could not open file");       //check open
00088     } else {
00089         for(int i = 0; i < 5; i++){                 
00090             fprintf(fp, "%d, %f\n", _index_array[i], _array_of_values[i]);      //send first 5 values of index and value array to SD card
00091         }
00092         fclose(fp);         //close file stream
00093     }
00094 }
00095 
00096 void BrickBreakerEngine::time_warning(Gamepad &gamepad, int frame, int fps){
00097     gamepad.leds_on();          //initially set all LEDs on
00098     int game_length = 45 * fps;
00099     if(frame > 0.17 * game_length){     //when 1/6th of game played, turn first LED off
00100         gamepad.led(6, 0);
00101     }
00102     if(frame > 0.33 * game_length){     //when 1/3rd of game played, turn second LED off
00103         gamepad.led(5, 0);
00104     }
00105     if(frame > 0.5 * game_length){      //1/2 played, turn 3rd LED off
00106         gamepad.led(4, 0);
00107     }
00108     if(frame > 0.67 * game_length){     //2/3 played, turn 4th off
00109         gamepad.led(3, 0);
00110     }
00111     if(frame > 0.83 * game_length){     //5/6th played, turn 5th off
00112         gamepad.led(2, 0);
00113     }
00114     if(frame > 0.97 * game_length){     //turn final one off just before the game ends
00115         gamepad.led(1, 0);
00116     }
00117 }
00118     
00119 void BrickBreakerEngine::end(Gamepad &gamepad, N5110 &lcd){
00120     while(!(gamepad.check_event(gamepad.A_PRESSED))){       //check when user wants to advance
00121         lcd.clear();
00122         char buffer[2];
00123         sprintf(buffer, "%d", _score);          //read the final score into buffer
00124         lcd.printString("Time up!", 18, 1);
00125         lcd.printString("You scored:", 9, 3);       //display time up message
00126         lcd.printString(buffer, 36, 4);             //display final score
00127         lcd.printString("(A = back)", 24, 5);       //check for advance message
00128         lcd.refresh();
00129         wait(0.2);          //longer duration between frames to reduce button bounce impact
00130     }
00131 }