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-05-04
- Revision:
- 35:138ad0faa42b
- Parent:
- 34:7e03391cb8a6
- Child:
- 38:a85bc227b907
File content as of revision 35:138ad0faa42b:
#include "BrickBreakerEngine.h" //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(){ FILE *fp; fp = fopen("/sd/bbhighscores.txt", "r"); if(fp == NULL){ printf("Error: Could not open file"); } else { int i = 0; rewind(fp); while(fscanf(fp, "%d,%f", &_index_array[i], &_array_of_values[i]) != EOF){ i++; } fclose(fp); } } void BrickBreakerEngine::check_high_score(){ read_high_scores(); for(int i = 4; i >= 0; i--){ if(_array_of_values[i] < _score){ _array_of_values[i+1] = _array_of_values[i]; _array_of_values[i] = _score; } } } void BrickBreakerEngine::write_high_scores(){ check_high_score(); FILE *fp; fp = fopen("/sd/bbhighscores.txt", "w"); if(fp == NULL){ printf("Error: Could not open file"); } else { for(int i = 0; i < 5; i++){ fprintf(fp, "%d, %f\n", _index_array[i], _array_of_values[i]); } fclose(fp); } } 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); } }