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
- Committer:
- JamesCummins
- Date:
- 2019-04-28
- Revision:
- 32:eff573ad8e42
- Parent:
- 26:0dc10374546f
- Child:
- 34:7e03391cb8a6
File content as of revision 32:eff573ad8e42:
#include "BrickBreakerEngine.h" #include <cmath> #include <fstream> #include <iostream> #include <sstream> #include <functional> //Constructor BrickBreakerEngine::BrickBreakerEngine(){ } //Destructor BrickBreakerEngine::~BrickBreakerEngine(){ } //Initialiser void BrickBreakerEngine::init(int radius, Ball &ball){ _ball_radius = radius; ball.init(_ball_radius); srand(time(NULL)); _square_coord.x = 2 + rand()%80; _square_coord.y = 8 + rand()%36; } //Method for rendering void BrickBreakerEngine::brickbreaker_draw(N5110 &lcd, Ball &ball){ ball.draw(lcd); lcd.drawRect(_square_coord.x, _square_coord.y, 5, 5, FILL_BLACK); print_score(lcd); } /////////////Brickbreaker functionality///////////////////// void BrickBreakerEngine::set_score(int score){ _score = score; } void BrickBreakerEngine::generate_rand_square(AnalogIn &randnoise){ int rand = randnoise.read_u16(); Vector2D square_coords = {rand % 80, 8 + rand % 36}; _square_coord = square_coords; } void BrickBreakerEngine::check_square_collision(AnalogIn &randnoise, Ball &ball){ int radius = ball.get_radius(); Vector2D position = ball.get_position(); if (abs(position.x - (_square_coord.x + 2)) <= (radius + 2) and abs(position.y - (_square_coord.y + 2)) <= (radius + 2)) { _score++; generate_rand_square(randnoise); } } void BrickBreakerEngine::print_score(N5110 &lcd){ char buffer[2]; int score = _score; sprintf(buffer, "%d", score); lcd.printString(buffer, 72, 0); } void BrickBreakerEngine::read_high_scores(){ std::ifstream file; file.open("/sd/bbhighscores.txt"); if(!(file.is_open())){ printf("Error: Could not open file"); } std::string nth_score; for(int n = 0; n < 5; n++){ std::getline(file, nth_score); std::istringstream ipstream(nth_score); ipstream >> _hsarray[n]; } } void BrickBreakerEngine::check_high_score(){ read_high_scores(); _score = _hsarray[5]; std::sort(_hsarray, _hsarray + 6, std::greater<int>()); } void BrickBreakerEngine::write_high_scores(){ check_high_score(); int array[5]; for(int i = 0; i < 5; i++){ array[i] = _hsarray[i]; } std::ofstream file; file.open("/sd/bbhighscores.txt"); for(int i = 0; i < 5; i++){ file << array[i] << std::endl; } file.close(); } void BrickBreakerEngine::time_warning(Gamepad &gamepad, int frame, int fps){ gamepad.leds_on(); int game_length = 45 * fps; if(frame > 0.17 * game_length){ gamepad.led(6, 0); } if(frame > 0.33 * game_length){ gamepad.led(5, 0); } if(frame > 0.5 * game_length){ gamepad.led(4, 0); } if(frame > 0.67 * game_length){ gamepad.led(3, 0); } if(frame > 0.83 * game_length){ gamepad.led(2, 0); } if(frame > 0.97 * game_length){ gamepad.led(1, 0); } } void BrickBreakerEngine::end(Gamepad &gamepad, N5110 &lcd){ while(!(gamepad.check_event(gamepad.A_PRESSED))){ lcd.clear(); char buffer[2]; sprintf(buffer, "%d", _score); lcd.printString("Time up!", 18, 1); lcd.printString("You scored:", 9, 3); lcd.printString(buffer, 36, 4); lcd.printString("(A = back)", 24, 5); lcd.refresh(); wait(0.2); } }